我有一个异步函数,它应该将反应添加到列表中,直到按下某个反应,然后它将添加所有适当的角色。为此,我有一个全局变量“reactions = []
”,当按下“ok”反应时,它会被重置。VS代码在“for react in reactions:
”行告诉我这一点:“reactions is used before it is defined. Move the definition before.
”这让人困惑,因为roleEmojis
是另一个全局变量,但效果很好。
这是我的密码:
import os
import discord
import time
from discord.utils import get
from discord.ext import commands
token = "TOKEN"
BOT = commands.Bot("£")
reactions_list = []
@BOT.event
async def on_reaction_add(reaction , user):
if reaction.emoji in roleEmojis.keys() and user != BOT.user:
if reaction == get(reaction.message.guild.emojis, name = "OK"):
for react in reactions_list:
await user.add_roles(roleEmojis[react.emoji])
reactions_list = []
else:
reactions_list.append(reaction)
BOT.run(token)
旁注:我还没有弄清楚如何检测某人对某条消息“无反应”,我非常感谢您的帮助
除全局变量外,您还可以创建可从任何位置访问的变量,如:
使用
global reactions
在本地方法中使用全局变量根据你提到的错误,你的错误在for循环中,我看不到代码被剪掉了。在python中,如果您使用的是全局变量,则需要明确提到。(除非它是一个列表,并且您可以通过函数参数或使用地址的任何其他数据类型来使用它)
前
结果将为0
结果将是1
相关问题 更多 >
编程相关推荐