首页 > 解决方案 > bot 没有运行 bs4 discord.py

问题描述

所以我有一个我正在尝试运行的机器人但是当我有这个代码时

import discord
from discord.ext import commands
from bs4 import BeautifulSoup
import aiohttp

class daddy:
    """My custom cog that does stuff!"""

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

@commands.command()
async def dottanow(self):
    """How many players are online atm?"""

    #Your code will go here
    url = "https://steamdb.info/app/570/graphs/" #build the web adress
    async with aiohttp.get(url) as response:
        soupObject = BeautifulSoup(await response.text(), "html.parser")
    try:
        online = soupObject.find(class_='home-stats').find('li').find('strong').get_text()
        await self.bot.say(online + ' players are playing this game at the moment')
    except:
        await self.bot.say("Couldn't load amount of players. No one is playing this game anymore or there's an error.")

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

而且我不知道问题是什么,我的机器人没有做任何事情,也没有打印到控制台,或者在不和谐中放入任何东西,我确信我没有搞砸任何事情。但如果我是,请告诉我。我在 Windows 10 上使用 py 3.6

标签: pythonbeautifulsoupdiscord.py

解决方案


该命令必须进入类内部。在您的代码中,它在类之外。

class daddy:
    """My custom cog that does stuff!"""

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

    @commands.command()
    async def dottanow(self):
        ...

推荐阅读