4

I want to add multiple custom reaction for multiple commands Or if we add reaction lists it will add random reactions from that lists. So how to do that.

from discord.utils import get

Add Emoji by Name.

reactions = ['emoji_name_1', 'emoji_name_2', 'emoji_name_3']

@bot.command(pass_context=True)
async def ping1(ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    reply = await bot.say(msg)
    for emoji_name in reactions:
        emoji = get(bot.get_all_emojis(), name=emoji_name)
        await bot.add_reaction(reply, emoji)

Add Emoji by ID.

reactions = ['a:abc:78768768768', 'a:def:768768766', 'a:ghi:878768787687']

@bot.command(pass_context=True)
async def ping2(ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    reply = await bot.say(msg)
    for emoji in emojilist:
        await bot.add_reaction(reply, emoji)

random reaction

reactions = ['a:abc:78768768768', 'a:def:768768766', 'a:ghi:878768787687']

@bot.command(pass_context=True)
async def ping2(ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    reply = await bot.say(msg)
    emojiresult = random.shuffle(reactions)
    for emoji in emojiresult:
        await bot.add_reaction(reply, emoji)
Demotry
  • 869
  • 4
  • 21
  • 40
  • In [the comments here](https://stackoverflow.com/a/48982339/6779307), you mention that you're trying to have the bot add reactions to its response to a command. Could you add that code here? – Patrick Haugh Oct 07 '18 at 20:49
  • 1
    `a:abc:78768768768` is not the name of one of your emoji, so you can't use it like that. Instead, just apply that string to `add_reaction` directly: `for emoji in reactions: await bot.add_reaction(reply, emoji)` – Patrick Haugh Oct 08 '18 at 02:36
  • Finally worked both for `Emoji Name` and `Emoji ID` thank you. – Demotry Oct 08 '18 at 02:47
  • @PatrickHaugh thank you for helping now everything working fine both `Add Emoji by Name` and `Add Emoji by ID` now i trying to make shuffle reaction before adding reactions. but its not working. i added my code above in my question 3rd code. – Demotry Oct 09 '18 at 00:40
  • `shuffle` changes the list in place, it doesn't return anything. – Patrick Haugh Oct 09 '18 at 00:41
  • So what do i need to do ? – Demotry Oct 09 '18 at 00:43
  • 1
    `emojiresult = random.sample(reactions, k=len(reactions))` instead – Patrick Haugh Oct 09 '18 at 00:48
  • Really amazing thank you. all my 90% question is answered by you. – Demotry Oct 09 '18 at 00:53

2 Answers2

3

You need to capture the message that you're sending, then call add_reaction on that message, not the message passed as an argument to on_message

from discord.utils import get

reactions = ['123', '456', '']

@commands.command(pass_context=True)
async def ping(self, ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    reply = await self.bot.say(msg)
    for emoji_id in reactions:
        emoji = get(ctx.server.emojis, name=emoji_id)
        await bot.add_reaction(reply, emoji or emoji_id)  
        # If emoji is None, then emoji_id is likely a unicode emoji
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • That probably means that `get` failed to find the emoji in `server.emojis`. You could try `get(bot.get_all_emojis(), name=emoji_name)` instead. – Patrick Haugh Oct 07 '18 at 21:59
  • Are you sure you have the right emoji names? I went to the Server Settings > Emoji page and used the names under "Alias" without `:` and it works fine. It won't work for unicode emoji, only custom emoji. If you want it to work with unicode emoji, you should change it to `bot.add_reaction(reply, emoji or emoji_name)` and use the unicode character in the `reactions` list – Patrick Haugh Oct 07 '18 at 22:12
  • yes worked for custom emoji. can it work in all server if i put custom emoji in 1 server ? what happens if another server has same name emoji. – Demotry Oct 07 '18 at 22:20
  • Yes, it should work across servers. If you're worried about emoji of the same name, you should use the emojis `id` instead of its name – Patrick Haugh Oct 07 '18 at 22:25
  • Ok if i add emoji id in `reactions = ['5654654', '65654654', '5656565']` then should i change anything in my code ? – Demotry Oct 07 '18 at 22:30
  • Change `emoji_name` to `emoji_id` and `name=emoji_name` to `id=emoji_id` – Patrick Haugh Oct 07 '18 at 22:43
  • `await bot.add_reaction(reply, emoji or emoji_id) Command raised an exception: HTTPException: BAD REQUEST (status code: 400): Unknown Emoji` – Demotry Oct 07 '18 at 23:03
  • For what emojis? Custom emoji form other servers, unicode emojis, animated emojis... If it's animated emojis, you may have to switch to rewrite branch, I'm not sure. – Patrick Haugh Oct 07 '18 at 23:44
  • custom animated emojis id – Demotry Oct 08 '18 at 00:22
  • I think you can send `a:name:id` as text to `add_reaction`, like `add_reaction(reply, "a:myemoji:1234")`. So that would be `add_reaction(reply, 'a:{0.name}:{0.id}'.format(emoji))`. I don't think there's a way of differentiating animated emoji from regular ones on async though – Patrick Haugh Oct 08 '18 at 00:35
  • `await bot.add_reaction(reply, 'a:{0.name}:{0.id}'.format(emoji)) AttributeError: 'NoneType' object has no attribute 'name'` – Demotry Oct 08 '18 at 02:15
  • Are your animated emoji not showing up in `get_all_emojis()`? Again, it's possible that the async branch is incapable of dealing with animated emoji. – Patrick Haugh Oct 08 '18 at 02:21
  • I updated my full code in my question above. i added to code 1 works 2nd not working. please check is there any wrong in my code. – Demotry Oct 08 '18 at 02:35
0
for r in reactions:
    await bot.add_reaction(message, r)
Mehvix
  • 296
  • 3
  • 12