【问题标题】:Execute a command only one time discord.py只执行一次命令 discord.py
【发布时间】:2021-04-26 14:07:10
【问题描述】:

我试图只回复一次特定消息。例如,如果有人写东西,机器人会说“有人没有打招呼”,如果他再次写东西,机器人将不再回复。我使用的命令一直说 else 错误

@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 成为全局变量?如何?为什么?
  • 如何使 i 变量成为全局变量?

标签: python discord discord.py


【解决方案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

【讨论】:

    猜你喜欢
    • 2023-03-12
    • 2020-12-31
    • 1970-01-01
    • 2018-01-23
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    相关资源
    最近更新 更多