python 如何从消息中删除特定React

fnatzsnv  于 2023-04-10  发布在  Python
关注(0)|答案(1)|浏览(119)

我在www.example.com做一个简单的游戏discord.py/pycord,我希望我的机器人能够删除点击时的特定React。有没有办法做到这一点?

以下是预期结果:

以下是我的实际代码(我使用pycord):

import discord
from discord.ext import commands

intents = discord.Intents().all()

bot = commands.Bot(intents=intents)

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    if message.content == 'test':
        me = await message.reply('Is this cool?')
        await me.add_reaction("👎")
        await me.add_reaction("👍")
        try:
            reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user: 
user == message.author and reaction.emoji in ["👎", "👍"], timeout=30.0)

        except asyncio.TimeoutError:
            await message.reply("Tmieout bro")
        else:
            if reaction.emoji == "👍":
                await message.reply('Like it.')
                await reaction.delete()

            else:
                await message.reply("NO like")
                await reaction.delete()

先谢谢你了

o4hqfura

o4hqfura1#

你首先得到React对象,然后你删除它。文档
验证码:

@bot.slash_command(name='removereaction', description="I'm helping someone with their Stack post")
async def removereaction(ctx, message: discord.Option(discord.Message)):
    print(message)
    for i in message.reactions:
        async for user in i.users():
            await i.remove(user)
    await ctx.respond(message.reactions)

它的工作原理是从discord.Option类型的参数message中获取消息。设置discord.Option,以便您可以使用来自链接或ID的消息。然后,它使用此参数循环所有消息的React。然后它循环每个与该ReactReactReact的用户。它必须是异步的,因为i.users()是异步的(参见here)。
完整的代码可以帮助你:

import discord
from discord.ext import commands

intents = discord.Intents().all()

bot = commands.Bot(intents=intents)

@bot.event

async def on_message(message):
    if message.author == bot.user:
        return
    if message.content == 'test':
        me = await message.reply('Is this cool?')
        await me.add_reaction("👎")
        await me.add_reaction("👍")
        try:
            reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user: 
user == message.author and reaction.emoji in ["👎", "👍"], timeout=30.0)

        except asyncio.TimeoutError:
            await message.reply("Tmieout bro")
        else:
            if reaction.emoji == "👍":
                await message.reply('Like it.')
                async for user in reaction.users():
                    await reaction.remove(user)

            else:
                await message.reply("NO like")
                async for user in reaction.users():
                    await reaction.remove(user)

如果你想删除JUST机器人的React,请更改以下行:

async for user in reaction.users():
                        await reaction.remove(user)

收件人:

await reaction.remove(bot.user)

相关问题