繁体   English   中英

disnake (discord.py 也可以工作.. 或不,取决于) postgreSQL setprefix 命令

[英]disnake (discord.py works also.. or not, depends) postgreSQL setprefix command

我目前在一个可能很容易的情况下陷入困境......我的!setprefix<prefix>不会做任何事情。 它只是空的。 当我在任何此代码中插入print(DEFAULT_PREFIX)时,它将打印设置的默认值“。”,即使在我的 postgreSQL 数据库中也是如此。 默认设置为“.”。 (通过删除和重新启动所有内容重新检查)我敢打赌这与我在 main.py 中设置的 bot.db 有关。

我的 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(作为 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))

因此,经过长时间的调试运行,我终于遇到了问题。 这是由于我的getprefix() function 无法解析bot.db并且仅导入会导致其他问题。

所以我只是将整个getprefix() function 移动到main.py并将prefix.py ctx.db.execute(*)更改为self.bot.db.execute(*)和瞧,它的工作原理。 这让我很头疼,这是我必须指出的事情。 也许这将帮助任何其他陷入组织错误的人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM