简体   繁体   English

Discord.py - 让机器人对自己的消息做出反应

[英]Discord.py - Make a bot react to its own message(s)

I am trying to make my discord bot react to its own message, pretty much.我试图让我的 discord 机器人对自己的消息做出反应,几乎。 The system works like this:该系统的工作原理如下:

A person uses the command,.bug - And gets a message in DM', she/she is supposed to answer those questions.一个人使用命令,.bug - 并在 DM' 中收到一条消息,她/她应该回答这些问题。 And then whatever he/she answered, it will be transferred an embedded message to an admin text-channel.然后无论他/她回答什么,都会将嵌入的消息传输到管理员文本通道。

But I need to add 3 emojis, or react with three different emojis.但我需要添加 3 个表情符号,或者对三个不同的表情符号做出反应。 And depending on what the admin chooses, it will transfer the message once more.并且根据管理员的选择,它会再次传输消息。 So if an admin reacts to an emoji that equals to "fixed", it will be moved to a "fixed" text-channel (the entire message).因此,如果管理员对等于“固定”的表情符号做出反应,它将被移动到“固定”文本通道(整个消息)。

I have done a lot of research about this, but only found threads about the old discord.py, meaning await bot.add_react(emoji) - But as I have understood it, that no longer works!我对此做了很多研究,但只找到了关于旧 discord.py 的线程,意思是await bot.add_react(emoji) - 但据我了解,这不再有效!

Here is my code:这是我的代码:

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)

In discord.py@rewrite, you have to use discord.Message.add_reaction :在 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)

Then, to exploit reactions, you'll have to use the discord.on_reaction_add event.然后,要利用反应,您必须使用discord.on_reaction_add事件。 This event will be triggered when someone reacts to a message and will return a Reaction object and a User object:当有人对消息做出反应时将触发此事件,并将返回Reaction object 和User object:

@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

NB: You'll have to replace "emoji 1" , "emoji 2" and "emoji 3" with your emojis.注意:你必须用你的表情符号替换"emoji 1""emoji 2""emoji 3" add_reactions accepts: add_reactions接受:

  • Global emojis (eg. ) that you can copy on emojipedia您可以在emojipedia上复制的全局表情符号(例如 )
  • Raw unicode (as you tried)原始 unicode (如您所愿)
  • Discord emojis : \N{EMOJI NAME} Discord 表情符号\N{EMOJI NAME}
  • Custom discord emojis (There might be some better ways, I've came up with this after a small amount a research)自定义discord 表情符号(可能有一些更好的方法,经过少量研究后我想出了这个)
     async def get_emoji(guild: discord.Guild, arg): return get(ctx.guild.emojis, name=arg)

When you want to add non custom emojis you need to add its id and name into a format that discord.py can read: <:emoji name:emoji id>当您想添加非自定义表情符号时,您需要将其 id 和名称添加为 discord.py 可以读取的格式: <:emoji name:emoji id>

For example, the white and green check emoji that is a discord default emoji... you would need to write <:white_check_mark:824906654385963018> for discord.py to be able to identify it.例如,作为 discord 默认表情符号的白色和绿色检查表情符号...您需要为 discord.py 编写<:white_check_mark:824906654385963018>才能识别它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM