【发布时间】:2021-06-05 10:06:33
【问题描述】:
我正在创建一个不和谐的机器人朋友,但在调用此函数 dice_roll() 时遇到问题。当我不等待它时,我收到此错误:RuntimeWarning: coroutine 'dice_roll' is never awaited 但是,在添加“await”并尝试使用该命令后,它根本不会运行。我已经阅读了一堆文档并尝试了几件事,但我对 async/await 还不够熟悉,无法知道我真正在做什么。我有另一个运行良好的命令,即“ich liebe..”,其中机器人回复“die Kuche!”
代码如下
import random
import discord
import asyncio
bot = discord.Client()
@bot.event
async def dice_roll():
try:
dice_type = int(input())
await message.channel.send("Roll which dice: ")
num_dice = int(input())
await message.channel.send("How many times: ")
except ValueError:
await message.channel.send("Please use an integer like '4' or '20'.")
await dice_roll()
output = []
for i in range(num_dice):
rolls = random.randint(1,dice_type)
output.append(rolls)
formatted_output = str(output)[1:-1]
await message.channel.send("You rolled {}".format(formatted_output))
print("Task result: Dice was {} and num times was ()".format(dice_type, num_dice))
@bot.event
async def on_message(message):
if message.content == "rolling":
dice_roll()
elif message.content == "ich liebe":
await message.channel.send("die Kuche!")
print("cake command complete")
@bot.event
async def on_ready():
print("The bot is ready!")
bot.run(TOKEN)
【问题讨论】:
-
如果你等待它就会运行......但你正在等待命令提示符中的输入......
-
dice_type = int(input())它在应用程序的终端等待输入,您可以使用bot.wait_for(...)代替,这里是与它相关的文档:bot.wait_for
标签: python asynchronous async-await discord.py