【问题标题】:Bot Exclusion discord.py机器人排除 discord.py
【发布时间】:2021-07-04 04:12:53
【问题描述】:

如果用户滥用它(例如发送垃圾邮件命令和其他东西),我如何让我的机器人从使用中排除某人

@client.command(pass_context = True, aliases = ['bban'])
@commands.is_owner()
async def botban(ctx, member: discord.Member = None, banreason='Kein Grund angegeben!'):
    if member is None:
        em = discord.Embed(color = 0xff2200, title = "Argumente Fehlen!",
                           description = f"{ctx.author.mention} Bitte nenne den User, den du vom Bot ausschließen willst\n\n```py\no?botban [@nutzer / id] [(optional) Grund]\n```\n Optional: ```py\no?bban [@nutzer / id] [(optional) Grund]\n```",
                           footer = f'User-ID: ' + str(ctx.author.id) + ' Made by Ohnezahn DNC#8135 with discord.py')
        em.set_author(name = f'{ctx.author.name}#{ctx.author.discriminator}',
                      icon_url = f'{ctx.message.author.avatar_url}')
        return await ctx.send(embed = em)

    elif message.author.id in 'blacklist.json':  # Check if user already banned
        return await ctx.send(
            f"Der Nutzer {member.id} / {member.name} - {member.discriminator} ist bereits ausgeschlossen! Grund:\n\n{banreason}")

    elif member.id not in 'ban.json':
        with open('ban.json', 'w') as fp:
            json.dump(banlist, fp)
            emb = discord.Embed(color = 0xff2200, title = "Bot Ban ausgeführt",
                                description = f'{member.mention} / {member.name}#{member.discriminator} / {member.id} wurde von {ctx.author.name}#{ctx.author.discriminator} / {ctx.author.id} / {ctx.author.mention} vom Bot ausgeschlossen\n\nGrund: {banreason}',
                                footer = f'User-ID: ' + str(member.id) + '/ Made by Ohnezahn DNC#8135 with discord.py')
            emb.set_author(name = f'{ctx.author.name}#{ctx.author.discriminator}',
                           icon_url = f'{ctx.message.author.avatar_url}')
        return await ctx.send(embed = emb)
    else:
        # Add ban to dict
        banlist[member.id] = banreason

        # Update File
        with open('ban.json', 'w') as fp:
            json.dump(banlist, fp)

        emb = discord.Embed(color = 0xff2200, title = "Bot Ban ausgeführt",
                            description = f'{member.mention} / {member.name}#{member.discriminator} / {member.id} wurde von {ctx.author.name}#{ctx.author.discriminator} / {ctx.author.id} / {ctx.author.mention} vom Bot ausgeschlossen\n\nGrund: {banreason}',
                            footer = f'User-ID: ' + str(member.id) + '/ Made by Ohnezahn DNC#8135 with discord.py')
        emb.set_author(name = f'{ctx.author.name}#{ctx.author.discriminator}',
                       icon_url = f'{ctx.message.author.avatar_url}')
        return await ctx.send(embed = emb)


@client.check
def check_commands(ctx):
    # Check if user banned, convert id to str because json.load (line 9) load str id's.
    return str(ctx.author.id) not in blacklisted_users()

这就是我的ban.json现在的样子:

{
    "blacklistedUsers": [
    ]
}

这就是我现在拥有的代码...我现在正在为感觉年龄而努力...

【问题讨论】:

  • 这些assert 声明的目的是什么? python中的一切都是一个对象,它永远是True
  • 欢迎来到堆栈!最简单的方法是创建一个json,然后无论何时运行此命令,您都可以使用Global Check 来检查用户是否在其中。如果您愿意,可以阅读有关使用 json in the linked docs 的更多信息
  • 一个更简单的选择是使用冷却时间
  • @ŁukaszKwieciński 冷却时间!= 机器人排除...它只是让用户等到他/她可以再次使用该命令

标签: discord.py


【解决方案1】:

您好,欢迎来到堆栈溢出,命令中有一些东西叫做检查。您可以添加一个数组,检查用户是否在该数组中。如果是,不要让他们通过使用 return 停止命令来运行命令

【讨论】:

    【解决方案2】:

    欢迎!

    你可以做类似的事情:

    import discord, json
    from discord.ext import commands
    
    intents = discord.Intents.default()
    bot = commands.Bot(command_prefix='?', intents=intents)
    
    # Load banned data from json
    with open('ban.json') as json_file:
        bot.banneds = json.load(json_file)
    
    @bot.event
    async def on_ready():
        print(f'Logged in as {bot.user} (ID: {bot.user.id})')
        print('------')
    
    @bot.command()
    async def test(ctx): # Only random command for test
        await ctx.send("Test okay!")
    
    @bot.command()
    @commands.is_owner()
    async def ban(ctx, member: discord.Member = None, banreason = 'Empty Reason'):
        if member.id in bot.banneds: # Check if user already banned
            await ctx.send("Already banned.")
            return
        else:
            # Add ban to dict
            bot.banneds[member.id] = banreason
    
            # Update File
            with open('ban.json', 'w') as fp:
                json.dump(bot.banneds, fp)
    
            await ctx.send(f"{member} has been banned, because {banreason}!")
        
    # Ban Check (Global)
    @bot.check
    def check_commands(ctx):
        # Check if user banned, convert id to str because json.load (line 9) load str id's.
        return str(ctx.author.id) not in bot.banneds
    
    bot.run('key')
    

    第一次用空对象创建ban.json

    {}
    

    请注意,如果用户禁止尝试使用命令,您会收到错误“discord.ext.commands.errors.CheckFailure:命令测试的全局检查功能失败。”,请将其添加到您的错误处理程序中。

    【讨论】:

    猜你喜欢
    • 2021-03-29
    • 2021-08-07
    • 2021-05-03
    • 1970-01-01
    • 2018-08-16
    • 2021-10-07
    • 2021-10-20
    • 2021-05-25
    • 1970-01-01
    相关资源
    最近更新 更多