如果用户对😃做出反应,我希望机器人编辑该消息 [英] If the user reacts with 😃, I want the bot to edit the message

查看:21
本文介绍了如果用户对😃做出反应,我希望机器人编辑该消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似于此。

@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 但我想让机器人编辑第一条消息。有人能帮忙吗?

推荐答案

您需要在机器人上线时发送一条消息,然后监视该消息是否有新的反应。要做到这一点,最简单的方法是使用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)
    message = tuple(m.id for m in message)
    emoji = make_sequence(emoji)
    author = make_sequence(author)
    def check(reaction, user):
        if ignore_bot and user.bot:
            return False
        if message and reaction.message.id 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")

这篇关于如果用户对😃做出反应,我希望机器人编辑该消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆