【问题标题】:disnake (discord.py works also.. or not, depends) postgreSQL setprefix commanddisnake (discord.py 也可以工作.. 或不,取决于) postgreSQL setprefix 命令
【发布时间】:2022-01-21 14:28:00
【问题描述】:

我目前陷入了一个可能很容易的境地……我的!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))

【问题讨论】:

  • 我只是随机投反对票? :D 告诉我我缺少什么或可以做得更好。我还在学习,只是投反对票就像“是的,这是****,但我不会帮助你变得更好”。
  • 我在main.py 中没有看到您对prefix.py 的setup(bot) 函数的调用。这样 cog Prefix 就不会被插入到机器人中。
  • main.py: sourceb.in/k8jo8i272l prefix.py: sourceb.in/0ofz6Bxs4y stack 刚刚告诉我“代码太多”,所以我减少了它。但我认为在链接中粘贴了这个代码。我应该更容易获得完整的步骤
  • 您的setprefix 命令是否正常运行并正确响应?
  • 不,它什么也没做。我现在在深入调试每一行的时候把它搞定了,实际上没有人能帮助我。但我知道这是 getprefix 的东西,所以我把它移回了我的 main.py 并且......它起作用了。这是由于我的(现在更改了)setprefix 中的await ctx.db.execute('UPDATE prefixes SET prefix = $1 WHERE guild_id = $2', newprefix, ctx.guild.id) 和我的getprefix() 中引用的bot.db,但我不能只是从main.py 中导入一些东西然后重新导入它。所以我重新组织了我的完整代码,没有拆分 main 中需要的任何函数。

标签: python postgresql discord discord.py


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-10-25
    • 2023-01-24
    • 2020-09-24
    • 1970-01-01
    • 2022-11-25
    • 1970-01-01
    • 2021-08-07
    • 2021-06-23
    • 2022-08-10
    相关资源
    最近更新 更多