【问题标题】:Prefix command won't update MongoDB前缀命令不会更新 MongoDB
【发布时间】:2021-05-31 11:49:33
【问题描述】:

所以,我尝试制作一个前缀命令,当你这样做时,我试图让它发布到 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»")

【问题讨论】:

    标签: python mongodb discord.py


    【解决方案1】:

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

    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»")
    

    【讨论】:

      猜你喜欢
      • 2020-08-26
      • 2021-09-11
      • 2018-05-16
      • 2019-11-09
      • 2021-07-07
      • 2023-01-25
      • 1970-01-01
      • 2021-04-04
      • 1970-01-01
      相关资源
      最近更新 更多