如果用户对此做出了回应,我希望该机器人编辑该消息

时间:2019-04-22 17:05:05

标签: python discord discord.py

这样的事情。

@client.event
async def on_reaction_add(reaction, user):
    a = await client.say("React to see help")
    if reaction.emoji == "":
        await client.edit_message(a, "Moderator commands")

下面是一个示例:https://cdn.discordapp.com/attachments/562005351353024525/569931890577113098/unknown.png 但是我希望该机器人编辑第一条消息。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:1)

您需要在机器人上线时发送一条消息,然后监视该消息以获取新的反应。最简单的方法是使用Client.wait_for而不是on_reaction_add事件进行后台循环。 reaction_check功能使查找正确的反应更加容易。

from collections.abc import Sequence
from discord import Client

grin = "\N{GRINNING FACE}"

def make_sequence(seq):
    if seq is None:
        return ()
    if isinstance(seq, Sequence) and not isinstance(seq, str):
        return seq
    else:
        return (seq,)

def reaction_check(message=None, emoji=None, author=None, ignore_bot=True):
    message = make_sequence(message)
    emoji = make_sequence(emoji)
    author = make_sequence(make_sequence)
    def check(reaction, user):
        if ignore_bot and user.bot:
            return False
        if message and reaction.message not in message:
            return False
        if emoji and reaction.emoji not in emoji:
            return False
        if author and user not in author:
            return False
        return True
    return check

client = Client()

async def background_loop():
    await client.wait_until_ready()
    channel = client.get_channel(int(*SOME CHANNEL ID*))
    msg = await channel.send("React to see help")
    await msg.add_reaction(grin)
    while not client.is_closed:
        res = await client.wait_for('reaction_add', check=reaction_check(message=msg, emoji=grin))
        if res:  # not None
            await msg.edit(content="Moderator commands")

client.loop.create_task(background_loop())
client.run("TOKEN")

答案 1 :(得分:0)

或者您可以使用软件包discord_interactive_help,该软件包允许您显示一堆页面,并让用户通过反应与本手册进行交互。

免责声明:我是discord_interactive_help的作者。

答案 2 :(得分:0)

您应该能够检测到当前代码的反应,问题出在您的编辑上。

channel = a.channel
msg_id = a.id

if reaction.emoji == "?":
    msg = await channel.fetch_message(msg_id)
    await msg.edit(content = content)