前缀命令不会更新 MongoDB

时间:2021-03-01 22:52:31

标签: python mongodb discord.py

所以,我尝试创建一个前缀命令,当你这样做时,我试图让它发布到 MongoDB,它不起作用,有人可以帮助我吗? 我没有错误。 :/

这是一个齿轮,只是让你知道。

    @commands.command(pass_context=True, aliases=["prefix"])
    @commands.has_permissions(administrator=True)
    async def changeprefix(self, ctx, prefix):

        post = {
                '_id': ctx.guild.id,
                'prefix': prefix,
                }
                
        await self.client.prefixes.upsert(post)
        await ctx.send(f'Prefix changed to: {prefix}')
        await ctx.guild.me.edit(nick=f"[{prefix}] «y e s bot»")

1 个答案:

答案 0 :(得分:0)

您必须首先有一个进入数据库的条目。我建议这样做:

guildExist = database.find_one({'_id': ctx.guild.id})
if guildExist == None:
    database.insert_one(post)
else:
    database.update_one({'_id': ctx.guild.id}, {'$set': {'prefix': prefix}})

这会检查公会是否已经在数据库中,如果没有,它将添加它。 然后如果它已经存在,它会更新前缀。

整体代码应该是这样的:

@commands.command(pass_context=True, aliases=["prefix"])
@commands.has_permissions(administrator=True)
async def changeprefix(self, ctx, prefix):

    post = {
            '_id': ctx.guild.id,
            'prefix': prefix,
            }

    guildExist = database.find_one({'_id': ctx.guild.id})
    if guildExist == None:
        database.insert_one(post)
    else:
        database.update_one({'_id': ctx.guild.id}, {'$set': {'prefix': prefix}})

    await self.client.prefixes.upsert(post)
    await ctx.send(f'Prefix changed to: {prefix}')
    await ctx.guild.me.edit(nick=f"[{prefix}] «y e s bot»")