[英]Why am I unable to call this function? Discord.py and ASYNC/AWAIT issues
I'm creating a discord bot friends but am having trouble in calling this function dice_roll().我正在创建一个 discord 机器人朋友,但在调用这个 function dice_roll() 时遇到了麻烦。 When I don't await it, I get this error: RuntimeWarning: coroutine 'dice_roll' was never awaited However, after adding "await" and trying to use the command, it never runs at all.
当我不等待它时,我收到此错误: RuntimeWarning: coroutine 'dice_roll' was never awaited 但是,在添加“await”并尝试使用该命令后,它根本不会运行。 I've read a bunch of documentation and tried several things, but I'm just not familiar enough with async/await to know what I'm really doing.
我已经阅读了一堆文档并尝试了几件事,但是我对 async/await 还不够熟悉,无法知道我真正在做什么。 I have another command which works fine aka "ich liebe.." in which the bot replies "die Kuche!"
我有另一个运行良好的命令,即“ich liebe..”,其中机器人回复“die Kuche!”
Code is below代码如下
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)
You have set the function to:您已将 function 设置为:
@bot.event
async def dice_roll():
But you want:但你想要:
@bot.command()
async def dice_roll():
Because event is when an event happens such as因为事件是事件发生的时间,例如
@bot.event
async def on_ready():
And a command is like a function, which is callable一个命令就像一个 function,它是可调用的
I made it work我让它工作
import random
import discord
import asyncio
from discord.ext.commands import Bot
bot = discord.Client()
@bot.event
async def dice_roll(message):
try:
await message.channel.send("Roll which dice: ")
msg = await bot.wait_for('message')
dice_type = int(msg.content)
await message.channel.send("How many times: ")
msg2 = await bot.wait_for('message')
num_dice = int(msg2.content)
except ValueError:
await message.channel.send("Please use an integer like '4' or '20'.")
await dice_roll(message)
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":
await dice_roll(message)
print("dice rolled successfully")
elif message.content == "ich liebe":
await message.channel.send("der Kuchen!")
print("cake command complete")
@bot.event
async def on_ready():
print("The bot is ready!")
bot.run(TOKEN)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.