i'm currently hardstuck in a maybe easy situation... my !setprefix<prefix>
won't do anything. it's just empt. When i insert a print(DEFAULT_PREFIX)
in any of this code, it will print the set default of "!". Even in my postgreSQL db, the default is set to "!". (rechecked with deleting and restarting everything)
I bet it has something to do with my bot.db which is set in main.py.
my main.py
*
from cogs.prefix import getprefix
async def create_db_pool():
bot.db = await asyncpg.create_pool(database="**", user="**", password="**")
print("Database connected succesfully") #placeholder, is set correctly in my code
bot = commands.Bot(command_prefix=getprefix, help_command=None, intents=intents,
activity=activity, status=disnake.Status.idle)
*
bot.loop.run_until_complete(create_db_pool())
bot.run(TOKEN)
my prefix.py (as cog)
from disnake.ext import commands
DEFAULT_PREFIX = '!'
async def getprefix(ctx, message):
if not message.guild:
return commands.when_mentioned_or(DEFAULT_PREFIX)(ctx, message)
prefix = await ctx.db.fetch('SELECT prefix FROM prefixes WHERE guild_id = $1', message.guild.id)
if len(prefix) == 0:
await ctx.db.execute('INSERT INTO prefixes(guild_id, prefix) VALUES ($1, $2)', message.guild.id, DEFAULT_PREFIX)
prefix = DEFAULT_PREFIX
else:
prefix = prefix[0].get("prefix")
return commands.when_mentioned_or(prefix)(ctx, message)
class Prefix(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.has_permissions(administrator=True)
async def setprefix(ctx, newprefix):
await ctx.db.execute('UPDATE prefixes SET prefix = $1 WHERE guild_id = $2', newprefix, ctx.guild.id)
await ctx.send("Updated to prefix by!")
@setprefix.error
async def setprefix_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions):
await ctx.send("You can't do that!")
def setup(bot):
bot.add_cog(Prefix(bot))
So, after a long debugging run i finally got my problem. It was due my getprefix()
function could not resolve bot.db
and just importing leads to other problems.
So i simply moved the whole getprefix()
function into main.py
and changed in prefix.py
ctx.db.execute(*)
into self.bot.db.execute(*)
and voila, it works. That costs me a lot of headache and was the thing that i had to be pointed out. Maaybe this will help any other guy stucked at this point where organizing gone wrong.