【问题标题】:runtimewarning courutine messagable.send was never awaitedruntimewarning coroutine messageable.send 从未等待
【发布时间】:2020-10-03 05:22:19
【问题描述】:

一点背景 在学习了一点python之后,我一直在尝试制作一个discord bot作为我的第一个宠物项目。我的目标是从我一直在玩的在线游戏中检索API数据并将其粘贴到我的discord服务器上。玩家ID我需要检索的数据来自 discord 命令。例如:!stats 2255880 是 2255880 是游戏内玩家 ID 的命令。我几乎完成了代码,除了我不断收到的地狱错误和谷歌没有帮帮我

错误:

RuntimeWarning: coroutine 'Messageable.send' was never awaited
  message.channel.send(end_data )
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
#to import modules
import discord
from discord.ext import commands
from discord.ext.commands import Bot
import requests
import json


#client for bot
client=discord.Client()

######## TORN API DATA #########


apikey='insert key here'


@client.event
async def on_message(message):

   if message.content.startswith('!stats'):
      def torn_script(player_ID):
         apiurl='https://api.torn.com/user/'+str(player_ID)+'?selections=personalstats&key='+apikey
         r=requests.get(apiurl)
         astext=r.text
         asdict=json.loads(astext)
         data=json.loads(requests.get(apiurl).text)['personalstats']
         print('attacks won: ',data['attackswon'])
         async def discord_display(end_data):
            await message.channel.send(end_data )
         discord_display(['finally the dumbass succeded.LOL']) 
   torn_script(message.content[6:])

【问题讨论】:

  • 您是否尝试在您的代码中使用 Ctrl-F 来“发送”?你知道await是什么吗?
  • 我确实添加了它async def discord_display(end_data): await message.channel.send(end_data ) discord_display(['finally the dumbass succeded.LOL']) ,但得到了同样的信息
  • 在你的def discord_display 中你必须像这样await message.channel.send(end_data)
  • @Abdulaziz 我确实更改了代码。我更新了上面的代码。但它仍然给我同样的错误

标签: python discord.py coroutine


【解决方案1】:

由于discord_display是协程(async def discord_display()),所以需要等待:

async def discord_display(end_data):
    await message.channel.send(end_data )
await discord_display(['finally the dumbass succeded.LOL']) 

PS:如果你已经导入了commands.Bot,为什么还要使用discord.ClientCommands 扩展方式更加实用和强大。

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 2022-10-22
    • 1970-01-01
    • 2018-10-26
    • 2021-08-01
    • 2021-10-22
    • 2022-01-16
    • 2019-12-15
    • 2021-10-15
    相关资源
    最近更新 更多