I've got this code for my bot, I'm trying to make a !poll command. In theory, I'll type !poll (question), and then the bot will send my questions as an embed, and under that embed, add a thumbsup and thumbsdown reaction so people can vote. However, I just cannot work out how to make it add the reaction, no matter how hard I try and research. Here's the code:
#commands.has_permissions(administrator=True)
async def poll (ctx, *, message):
embed=discord.Embed(title="New Poll!", description = message, color=0xff0000)
await ctx.send(embed=embed)
So what do I now need to add to my code so it can add a 👍 and 👎 reaction to the embed? Thanks.
Messageable.send returns a discord.Message instance, you then simply .add_reaction()
message = await ctx.send(embed=embed)
await message.add_reaction('👍')
await message.add_reaction('👎')
Reference:
Messageable.send
Message.add_reaction
Related
I was was trying to get it to dm the user who used the command but it doesn't seem to work can someone explain what i did wrong?
#client.event
async def on_message(message):
if isinstance(message.channel, discord.DMChannel):
channel = client.get_channel(942903733262626836)
await channel.send(f"{message.author} sent:\n```{message.content}```")
await client.process_commands(message)
#client.command()
async def feedback(ctx, user: discord.User, *, message='Hello please let us know about your about your feedback.'):
message = message
await user.send(message)
await ctx.send('DM Sent Succesfully.')
If feedback is a command that the user has to write (ex. !feedback) then you can just use ctx.author.send() which will send the dm to the author of the command. What are you trying to do with the on_message function?
#client.command()
async def feedback(ctx, message='Hello please let us know about your about your feedback.'):
await ctx.author.send(message)
await ctx.send('DM Sent Succesfully.')
You made user an argument to the command & you're DMing that person instead of the author.
async def command(ctx, user: discord.User, ...)
must be used as
!command #user
where #user pings the user that will be DM'ed.
You said you wanted to DM the person that used the command, which is the author, so you should DM them instead. You can access the author using ctx.author, and DM them the same way you just DM'ed the other user.
# Instead of
await user.send(...)
# use
await ctx.author.send(...)
I am only trying to learn how to "read" the reaction on a message the same bot sends. I've been stuck for days. I looked it up but the only tutorials I find are with a specific message for roles. I don't care about that, I won't use a single message for the whole server, so there is no way I can get the ID for the message. I only want the bot to send a message, then the user reacts and the bot writes "you reacted with [emoji]".
I found some questions on this site, but they only managed to confuse me even more. Still, this is what I barely managed to make.
#bot.command()
async def react(ctx):
await ctx.send("React to me!")
#bot.event
async def on_reaction_add(reaction, user):
await channel.send("{}, you responded with {}".format(user, reaction))
There is actually a good example of this already in the documentation, you can find it here:
wait_for
But to simplify the whole thing a bit, here is a sample code:
#bot.command()
async def react(ctx):
def check(reaction, user): # Our check for the reaction
return user == ctx.message.author # We check that only the authors reaction counts
await ctx.send("Please react to the message!") # Message to react to
reaction = await bot.wait_for("reaction_add", check=check) # Wait for a reaction
await ctx.send(f"You reacted with: {reaction[0]}") # With [0] we only display the emoji
Here is how it looks like:
Without reaction[0] you would only get unnecessary information. [0] shows you only the first digit, in this case Reaction emoji.
I made a small discord bot, and I need him to delete a message from the writer and then post something. When I use #bot.event it works fine but it's the only thing working in code(only the delete part works). And how to use ctx.message.delete?
#bot.command(aliases=["IDC"])
async def idc(ctx):
ctx.channel.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
You just need to save the channel for later use.
#bot.command(aliases=["IDC"])
async def idc(ctx):
chn = ctx.channel
await ctx.message.delete()
await chn.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
Your bot will need the delete messages permission.
EDIT:
This will also work, but it is recommended to use the first way as it can be faster.
#bot.command(aliases=["IDC"])
async def idc(ctx):
await ctx.send("https://cdn.discordapp.com/attachments/586526210469789717/838335535944564756/Editor40.mp4")
await ctx.message.delete()
async def react(ctx):
emoji = '\N{THUMBS UP SIGN}'
await ctx.add_reaction(emoji)
Doesnt work if I type ".react"
I want the bot to react with a specific emoji on the message if a user types ".react"
Without any extra context, I can't really tell what is causing your issue. However, one thing does stand out is that the add_reaction method is a member of the message attribute of ctx, not `ctx directly. This means you need to access it first, e.g:
await ctx.message.add_reaction()
Changing that might not solve your issue though, especially if you are not getting an error. Do you have #commands.command() or #client.command() before your async def react?
Your overall code should look something like this:
from discord.ext import commands
bot = commands.Bot(command_prefix='.')
#bot.command()
async def react(ctx):
emoji = '\N{THUMBS UP SIGN}'
await ctx.message.add_reaction(emoji)
bot.run('token')
As the title says. I'm trying to figure out how to make a mute command using discord.py rewrite. I'm thinking that we need to have a "mute" role where the command used gives the user the "mute" role and for how long. How do I achieve this.
I already have
#bot.command()
#commands.has_permissions(mute_members)
async def mute(ctx, member:discord.Member):
The best way to do this would be to have a database setup in which you can add and remove users. Then you can use the on_message event and check if the author is in the database, if it is then delete the message. You could also just use a list/file for it to be simpler.
you can create a Muted role and make your bot add the role to the user you want to mute:
#bot.command()
async def mute(ctx, member: discord.Member):
role = discord.utils.get(ctx.guild.roles, name='Muted')
await member.add_roles(role)
await ctx.send("role added")