英文:
How to remove a specific reaction from message
问题
I'm developing a simple game in discord.py/pycord and would like my bot to be able to delete a specific reaction when clicked. Is this possible?
我正在开发一个discord.py/pycord中的简单游戏,希望我的机器人能够在点击时删除特定的反应。这是否可能?
I'm trying to reproduce the scenario shown below:
我想复制下面显示的场景:
Here's the code I'm using (I'm using pycord):
这是我使用的代码(我正在使用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("SomeEmoji")
await me.add_reaction("SomeEmoji")
try:
reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user:
user == message.author and reaction.emoji in ["SomeEmoji", "SomeEmoji"], timeout=30.0)
except asyncio.TimeoutError:
await message.reply("Timeout bro.")
else:
if reaction.emoji == "":
await message.reply('Like it.')
await reaction.delete()
else:
await message.reply("NO like")
await reaction.delete()
I want to know how to remove the specific reaction on each click. Currently, this code is not working as expected.
我想知道如何在每次点击时删除特定的反应。目前,这段代码不按预期工作。
英文:
I'm developing a simple game in discord.py/pycord and would like my bot to be able to delete a specific reaction when clicked. Is this possible?
I'm trying to reproduce the scenario shown below:
Here's the code I'm using (I'm using 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("SomeEmoji")
await me.add_reaction("SomeEmoji")
try:
reaction, user = await bot.wait_for("reaction_add", check=lambda reaction, user:
user == message.author and reaction.emoji in ["SomeEmoji", "SomeEmoji"], timeout=30.0)
except asyncio.TimeoutError:
await message.reply("Timeout bro.")
else:
if reaction.emoji == "":
await message.reply('Like it.')
await reaction.delete()
else:
await message.reply("NO like")
await reaction.delete()
I want to know how to remove the specific reaction on each click. Currently, this code is not working as expected.
答案1
得分: 1
以下是代码的翻译部分:
首先获取反应对象,然后将其移除。文档
代码:
@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)
这是代码如何工作的部分,它从参数 message
中获取消息,其类型为 discord.Option
。discord.Option
被设置为可以使用链接或ID从消息中获取消息。然后,它使用这个消息来循环遍历消息的所有反应。然后它循环遍历与该反应关联的每个用户。它必须是异步的,因为 i.users()
是异步的(参见这里)。
以下是可能对你有帮助的完整代码:
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("Timeout 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)
如果你只想移除机器人的反应,请更改以下行:
async for user in reaction.users():
await reaction.remove(user)
为:
await reaction.remove(bot.user)
英文:
You first get the reaction object, then you remove it. Docs
Code:
@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)
How this works is it gets the message from the parameter message
that has type discord.Option
. The discord.Option
is set so that you can use a message from a link or ID. Then, it uses this to cycle through all of the message's reactions. Then it cycles through each user that reacted with said reaction. It must be async, because i.users() is async (See here).
The full code that may help you:
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)
If you want to remove JUST the bot's reactions change the following line:
async for user in reaction.users():
await reaction.remove(user)
To:
await reaction.remove(bot.user)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论