python - Discord.py - 让机器人对其自己的消息使用react

标签 python python-3.x discord.py discord.py-rewrite

我试图让我的不和谐机器人对它自己的消息使用react,几乎。系统是这样工作的:
一个人使用命令!!bug - 并在 DM' 中收到一条消息,她/她应该回答这些问题。然后无论他/她回答什么,它都会将嵌入的消息传输到管理员文本 channel 。
但我需要添加 3 个表情符号,或者用三个不同的表情符号使用react。并且根据管理员的选择,它将再次传输消息。因此,如果管理员对等于“固定”的表情符号使用react,它将被移动到“固定”文本 channel (整个消息)。
我对此做了很多研究,但只找到了关于旧的 discord.py 的线索,意思是await bot.add_react(emoji) - 但据我所知,这不再有效!
这是我的代码:

import discord
from discord.ext import commands
import asyncio

TOKEN = '---'
bot = commands.Bot(command_prefix='!!')

reactions = [":white_check_mark:", ":stop_sign:", ":no_entry_sign:"]


@bot.event
async def on_ready():
    print('Bot is ready.')


@bot.command()
async def bug(ctx, desc=None, rep=None):
    user = ctx.author
    await ctx.author.send('```Please explain the bug```')
    responseDesc = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
    description = responseDesc.content
    await ctx.author.send('````Please provide pictures/videos of this bug```')
    responseRep = await bot.wait_for('message', check=lambda message: message.author == ctx.author, timeout=300)
    replicate = responseRep.content
    embed = discord.Embed(title='Bug Report', color=0x00ff00)
    embed.add_field(name='Description', value=description, inline=False)
    embed.add_field(name='Replicate', value=replicate, inline=True)
    embed.add_field(name='Reported By', value=user, inline=True)
    adminBug = bot.get_channel(733721953134837861)
    await adminBug.send(embed=embed)
    # Add 3 reaction (different emojis) here

bot.run(TOKEN)

最佳答案

在 discord.py@rewrite 中,你必须使用 discord.Message.add_reaction :

emojis = ['emoji 1', 'emoji_2', 'emoji 3']
adminBug = bot.get_channel(733721953134837861)
message = await adminBug.send(embed=embed)

for emoji in emojis:
    await message.add_reaction(emoji)
然后,要利用 react ,您必须使用 discord.on_reaction_add 事件。当有人对消息使用react时将触发此事件并返回 Reaction 对象和 User 目的:
@bot.event
async def on_reaction_add(reaction, user):
    embed = reaction.embeds[0]
    emoji = reaction.emoji

    if user.bot:
        return

    if emoji == "emoji 1":
        fixed_channel = bot.get_channel(channel_id)
        await fixed_channel.send(embed=embed)
    elif emoji == "emoji 2":
        #do stuff
    elif emoji == "emoji 3":
        #do stuff
    else:
        return

注意:您必须更换 "emoji 1" , "emoji 2""emoji 3"用你的表情符号。 add_reactions接受:
  • 您可以在 emojipedia 上复制的全局表情符号(例如😀)
  • 原始 unicode(如您所试)
  • Discord emojis :\N{EMOJI NAME}
  • 定制 discord emojis (可能有一些更好的方法,我经过少量研究后想出了这个)
    async def get_emoji(guild: discord.Guild, arg):
         return get(ctx.guild.emojis, name=arg)
    
  • 关于python - Discord.py - 让机器人对其自己的消息使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62960775/

    相关文章:

    python - 根据用户 react 发送消息

    python - 在 JSON 响应中查找并编辑它以再次发布

    python - 如何获取数据表的正确查询集

    python - 我可以使用 python 中的 coerce() 函数添加两个元组以形成一个新元组吗?

    python - Discord Py 任务未按预期工作。不予返回

    python - 如何对协程进行限速,限速后重新调用协程?

    python - 我如何利用 werkzeug.security 的 check_password_hash 函数根据现有的加盐 sha1 密码哈希值验证密码是否正确

    python - 在检查长度时添加两个列表

    python - getattr() 函数如何处理变量?

    python-3.x - telethon 通过 user_id 获取 access_hass