Python 尝试随机化哪个函数在discord.py中运行,但出现错误
当我运行代码并在discord中键入“please”时,会出现以下错误:Python 尝试随机化哪个函数在discord.py中运行,但出现错误,python,discord.py,Python,Discord.py,当我运行代码并在discord中键入“please”时,会出现以下错误: 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.u
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')
我不知道如何解决这个问题,我想有一个随机选择的函数运行时,我键入“请”。我试着把函数放在一个列表中,然后在它们之间随机选择。我想是on_消息功能给我带来了麻烦。我对编写discord机器人程序还不熟悉,所以这可能是一个非常简单的修复方法。感谢您的帮助。请查看以下代码:
RuntimeWarning: coroutine 'Hello' was never awaited
await coro(*args, **kwargs)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
该列表不存储这两个函数的地址,它实际上调用这两个函数并存储它们的结果。你想要:
@client.event
async def on_message(message):
if message.content == "please":
functions = [Hello(message), Test(message)]
await random.choice(functions)
所以,random.choice
返回一个函数(我们不知道是哪一个),我们希望调用该函数并等待结果
这是在几个函数之间进行选择的一种非常有效的方法。您是否在任何地方运行或定义函数
Hello
呢?是的,就在这里@client.event async def Hello(message):if message.content==“quick”:global x x=1 wait message.channel.send('what Hello in意大利语?').time.sleep(2) wait message.channel.send('您想知道答案吗?(y/n)’如果message.content==“y”和x==1:wait message.channel.send('Ciao'))x=0
这是从底部开始的第二个函数请编辑您的问题以将其包括在内。它在评论中不可读。评论的格式很奇怪,这是从底部开始的第二个函数。啊,明白了,您无法定义自己的客户机。事件,如Hello
或Test
,是有效不一致的列表。py事件。当我运行旧代码并键入“请”时,我用新代码替换了旧代码在discord中,它不会发送任何内容,但也不会给出错误。这些函数无论如何都不会正常运行,on_消息中的检查内容是否为请,Hello检查内容是否为测验,因此,如果你只说请,Hello中的if条件将失败。我去掉if语句,它就工作了d、 非常感谢
@client.event
async def on_message(message):
if message.content == "please":
functions = [Hello, Test]
await random.choice(functions)(message)