【发布时间】: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)函数的调用。这样 cogPrefix就不会被插入到机器人中。 -
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