【问题标题】:Discord.py bot events formatted exactly the same yet one runs and one won't runDiscord.py 机器人事件的格式完全相同,但一个运行,一个不会运行
【发布时间】:2019-06-17 15:28:46
【问题描述】:

我最近一直在尝试使用不和谐机器人和不和谐 API,但我遇到了机器人事件的问题。当使用 discord.py 模块创建两个事件时,只有一个可以工作而另一个不能工作,但两者的格式完全相同。为什么会发生这种情况,我该如何解决这个问题?这是我的代码:

@bot.event
async def on_message(message):
    message = await bot.wait_for_message(author=message.author)
    if message.content.startswith('!genlifetime password'):
        global amount
        amount = message.content[len('!genlifetime password'):].strip()
        num = int(amount)
        chars = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
        for x in range(0, num):
            authkey1 = ''
            authkey2 = ''
            authkey3 = ''
            authkey4 = ''
            for i in range(0,4):
                authkey1 = authkey1 + chars[random.randrange(0,35)]
            for i in range(0,4):
                authkey2 = authkey2 + chars[random.randrange(0,35)]
            for i in range(0,4):
                authkey3 = authkey3 + chars[random.randrange(0,35)]
            for i in range(0,4):
                authkey4 = authkey4 + chars[random.randrange(0,35)]
            authkey = authkey1 + '-' + authkey2 + '-' + authkey3 + '-' + authkey4
            print(authkey)
            with open(keyfile, 'a') as f:
                f.write(authkey + ' LIFETIME \n')

@bot.event
async def authorize(message):
    message = await bot.wait_for_message(author=message.author)
    if message.content.startswith('!activate'):
        global key
        key = message.content[len('!activate'):].strip()
        print(key)        

bot.run("NTM3Mzk1NDQzNzAxNjQ1MzEz.DykoDA.x5PrEwxZ0hlY2TeCtKVlg1QsbfQ")

当我运行我的机器人并输入!genlifetime password 10 时,该机器人将生成 10 个应有的密钥,将它们打印到 shell 中,然后将它们放入 keys.txt。但是,authorize 事件根本不起作用。如果我输入 !authorize key 在 shell 中什么也不会发生。根本不打印密钥。我什至尝试在message = await bot.wait_for_message(author=message.author) 之前放置一个打印件,但该打印件也不会打印出来。两个事件的格式相同,为什么一个有效而另一个无效?

【问题讨论】:

  • 如果更改文件中函数的顺序,让authorize()首先出现,它会执行吗?
  • @varlogtim 不,相同的结果发生在生成密钥 1 有效而身份验证无效的情况下。
  • 是不是像 "!authorize" vs "!activate" 这么简单?
  • @CraigMeier 也不起作用。
  • 链接到 API 文档:discordpy.readthedocs.io/en/latest/api.html - 这里提到了 on_message() 函数,但没有提到任何 authorize() 函数

标签: python discord discord.py


【解决方案1】:

我认为代码应该是这样的:

@bot.event
async def on_message(message):
    message = await bot.wait_for_message(author=message.author)
    if message.content.startswith('!activate'):
        global key
        key = message.content[len('!activate'):].strip()
        print(key)        
    if message.content.startswith('!genlifetime password'):
        global amount
        amount = message.content[len('!genlifetime password'):].strip()
        num = int(amount)
        chars = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
        for x in range(0, num):
            authkey1 = ''
            authkey2 = ''
            authkey3 = ''
            authkey4 = ''
            for i in range(0,4):
                authkey1 = authkey1 + chars[random.randrange(0,35)]
            for i in range(0,4):
                authkey2 = authkey2 + chars[random.randrange(0,35)]
            for i in range(0,4):
                authkey3 = authkey3 + chars[random.randrange(0,35)]
            for i in range(0,4):
                authkey4 = authkey4 + chars[random.randrange(0,35)]
            authkey = authkey1 + '-' + authkey2 + '-' + authkey3 + '-' + authkey4
            print(authkey)
            with open(keyfile, 'a') as f:
                f.write(authkey + ' LIFETIME \n')

虽然,我还没有测试过。

【讨论】:

  • 虽然,如果这确实有效,我可能会进一步抽象,并有一个 on_message() 函数来处理读取消息,然后每个单独的函数实际完成工作,例如 gen_password()activate()函数
  • 酷酷!正如我提到的,您可能应该拆分消息处理和其他逻辑,因为当您扩展功能时,这将获得超级消息。
  • 感谢您的建议,一定会这样做!
【解决方案2】:

wait_for_message 调用似乎是多余的:根据文档,传递给回调的 message 参数是您想要的消息。如果您要删除对wait_for_message 的调用并直接使用传入的消息,它可能会按预期工作。

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 2021-08-13
    • 2021-06-11
    • 1970-01-01
    • 2021-03-13
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多