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(作为齿轮)
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 = ', message.guild.id)
if len(prefix) == 0:
await ctx.db.execute('INSERT INTO prefixes(guild_id, prefix) VALUES (, )', 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 = WHERE guild_id = ', 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()
函数无法解析 bot.db
,只是导入会导致其他问题。
所以我只是将整个 getprefix()
函数移动到 main.py
并将 prefix.py
ctx.db.execute(*)
更改为 self.bot.db.execute(*)
瞧,它起作用了。这让我很头疼,这是我必须指出的事情。也许这会帮助其他陷入组织错误的人。
我目前在一个可能很容易的情况下陷入困境......我的 !setprefix<prefix>
不会做任何事情。它只是空的。当我在此代码中的任何一个中插入 print(DEFAULT_PREFIX)
时,它将打印设置的默认值“!”。即使在我的 postgreSQL 数据库中,默认设置为“!”。 (重新检查删除并重新启动所有内容)
我打赌这与我在 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 = ', message.guild.id)
if len(prefix) == 0:
await ctx.db.execute('INSERT INTO prefixes(guild_id, prefix) VALUES (, )', 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 = WHERE guild_id = ', 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()
函数无法解析 bot.db
,只是导入会导致其他问题。
所以我只是将整个 getprefix()
函数移动到 main.py
并将 prefix.py
ctx.db.execute(*)
更改为 self.bot.db.execute(*)
瞧,它起作用了。这让我很头疼,这是我必须指出的事情。也许这会帮助其他陷入组织错误的人。