有没有办法让 "global" 命令进入并通过 discord.py 命令?
Is there a way to make the "global" command carry into and through a discord.py command?
global t
t = 0
@bot.command()
async def ping(ctx, member: discord.Member):
while True:
await ctx.channel.send(member.mention)
@bot.event
async def on_message(message):
try:
if message.author == member:
t = 5 #(A)
return
except:
pass
if t == 5:
break
第 (A) 行显示错误。我假设问题是变量 t 没有通过 @bot.event 携带,但似乎即使是全局命令也不起作用。还有其他一些我没发现的问题吗?
说明
定义 t
时,您已经在模块(全局)级别定义了它。因此,global t
命令是多余的。
相反,您应该在要在 t
中使用的每个新本地范围内使用 global
。
截至目前,语句 t = 5
创建了一个局部于 on_message
的变量。您可能想要编辑全局 t
,这可以通过对您的代码进行以下编辑来完成:
代码
t = 0
@bot.command()
async def ping(ctx, member: discord.Member):
while True:
await ctx.channel.send(member.mention)
@bot.event
async def on_message(message):
global t # Since we are assigning t in this function, we must state to use t from the global scope
try:
if message.author == member:
t = 5 #(A)
return
except:
pass
if t == 5:
break
参考
global t
t = 0
@bot.command()
async def ping(ctx, member: discord.Member):
while True:
await ctx.channel.send(member.mention)
@bot.event
async def on_message(message):
try:
if message.author == member:
t = 5 #(A)
return
except:
pass
if t == 5:
break
第 (A) 行显示错误。我假设问题是变量 t 没有通过 @bot.event 携带,但似乎即使是全局命令也不起作用。还有其他一些我没发现的问题吗?
说明
定义 t
时,您已经在模块(全局)级别定义了它。因此,global t
命令是多余的。
相反,您应该在要在 t
中使用的每个新本地范围内使用 global
。
截至目前,语句 t = 5
创建了一个局部于 on_message
的变量。您可能想要编辑全局 t
,这可以通过对您的代码进行以下编辑来完成:
代码
t = 0
@bot.command()
async def ping(ctx, member: discord.Member):
while True:
await ctx.channel.send(member.mention)
@bot.event
async def on_message(message):
global t # Since we are assigning t in this function, we must state to use t from the global scope
try:
if message.author == member:
t = 5 #(A)
return
except:
pass
if t == 5:
break