Python Discord Bot - python clear_reaction() clears all reactions instead of a specific one(Python Discord Bot - python clear_reaction() 清除所有反应而不是特定反应)
问题描述
我正在使用 python 制作一个 Discord Bot,我希望一条消息只有某些反应,并且在添加反应时,我希望代码删除每个不需要的反应.我真的希望它验证是否没有其他反应要删除,而不仅仅是刚刚添加的那个.
I am making a Discord Bot using python and I want a message to have only certain reactions and on adding a reaction I want the code to delete every unwanted one. I really want it to verify if there is no other reactions to remove, not only the one that has just been added.
如标题所述,我的问题是我不知道为什么,但 clear_reaction()
清除了所有反应.
My problem, as said in the title, is that I don't know why but clear_reaction()
clears all reactions.
这是我的代码:
inter_totale = ["✅", "❌"]
@bot.event
async def on_raw_reaction_add(payload):
emoji, user, member, channel = payload.emoji, await bot.fetch_user(user_id=payload.user_id), payload.member, bot.get_channel(payload.channel_id)
msg = await channel.fetch_message(payload.message_id)
if payload.message_id == specific_message:
if inter_totale.count(emoji.name):
pass # NOT DONE YET
else: # deletes unwanted reactions
for r in msg.reactions:
if not inter_totale.count(r.emoji):
await msg.clear_reaction(r)
谢谢.
推荐答案
这将删除所有反应而不是机器人它.
This is going to remove all reactions instead of the bot it.
你可以做这样的事情.if 语句只是为了检查它是否不是机器人本身,它会删除所有不是机器人做出的反应.
You can do something like this. The if statement is just to check if it is not the bot itself, it will remove all reactions not made by the bot.
@bot.event
async def on_raw_reaction_add(payload):
channel = await bot.fetch_channel(payload.channel_id)
message = await channel.fetch_message(payload.message_id)
reaction = discord.utils.get(message.reactions, emoji=payload.emoji.name)
inter_totale = ["✅", "❌"]
if user.id != bot.user.id and payload.emoji.name not in inter_total:
await reaction.remove(payload.member)
这篇关于Python Discord Bot - python clear_reaction() 清除所有反应而不是特定反应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python Discord Bot - python clear_reaction() 清除所有反应而不是特定反应


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01