【问题标题】:Discord.py - Make a bot react to its own message(s)Discord.py - 让机器人对自己的消息做出反应
【发布时间】:2020-11-07 15:52:40
【问题描述】:

我试图让我的不和谐机器人对自己的信息做出反应,差不多。系统是这样工作的:

一个人使用命令 !!bug - 并在 DM 中收到一条消息,她/她应该回答这些问题。然后无论他/她回答什么,都会将嵌入的消息传输到管理员文本通道。

但我需要添加 3 个表情符号,或者与三个不同的表情符号做出反应。并且根据管理员的选择,它会再次传输消息。因此,如果管理员对等于“固定”的表情符号做出反应,它将被移动到“固定”文本通道(整个消息)。

我对此做了很多研究,但只找到了关于旧 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)

【问题讨论】:

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


    【解决方案1】:

    在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)
    

    然后,要利用反应,您必须使用discord.on_reaction_add 事件。当有人对消息做出反应时将触发此事件,并返回一个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)
      

    【讨论】:

    • 啊,我明白了!但是在添加表情符号时,我为此使用了非自定义表情符号,但仍然得到它是unknown emoji 的错误,我该怎么写?尝试使用:white_check_mark:。好吧,从我的 emojis 数组中获取它。
    • 啊!我尝试使用原始 unicode,现在可以正常工作了!谢谢:)
    【解决方案2】:

    当您想添加非自定义表情符号时,您需要将其 id 和名称添加为 discord.py 可以读取的格式:<:emoji name:emoji id>

    例如,白色和绿色的检查表情符号是不和谐的默认表情符号... 您需要写 <:white_check_mark:824906654385963018> 以便 discord.py 能够识别它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 2021-08-29
      • 2022-01-05
      • 2020-07-29
      • 2022-01-05
      • 1970-01-01
      • 2020-12-05
      相关资源
      最近更新 更多