【问题标题】:Im trying set a custom prefix for my discord bot using postgreSQL using python as front end我尝试使用 python 作为前端的 postgreSQL 为我的不和谐机器人设置自定义前缀
【发布时间】:2021-09-26 00:04:09
【问题描述】:
DEFAULT_PREFIX = '>>'

async def get_prefix(bot, message):
    if not message.guild:
        return commands.when_mentioned_or(DEFAULT_PREFIX)(bot, message)
    prefix = await my_bot.db.fetchrow('SELECT prefix FROM guilds WHERE guild_id = $1', message.guild.id)
    if len(prefix) == 0:
        await my_bot.db.execute('INSERT INTO guilds (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)(bot, message)


intents = discord.Intents().all()
intents.members = True
my_bot = MyBot(command_prefix = get_prefix, case_insensitive = True, intents = intents)
my_bot.remove_command('help')


async def create_db_pool():
    my_bot.db = await asyncpg.create_pool(database = 'tutorial', user = 'postgres', password = 'SHOCKface123!@#')
    print('Connection succesfull')


@my_bot.command()
@commands.has_permissions(administrator = True)
async def chgprefix(ctx, *, new_prefix):
    await my_bot.db.execute('UPDATE guilds SET prefix = $1 WHERE guild_id = $2', new_prefix, ctx.guild.id)
    await ctx.send('changed prefix')

错误:

File "C:\Users\SBA3\Desktop\dasboard\print.py", line 39, in get_prefix
if len(prefix) == 0:
TypeError: object of type 'NoneType' has no len()

【问题讨论】:

  • 第一次听说python是前端语言

标签: python postgresql discord.py


【解决方案1】:

您的查询 prefix = await my_bot.db.fetchrow('SELECT prefix FROM guilds WHERE guild_id = $1', message.guild.id) 似乎正在选择一列(前缀),而不是整行。这意味着如果不存在具有该公会 ID 的现有行,它将返回 None,因为这样的列不存在。

尝试将查询替换为SELECT * FROM guilds ...

【讨论】:

    猜你喜欢
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 2023-03-20
    • 2020-11-25
    • 2017-10-15
    • 2019-03-21
    • 2021-11-19
    相关资源
    最近更新 更多