我现在很难在一个也许简单的情况下..。我的!setprefix<prefix>什么也不做。只是蓬松而已。当我在任何代码中插入一个print(DEFAULT_PREFIX)时,它都会打印set默认值"!“。即使在我的postgreSQL db中,默认设置为"!“。(重新检查删除和重新启动所有内容)我敢打赌,这与我的bot.db (设置为main.py )有关。
我的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)我的prefix.py (作为齿轮)
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))发布于 2022-01-22 15:20:20
因此,经过长期的调试运行,我终于遇到了问题。这是因为我的getprefix()函数无法解决bot.db,只是导入导致了其他问题。
因此,我简单地将整个getprefix()函数移到main.py中,并在prefix.py ctx.db.execute(*)中将其转换为self.bot.db.execute(*)和瞧,这是有效的。这让我很头疼,这也是我必须指出的。马伊比,这将帮助任何其他人,在这一点上,组织错误。
https://stackoverflow.com/questions/70802900
复制相似问题