#What's the best way to use a database in a discord bot

1 messages · Page 1 of 1 (latest)

lilac star
#

I want to use a postgreSQL database for my discord bot, and I was thinking what is the way to do it. I was told to use a connection pool, but i'm not sure how to make it accessible to every cog

graceful walrus
#

?tag botvar

ornate lichenBOT
#

Need to keep track of a variable between functions? No problem!

⚠️ Careful what you name it though, else you might overwrite something ⚠️

Just add it to your commands.Bot or discord.Client instance like so:

bot = commands.Bot(...)
bot.my_variable = 0

async def foo():
    bot.my_variable += 1

# In a cog
@commands.command()
async def counter(self,ctx):
    await ctx.send("Current Counter is at {}".format(ctx.bot.my_variable))

This also allows you to access this from other cogs/extensions/functions. Anywhere you have access to the bot instance