0

An error occurs using this code it says bot is not defined. I don't understand cogs that much but I understand a bit of classes too. I wanted to know how functions work in a cog and how variables are assigned like guildstats = ... below.

This is my code: (I am trying to make a database using the discord bot in a guild. The code works without using cogs but I wanted it to be easier to debug any errors so I went for cogs.)

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))
vgalin
  • 491
  • 4
  • 18

1 Answers1

1

You're trying to call the variable bot when you never defined it, your program has no idea what the variable "bot" is, try changing all the times you called bot to instead call self.bot instead

For example your on_ready function should look like this:

    @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})')
Dharman
  • 30,962
  • 25
  • 85
  • 135
FooFooy
  • 63
  • 5