I have this
await ctx.send("Which inventory do you want to access?")
await ctx.send("Mining, Collecting, Farming, Fishing or Fighting?")
def check(user):
if user == ctx.author:
# Idk what to do here
pass
type_check = await self.bot.wait_for('message', check=check)
if type_check.content.lower() == "mining":
await ctx.send("You chose Mining!")
if type_check.content.lower() == "collecting":
await ctx.send("You chose Collecting!")
if type_check.content.lower() == "farming":
await ctx.send("You chose Farming!")
if type_check.content.lower() == "fishing":
await ctx.send("You chose Fishing!")
if type_check.content.lower() == "fighting":
await ctx.send("You chose Fighting!")
And I need to check if a user wrote the message and if they did it would await ctx send the thing
The check func must return a boolean, also the argument passed is a discord.Message object not a user
def check(message):
if message.author == ctx.author:
return True
or
# This is a better way
def check(message):
return message.author == ctx.author
btw a better solution for those if statements is checking if they're in a list:
inv_type = type_check.content.lower()
if inv_type in ['mining', 'collecting', 'farming', 'fishing', 'fighting']:
await ctx.send(f"You chose {inv_type}!")
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
The following code is supposed to run when I type $play on my discord server but nothing happens when I run it..
client = commands.Bot(command_prefix='$')
#client.command(name="play")
async def play(ctx):
def check(m):
return m.author == ctx.author and m.channel == ctx.message.channel and m.content.isdigit()
number = random.randint(1,100)
await ctx.send('I have a number in mind between 1 and 100, guess it')
for i in range(0,5):
guess = await client.wait_for("message", check=check)
if int(guess.content) > number:
await ctx.send("The number is greater")
elif int(guess.content) < number:
await ctx.send("The number is smaller")
elif int(guess.content) == number:
await ctx.send("You guessed the number!!.")
else:
return ("It has to be a positive integer between 1 to 100")
else:
await ctx.send("You lost, type $play to play again.")
It looks like your on_message event is blocking commands from working.
Add the following to your on_message event:
#client.event
async def on_message(message):
if message.author == client.user:
return
# Your next events
await client.process_commands(message) # Process commands
We added this because you have overwritten the normal on_message event.
process_commands makes sure that commands are recognized.
See the docs for more information.
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