首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >disnake (discord.py也工作..。或者不是,取决于) postgreSQL设置前缀命令

disnake (discord.py也工作..。或者不是,取决于) postgreSQL设置前缀命令
EN

Stack Overflow用户
提问于 2022-01-21 14:28:00
回答 1查看 353关注 0票数 -2

我现在很难在一个也许简单的情况下..。我的!setprefix<prefix>什么也不做。只是蓬松而已。当我在任何代码中插入一个print(DEFAULT_PREFIX)时,它都会打印set默认值"!“。即使在我的postgreSQL db中,默认设置为"!“。(重新检查删除和重新启动所有内容)我敢打赌,这与我的bot.db (设置为main.py )有关。

我的main.py

代码语言:javascript
复制
*
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 (作为齿轮)

代码语言:javascript
复制
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))
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-22 15:20:20

因此,经过长期的调试运行,我终于遇到了问题。这是因为我的getprefix()函数无法解决bot.db,只是导入导致了其他问题。

因此,我简单地将整个getprefix()函数移到main.py中,并在prefix.py ctx.db.execute(*)中将其转换为self.bot.db.execute(*)和瞧,这是有效的。这让我很头疼,这也是我必须指出的。马伊比,这将帮助任何其他人,在这一点上,组织错误。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70802900

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档