【发布时间】: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,比如
Hello或Test,here是一个有效discord.py事件的列表。
标签: python discord.py