python - 运行时警告 : coroutine was never awaited

标签 python python-3.x python-asyncio discord.py

最近在尝试让我的 Discord 机器人简单地从 Pubg.op.gg 中提取数据并向用户提供其 K/D 时遇到了很多麻烦。

我遇到了多个错误,但出现最多的错误是:

F:\Python3\lib\site-packages\discord\ext\commands\core.py:50: RuntimeWarning: coroutine 'Pubg.get_kd' was never awaited
  ret = yield from coro(*args, **kwargs)

根据错误消息,我只是对等待的内容感到困惑,因为我找不到 Pubg.get_kd。

这是我的代码:

import discord
from discord.ext import commands
import asyncio,aiohttp,json
from bs4 import BeautifulSoup as soup

class Pubg():
    def __init__(self,bot):
        self.bot = bot

    async def get_info(self):
        urlForInfo = await aiohttp.get("https://pubg.op.gg/user/"+user_info[0])
        urlForInfo = await urlForInfo.text()
        # urlForInfo = urllib.request.urlopen("https://pubg.op.gg/user/"+user[0]).read()
        soup = bs.BeautifulSoup(urlForInfo, "lxml")
        info = soup.find("div",{"class":"player-summary__name"})
        playerID = info["data-user_id"]
        playerNickname = info["data-user_nickname"]
        season = soup.find("button",{"class":"ranked-stats-wrapper__season-btn"})
        currentSeason = season["data-season"]
        return playerID, playerNickname, currentSeason

    async def get_kd(self):
        playerID, playerNickname, currentSeason = self.get_info()
        url = "https://pubg.op.gg/api/users/{}/ranked-stats?season={}&server={}&queue_size={}&mode={}".format(playerID,currentSeason,user[3],user[1],user[2])
        resp = requests.get(url).text
        resp = json.loads(resp)
        """ Player Game Stats """
        kills = resp["stats"]["kills_sum"]
        deaths = resp["stats"]["deaths_sum"]

        killDeath = str(kills / deaths)
        KD = killDeath[:4]
        return KD

    @commands.command(pass_context = True)
    async def kd(self, ctx):
        KD = self.get_kd()
        user_info = ctx.message.content.replace(".kd", "")
        user = user_info.split("-")
        await self.bot.say(f"Your K/D is: {KD}")
        #await self.bot.say("test")

def setup(bot):
    bot.add_cog(Pubg(bot))

最佳答案

对于协程,你总是需要 await。当你得到 async definitions 时,你在两个地方忘记了这一点。

所以这段代码现在应该可以工作了(如果这是唯一的问题):

import discord
from discord.ext import commands
import asyncio,aiohttp,json
from bs4 import BeautifulSoup as soup

class Pubg():
    def __init__(self,bot):
        self.bot = bot

    async def get_info(self):
        urlForInfo = await aiohttp.get("https://pubg.op.gg/user/"+user_info[0])
        urlForInfo = await urlForInfo.text()
        # urlForInfo = urllib.request.urlopen("https://pubg.op.gg/user/"+user[0]).read()
        soup = bs.BeautifulSoup(urlForInfo, "lxml")
        info = soup.find("div",{"class":"player-summary__name"})
        playerID = info["data-user_id"]
        playerNickname = info["data-user_nickname"]
        season = soup.find("button",{"class":"ranked-stats-wrapper__season-btn"})
        currentSeason = season["data-season"]
        return playerID, playerNickname, currentSeason

    async def get_kd(self):
        playerID, playerNickname, currentSeason = await self.get_info()
        url = "https://pubg.op.gg/api/users/{}/ranked-stats?season={}&server={}&queue_size={}&mode={}".format(playerID,currentSeason,user[3],user[1],user[2])
        resp = requests.get(url).text
        resp = json.loads(resp)
        """ Player Game Stats """
        kills = resp["stats"]["kills_sum"]
        deaths = resp["stats"]["deaths_sum"]

        killDeath = str(kills / deaths)
        KD = killDeath[:4]
        return KD

    @commands.command(pass_context = True)
    async def kd(self, ctx):
        KD = await self.get_kd()
        user_info = ctx.message.content.replace(".kd", "")
        user = user_info.split("-")
        await self.bot.say(f"Your K/D is: {KD}")
        #await self.bot.say("test")

def setup(bot):
    bot.add_cog(Pubg(bot))

关于python - 运行时警告 : coroutine was never awaited,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50378513/

相关文章:

python - 检查 Great Expectations 中的列名和列类型

python - 用 pandas 并行读取 CSV 会产生巨大的内存泄漏/进程僵尸

python - py3k : case-insensitive list sorting - With or WIthout lambda?

python - future 内部循环的状态始终处于待定状态

python - 使用 asyncio 在一定时间间隔内运行命令并在之后终止它

python - 如何使用 Ctrl-C 优雅地终止 asyncio 脚本?

c++ - Python 的 zip() 等价于 C 或 C++

python - 遍历和修改字典结构的树状列表

python - 使用 lstm 进行 IMDB 评论的准确性非常低

python - 在 DataFrame Python 上同时迭代行和列的形式