#client.command()
#has_permissions(administrator=True)
async def nuke(ctx, channel_name):
channel_id = int(''.join(i for i in channel_name if i.isdigit()))
existing_channel = client.get_channel(channel_id)
await ctx.send("Please confirm nuke: type yes or no")
def check(m):
return m.content == 'yes'
msg = await client.wait_for('message', check=check)
if existing_channel is not None:
await existing_channel.clone(reason="Has been nuked")
await existing_channel.delete()
else:
await ctx.send(f'No channel named **{channel_name}** was found')
def check(s):
return s.content == 'no'
msg = await client.wait_for('message', check=check)
await ctx.send("Nuke has been canceled")
The problem I'm having:
The bot should supposedly cancel the command once you say no, but it does not cancel the command. If you were to say yes right after saying no, the bot proceeds to clone and delete the channel anyway. There are no error messages either. If you need any other information, please let me know.
Wouldn't the line msg = await client.wait_for('message', check=check) pause execution until the condition m.content == 'yes' is met? This should work better:
#client.command()
#has_permissions(administrator=True)
async def nuke(ctx, channel_name):
channel_id = int(''.join(i for i in channel_name if i.isdigit()))
existing_channel = client.get_channel(channel_id)
await ctx.send("Please confirm nuke: type yes or no")
def check(m):
return m.content == 'yes' or m.content == 'no'
msg = await client.wait_for('message', check=check)
if msg.content == 'yes':
if existing_channel is not None:
await existing_channel.clone(reason="Has been nuked")
await existing_channel.delete()
else:
await ctx.send(f'No channel named **{channel_name}** was found')
elif msg.content == 'no':
await ctx.send("Nuke has been canceled")
Related
I am trying to create a command wherein if you react on the embed, the bot sends something back. If I add this code to a cog, it won't work. If possible, can you tell me why?
#bot.command(aliases=['test','t'])
async def Test(self, ctx):
TestEmbed = discord.Embed(title="Definition", description="This is just a test embed", color=11027200)
TestEmbed.add_field(name="\u200b", value="▹❁❁▹❁◃❁❁◃",inline=False)
emojis = ['⬅️','➡️']
TestEmbedAdd = await ctx.send(embed=TestEmbed)
for emoji in emojis:
await TestEmbedAdd.add_reaction(emoji)
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️']
try:
reaction, user = await bot.wait_for('reaction_add', timeout=5, check=check)
if reaction.emoji == '➡️':
await ctx.send("Reaction 2!")
elif reaction.emoji == '⬅️':
await ctx.send("Reaction 1!")
except asyncio.TimeoutError:
await ctx.send("Time is out!")
With the help of the people in the comment section, I have found the problem with my code. I had to change bot.command into commands.command. (I've tried both bot and command, and it still works splendidly). The other crucial thing I had to add was "self" under bot.wait_for. Without the self, the command wouldn't work. Thank you so much for the help.
#commands.command(aliases=['test','t'])
async def Testing(self, ctx):
TestEmbed = discord.Embed(title="Definition", description="This is just a test embed", color=11027200)
TestEmbed.add_field(name="\u200b", value="▹❁❁▹❁◃❁❁◃",inline=False)
emojis = ['⬅️','➡️']
TestEmbedAdd = await ctx.send(embed=TestEmbed)
for emoji in emojis:
await TestEmbedAdd.add_reaction(emoji)
def check(reaction, user):
return user == ctx.author and str(reaction.emoji) in ['⬅️', '➡️']
try:
reaction, user = await self.bot.wait_for('reaction_add', timeout=5, check=check)
if reaction.emoji == '➡️':
await ctx.send("Reaction 2!")
elif reaction.emoji == '⬅️':
await ctx.send("Reaction 1!")
except asyncio.TimeoutError:
await ctx.send("Time is out!")
you need #commands.command instead of #bot.command
I have this:
#commands.command()
async def destroyX(self, ctx):
await ctx.message.delete()
for channel in list(ctx.guild.channels):
try:
await channel.delete()
except:
pass
for user in list(ctx.guild.members):
try:
await user.ban()
except:
pass
for role in list(ctx.guild.roles):
await role.delete()
else:
await ctx.send('You are not allowed to execute this command!')
Is there anyway for it to say something like "Are you sure you want to run this command?" in the chat.
You can use an if-else statement:
#commands.command()
async def destroyX(self, ctx):
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
await ctx.message.delete()
await ctx.send("Are you sure you want to run this command?")
# for user input
response = await self.bot.wait_for('message', check=check)
if response.content == 'yes':
# the actions which should happen if the person responded with 'yes'
else:
# actions which should happen if the person responded with 'no' or something else
This is my help command:
#client.command(pass_context=True, aliases=['cmd', 'commands'])
async def help(ctx):
embed = discord.Embed(colour=discord.Colour.blurple())
embed=discord.Embed(title="", description="This is a list of everything Otay! can do! If you need additional help with Otay! join the [**support server**](https://discord.gg/Wa9DpJGEKv)", color=0x7289da)
embed.set_thumbnail(url="https://cdn.discordapp.com/attachments/776181059757932615/784819540496351233/otay2.png")
embed.add_field(name='`🎉` ***Fun***', value='`8ball`, `coinflip`, `dice`, `joke`, `say`, `fact`, `ss`', inline=False)
embed.add_field(name='`⚒️` ***Moderation***', value='`kick`, `ban`, `unban`, `nick`, `purge`, `slowmode`, `mute`, `unmute`, `lock`, `unlock`', inline=False)
embed.add_field(name='`⚠️` ***Setup***', value='`prefix`, `welcome`', inline=False)
embed.add_field(name='`🕹️` ***Games***', value='`rps`, `tictactoe`', inline=False)
embed.add_field(name='`🎁` ***Giveaway***', value='`gstart`', inline=False)
embed.add_field(name='`📷` ***Images***', value='`cat`, `dog`, `fox`, `koala`, `panda`, `meme`, `blursed`', inline=False)
embed.add_field(name='`🔧` ***Utilities***', value='`avatar`, `ping`, `poll`, `serverinfo`, `botinfo`, `userinfo`, `bitcoin`, `snipe`, `createinvite`, `password`', inline=False)
embed.add_field(name="`🎫` ***Ticket***", value="`configureticket`", inline=False)
embed.set_footer(icon_url=f"https://cdn.discordapp.com/attachments/776181059757932615/784819540496351233/otay2.png", text=ctx.author)
embed.timestamp = datetime.datetime.now()
msg = await ctx.send(embed=embed)
def checkifnotbot(reaction, user):
return user != client.user
await msg.add_reaction('🎉')
await msg.add_reaction('⚒️')
await msg.add_reaction('⚠️')
await msg.add_reaction('🕹️')
await msg.add_reaction('🎁')
await msg.add_reaction('📷')
await msg.add_reaction('🔧')
await msg.add_reaction('🎫')
await msg.add_reaction('🔗')
reaction, user = await client.wait_for("reaction_add", check=checkifnotbot)
if str(reaction.emoji) == "🎉":
embedfun=discord.Embed(title="`🎉`Help Fun", color=0x7298da)
await msg.edit(embed=embedfun)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "⚒️":
embedmod=discord.Embed(title="`⚒️`Help Moderation", color=0x7298da)
await msg.edit(embed=embedmod)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "⚠️":
embedsetup=discord.Embed(title="`⚠️`Setup", color=0x7289da)
embedsetup.timestamp = datetime.datetime.now()
await msg.edit(embed=embedsetup)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🕹️":
embedgames=discord.Embed(title="`🕹️Help Games`", color=0x7289da)
await msg.edit(embed=embedgames)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🎁":
embedgiveaway=discord.Embed(title="`🎁`Help Giveaway", color=0x7298da)
await msg.edit(embed=embedgiveaway)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "📷":
embedimages=discord.Embed(title="`📷`Help Images", color=0x7298da)
embedimages.timestamp = datetime.datetime.now()
await msg.edit(embed=embedimages)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🔗":
embedlinks=discord.Embed(title="`🔗`Links", color=0x7289da)
await msg.edit(embed=embedlinks)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🔧":
embedutils=discord.Embed(title="`🔧`Help Utilities", color=0x7298da)
embedutils.timestamp = datetime.datetime.now()
await msg.edit(embed=embedutils)
await msg.clear_reactions()
await msg.add_reaction('↩️')
elif str(reaction.emoji) == "🎫":
embedticket=discord.Embed(title="`🎫`Help Ticket", color=0x7289da)
await msg.edit(embed=embedticket)
await msg.clear_reactions()
await msg.add_reaction('↩️')
def checkifbot(reaction, user):
return user != client.user
reaction, user = await client.wait_for("reaction_add", check=checkifbot)
if str(reaction.emoji) == "↩️":
await msg.edit(embed=embed)
await msg.clear_reactions()
What i want to happen The embed "resets" it adds all the reactions again and just worked like if you typed -help.
My problem I don't know how to make it edit to the normal help command, because if it edits back and you need to add the reactions again you need to make like a million wait_fors
What i've tried
def checkifbot(reaction, user):
return user != client.user
reaction, user = await client.wait_for("reaction_add", check=checkifbot)
if str(reaction.emoji) == "↩️":
await msg.edit(embed=embed)
await msg.clear_reactions()
Problem with this is that i need to make so many wait_fors because if people want to see another category they have to click the reaction again.
And i tried doing this but this just sends it again and doesnt edit it
def checkifbot(reaction, user):
return user != client.user
reaction, user = await client.wait_for("reaction_add", check=checkifbot)
if str(reaction.emoji) == "↩️":
await help(ctx)
So is there a way to do something like: await msg.edit(help)(ctx)? Or how can i solve this problem?
You can solve this by creating a simple get_help function, that is
async def get_help(ctx):
return embed
# whatever u are currently doing in help(ctx), but return embed instead of sending
async def add_reactions(msg, ctx):
#add reactions
#add wait_for
async def help(ctx):
msg = await ctx.send(embed=get_help(ctx))
await add_reactions(msg, ctx)
def checkifbot(reaction, user):
return msg.id == reaction.message.id and user != client.user #improved check
reaction, user = await client.wait_for("reaction_add", check=checkifbot)
if str(reaction.emoji) == "↩️":
await msg.edit(embed = get_help(ctx))
However, you need not go through this much hassle and use discord.py's HelpCommand. which might be easier to implement and modify as per need.
So basically i was making a modmail system and the problem was we wanted the person who dmed the bot has to react to ✅ if he reacts then the bot has to reply him "OK"
but the code was not working so what is the problem how to fix it?
import discord
import asyncio
client = discord.Client()
#client.event
async def on_message(message):
# empty_array = []
# modmail_channel = discord.utils.get(client.get_all_channels(), name="mod-mail")
if message.author == client.user:
return
if str(message.channel.type) == "private":
embed = discord.Embed(title='Confirmation',
color=0x03d692)
embed.add_field(name="You're sending this message to **The Dynamic Legends**", value="React with :white_check_mark: to confirm." + "\nTo cancel this request, react with :x:.", inline=False)
confirmation_msg = await message.author.send(embed=embed)
await confirmation_msg.add_reaction('✅')
await confirmation_msg.add_reaction('❌')
sent_users = []
sent_users.append(message.author.name)
try:
print('Working')
def check1(reaction, user):
return user == client.user and user!='Mod Mail Humara#5439' and str(reaction.emoji) == '✅'
reaction, user = await client.wait_for("reaction_add", timeout=30.0, check=check1)
# print(reaction, user)
if str(reaction.emoji) == '✅':
message.author.send('yes)
client.run('TOKEN')
There's a logic problem in the check func
return user == client.user
It simply doesn't make sense, instead of == use != and don't put the user!='Mod Mail Humara#5439' part
Your check func fixed:
def check1(reaction, user):
return user != client.user and str(reaction.emoji) == '✅'
Also message.author.send is a coroutine, so you need to await it
await message.author.send("whatever")
Your code:
#client.event
async def on_message(message):
if message.author == client.user:
return
if isinstance(message.channel, discord.DMChannel):
embed = discord.Embed(title='Confirmation', color=0x03d692)
embed.add_field(name="You're sending this message to **The Dynamic Legends**", value="React with :white_check_mark: to confirm." + "\nTo cancel this request, react with :x:.", inline=False)
confirmation_msg = await message.author.send(embed=embed)
await confirmation_msg.add_reaction('✅')
await confirmation_msg.add_reaction('❌')
sent_users = []
sent_users.append(message.author.name)
try:
def check1(reaction, user):
return user != client.user and str(reaction.emoji) == '✅'
reaction, user = await client.wait_for("reaction_add", timeout=30.0, check=check1)
if str(reaction.emoji) == '✅':
await message.author.send('yes')
except Exception as e:
pass
I am making a discord bot with rewrite, when the command runs, the event must finish, but if I want to execute another command I can't beacase the previous one it's not finished and It will send the other messages, how can I stop that?
#client.event
async def on_message(message):
def check(m):
return m.channel == message.channel and m.author != client.user
if message.content.startswith("!order"):
channel = message.author
await channel.send("in game name")
in_game_name = await client.wait_for('message', check=check)
await channel.send("in game ID")
in_game_ID = await client.wait_for('message', check=check)
await channel.send("cargo type")
cargo_type = await client.wait_for('message', check=check)
await channel.send("cargo limit")
cargo_limit = await client.wait_for('message', check=check)
await channel.send("storage")
storage = await client.wait_for('message', check=check)
await channel.send("priority")
priority = await client.wait_for('message', check=check)
You could raise an exception in your check if it sees a certain word. Here, if the bot sees the message CANCEL it will cancel the command:
#client.event
async def on_message(message):
def check(m):
if m.channel == message.channel and m.content == "CANCEL":
raise ValueError("Cancelled command")
return m.channel == message.channel and m.author != client.user
if message.content.startswith("!order"):
channel = message.author
await channel.send("in game name")
in_game_name = await client.wait_for('message', check=check)