0
from discord.ext import commands
bot = commands.Bot()

allowed_role_id = 1234567890 # just an example, i will replace with the ID of the allowed role

@bot.event
async def on_ready():
    print(f'Logged in as {bot.user} (ID: {bot.user.id})')

@bot.command(name='sendmessage')
@commands.has_role(allowed_role_id)
async def send_message(ctx, *, message: str):
    for member in ctx.guild.members:
        if not member.bot:
            dm_channel = await member.create_dm()
            sent_message = await dm_channel.send(message)
            await sent_message.add_reaction("✅")
            await sent_message.add_reaction("❌")
            await sent_message.add_reaction("")

@bot.event
async def on_raw_reaction_add(payload):
    member = bot.get_user(payload.user_id) # Using the user_id here
    if member == bot.user:
        return

    guild = bot.get_guild(1077548297037819985) # Since this is in a dm, use the integer value for your guild.
    member = guild.get_member(payload.user_id)
    channel = bot.get_channel(payload.channel_id)
    message = await channel.fetch_message(payload.message_id)

    if message.author != bot.user:
        return

    log_channel = bot.get_channel(1095793300184764477)
    embed = discord.Embed(title="Reaction Log", color=0x00ff00)
    embed.add_field(name="User", value=f"{member.mention} ({member.id})", inline=True)
    embed.add_field(name="Reaction", value=payload.emoji.name, inline=True)
    embed.add_field(name="Roles", value=" ".join([role.mention for role in member.roles]), inline=False)
    embed.add_field(name="Nickname", value=member.nick, inline=True)
    embed.add_field(name="Joined At", value=member.joined_at.strftime("%Y-%m-%d %H:%M:%S"), inline=True)
    await log_channel.send(embed=embed)

logs the stuff twice, and sends the stuff twice

I expect it to log once, and send once, tried debugging it for 2 hours didn't work.

Note also there is another picture that it also sends twice when I do !sendmessage.

Hope can someone send me what I'm doing wrong and send me a modified code that fixes it, thanks.

Sylvester Kruin
  • 3,294
  • 5
  • 16
  • 39
Technical
  • 23
  • 3
  • Does this answer your question? [Discord bot sends 2 messages instead of 1](https://stackoverflow.com/questions/61552504/discord-bot-sends-2-messages-instead-of-1) – easleyfixed Apr 13 '23 at 21:17
  • Specifically this answer on that question --- @client.command() async def quit(ctx): await ctx.send("Shutting down the bot") return await client.logout() # this just shuts down the bot. – easleyfixed Apr 13 '23 at 21:25
  • i understood this but, is it okay if you can put in a code format so i can learn from this and like understand it, and i can't do client. command due to my code being bot.command, when i try to do what you said it doesn't fit with my code for some reason, if you put in a format of code it'll help me – Technical Apr 13 '23 at 21:33
  • Click the link I suggested and it has the code in code format.. I just copy and pasted it from there to here as an example. – easleyfixed Apr 13 '23 at 21:35
  • async def quit(ctx): Expected function or class declaration after decorator it underlines asysnc as it's wrong – Technical Apr 13 '23 at 21:45
  • Take a look at this link and see if that helps you format it correctly https://stackoverflow.com/questions/42670667/using-classes-as-method-decorators – easleyfixed Apr 13 '23 at 21:54

0 Answers0