【问题标题】:RuntimeWarning: coroutine was never awaitedRuntimeWarning:从未等待协程
【发布时间】:2018-10-26 23:00:24
【问题描述】:

最近在尝试让我的 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))

【问题讨论】:

  • 仔细看看KD = self.get_kd()
  • get_infoget_kd 是协程。你不能只是打电话给他们,你还需要等待他们。 KD = await self.get_kd()playerID, playerNickname, currentSession = await self.get_info()
  • @dirn 有道理,谢谢!

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


【解决方案1】:

始终用于您需要的协程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))

【讨论】:

    【解决方案2】:

    如果你做一个couroutine,那么一个带有异步的函数,每个返回必须是“等待返回”

    【讨论】:

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