Python 只执行一次命令discord.py
我只想回复一条特定的信息一次。例如,如果有人写了一些东西,机器人会说“有人没有打招呼”,如果他再写一些东西,机器人将不再回复。我使用的命令一直在为else说错误Python 只执行一次命令discord.py,python,discord,discord.py,Python,Discord,Discord.py,我只想回复一条特定的信息一次。例如,如果有人写了一些东西,机器人会说“有人没有打招呼”,如果他再写一些东西,机器人将不再回复。我使用的命令一直在为else说错误 @bot.event async def on_message(message): if message.content.startswith('hi'): print('hello') i=0
@bot.event
async def on_message(message):
if message.content.startswith('hi'):
print('hello')
i=0
else:
if i==0:
print('someone did not say hello')
i=i+1
就像有人说的,变量i不是全局变量,每次函数运行时都会重置。尝试在函数外部设置变量,然后在函数内部调用全局变量
respond = True
@bot.event
async def on_message(message):
global respond #tells python to use and modify the respond variable declared outside the function
if message.content.startswith('hi'):
print('hello')
else:
if respond:
print('someone did not say hello')
respond = False
例如,您是否希望
i
成为全局变量?怎么用?为什么?如何使I变量成为全局变量?