【问题标题】:Trying to randomize which function runs in discord.py But error occurs试图随机化哪个函数在 discord.py 中运行但发生错误
【发布时间】:2021-05-12 03:23:47
【问题描述】:
import discord
from discord.ext import commands
import random
import time

client = commands.Bot(command_prefix = '')

x = 0

@client.event
async def on_message(self, message):
    if message.author.id == client.user.id:
        return

@client.event
async def on_ready():
    print("bot is ready")

@client.command(aliases=['Signore'])
async def _summon(ctx):
        await ctx.send('Ciao sono Signore Buffo')
    
@client.command(aliases=['Gib'])
async def common_phrases(ctx):
    responses = [
    'Si. Yes.',
    'No. No.',
    'Per favore. Please.',
    'Grazie. Thank you.','Prego. Youre welcome.',
    'Mi scusi. Excuse me.',
    'Mi dispiace. I am sorry.',
    'Buon giorno. Good morning.',
    'Buona sera. Good evening.',
    'Buona notte. Good night.'
    ]
    await ctx.send(random.choice(responses))


@client.event
async def on_message(message):
    if message.content == "please":
        functions = [Hello(message), Test(message)]
        await random.choice(functions)


@client.event
async def Hello(message):
    if message.content == "quiz":
        global x
        x = 1
        await message.channel.send('what is Hello in italian?')
        time.sleep(2)
        await message.channel.send('Would you like to know the answer? (y/n)')
    if message.content == "y" and x == 1:
        await message.channel.send('Ciao')
        x = 0

@client.event
async def Test(message):
    if message.content =="test":
        global x
        x = 2
        await message.channel.send('This is a test')
        time.sleep(2)
        await message.channel.send('this is a test (2)')
    if message.content == "bruh" and x == 2:
        await message.channel.send('test complete')
        x = 0




client.run('normally code here')

当我运行代码并在 discord 中输入“please”时,它给了我错误:

RuntimeWarning: coroutine 'Hello' was never awaited
  await coro(*args, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

我不知道如何解决这个问题,我想在我输入“please”时运行一个随机选择的函数。我试图将这些函数放在一个列表中并在它们之间随机选择。我认为是 on_message 函数给我带来了麻烦。我是不和谐机器人编码的新手,所以这可能是一个非常简单的修复。任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 您是否在任何地方运行或定义函数Hello
  • 是的,这里是@client.event async def Hello(message): if message.content == "quiz": global x x = 1 await message.channel.send('what is Hello in italian?') time.sleep(2) await message.channel.send('Would you like to know the answer? (y/n)') if message.content == "y" and x == 1: await message.channel.send('Ciao') x = 0 ,它是倒数第二个函数
  • 请编辑您的问题以包含该问题。在 cmets 中不可读
  • 抱歉,cmets 的格式很奇怪,它是倒数第二个函数。
  • 啊,你不能定义自己的client.events,比如HelloTesthere是一个有效discord.py事件的列表。

标签: python discord.py


【解决方案1】:

看这段代码:

@client.event
async def on_message(message):
    if message.content == "please":
        functions = [Hello(message), Test(message)]
        await random.choice(functions)

该列表不存储两个函数的地址,它实际上调用了两个函数并存储了它们的结果。你想要:

@client.event
async def on_message(message):
    if message.content == "please":
        functions = [Hello, Test]
        await random.choice(functions)(message)

所以,random.choice 返回一个函数(我们不知道是哪一个),我们想调用该函数并等待结果。

这是在多个功能之间进行选择的一种非常有效的方式。

【讨论】:

  • 我用新代码替换了旧代码,当我运行它并在 discord 中输入“please”时,它不会发送任何内容,但也不会出错。
  • 函数无论如何都不会正常运行,on_message 检查内容是否为please,Hello 检查内容是否为quiz,所以如果你只说please,里面的if 条件你好会失败
  • 我去掉了 if 语句,它起作用了,非常感谢
猜你喜欢
  • 1970-01-01
  • 2017-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多