函数如何在嵌齿轮(discord.py)中工作?

时间:2020-08-24 04:52:40

标签: discord discord.py

使用此代码发生错误,表明未定义bot。我不太了解齿轮,但也了解一些课程。我想知道函数如何在嵌齿轮中工作以及如何像下面的guildstats = ...这样分配变量。

这是我的代码:(我正尝试在行会中使用discord bot创建数据库。该代码无需使用齿轮即可工作,但我希望它更容易调试任何错误,所以我选择了齿轮。)

class Boot(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    guildstats = pd.read_excel('DiscordStats.xlsx',sheet_name=0)
    userstats = pd.read_excel('DiscordStats.xlsx',sheet_name=1)

    def dataframe_to_excel(df1 = guildstats, df2 = userstats):
        with pd.ExcelWriter('DiscordStats.xlsx', mode = 'w') as writer:
            df1.to_excel(writer, index=False, sheet_name = 'GuildStats')
            df2.to_excel(writer, index=False, sheet_name = 'UserStats')

    def guildstats_writer():
        guild_row_data = []
        for guild in self.bot.guilds:
            if '\''+str(guild.id) not in guildstats['GuildID'].to_list():
                guild_row_data.append([guild.name,'\''+str(guild.id),'','',False])
            else:
                pass
        guild_row = pd.DataFrame(guild_row_data,columns = guildstats.columns.to_list())
        guildstats1 = guildstats.append(guild_row,ignore_index=True)
        Boot.dataframe_to_excel(df1=guildstats1)

    @commands.Cog.listener()
    async def on_ready(self):
        Boot.guildstats_writer(self)
        await sbot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="for MeepMop ~help"))
        print(f'{bot.user} is connected to the following guild:')
        for guild in bot.guilds:
            print(f'{guild.name} (id: {guild.id})')

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

1 个答案:

答案 0 :(得分:1)

您试图在从未定义变量 bot 时调用它,但程序不知道变量“ bot”是什么,请尝试更改所有调用bot的时间,改为调用< strong> self.bot 代替

例如,您的on_ready函数应如下所示:

    @commands.Cog.listener()
    async def on_ready(self):
        Boot.guildstats_writer(self)
        await self.bot.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="for MeepMop ~help"))
        print(f'{self.bot.user} is connected to the following guild:')
        for guild in self.bot.guilds:
            print(f'{guild.name} (id: {guild.id})')