【问题标题】:Why am I unable to call this function? Discord.py and ASYNC/AWAIT issues为什么我不能调用这个函数? Discord.py 和 ASYNC/AWAIT 问题
【发布时间】: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


【解决方案1】:

您已将函数设置为:

@bot.event
async def dice_roll():

但你想要:

@bot.command()
async def dice_roll():

因为事件是当事件发生时,例如

@bot.event
async def on_ready():

而且命令就像一个函数,是可调用的

【讨论】:

    【解决方案2】:

    我成功了

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 2016-07-05
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 1970-01-01
      相关资源
      最近更新 更多