【问题标题】:How to "temporary ban" someone with discord.py?如何“临时禁止”使用 discord.py 的人?
【发布时间】:2019-07-28 15:57:39
【问题描述】:

我正在使用 discord.py 制作一个管理不和谐机器人,所以我意识到我需要添加一个命令来临时禁止某人一段时间,这个禁令可以是通过角色或将成员踢出频道然后禁止他,但我不知道该怎么做。有人能帮我吗?

【问题讨论】:

标签: python-3.x scheduled-tasks discord python-asyncio discord.py


【解决方案1】:

经过多次尝试和错误,我终于得到了它!下面是一个 discord.py 机器人,它有一个命令可以暂时禁止一个用户,并且可以用于多个用户

ban_list = []
day_list = []
server_list = []

#This is a background process
async def countdown():
    await client.wait_until_ready()
    while not client.is_closed:
        await asyncio.sleep(1)
        day_list[:] = [x - 1 for x in day_list]
        for day in day_list:
            if day <= 0:
                try:
                    await client.unban(server_list[day_list.index(day)], ban_list[day_list.index(day)])
                except:
                    print('Error! User already unbanned!')
                del ban_list[day_list.index(day)]
                del server_list[day_list.index(day)]
                del day_list[day_list.index(day)]
               
#Command starts here
@client.command(pass_context = True)
async def ban(ctx,member:discord.Member, days = 1):
    if str(ctx.message.author.id) == '<You ID goes here>':
        try:
            await client.ban(member, delete_message_days=0)
            await client.say('User banned for **' + str(days) + ' day(s)**')
            ban_list.append(member)
            day_list.append(days * 24 * 60 * 60)
            server_list.append(ctx.message.server)
        except:
            await client.say('Error! User not active')
    else:
        await client.say('You do not have permission to ban users!')

client.loop.create_task(countdown())

我通过在不同的时间段内禁止三个用户来测试这个程序,它就像一个魅力。请注意,时间可能不太准确。选择的时间越长,误差越大。

由于某些原因,离线用户无法被 Bot 禁止。

机器人必须全时在线才能工作...如果您重新启动机器人或机器人崩溃,所有列表都会被清除。

【讨论】:

    【解决方案2】:

    这取决于您所说的“临时禁令”。

    您是希望用户在一段时间内实际被踢出并被服务器禁止,还是希望用户暂时被限制某些权限,例如聊天?

    我推荐后者并使用 API 的Discord rewrite branch,这是新的和改进的。

    通过角色分配限制成员并在 x 秒后解除限制:

    @bot.command()
    async def restrict(ctx, member:discord.Member, duration: int):
        role = discord.utils.get(ctx.guild.roles, name="Restricted")
        await member.add_roles(role)
        await asyncio.sleep(duration)
        await member.remove_roles(role)
    

    禁止用户并在 x 秒后取消禁止:

    @bot.command()
    async def ban(ctx, user:discord.User, duration: int):
        await ctx.guild.ban(user)
        await asyncio.sleep(duration)
        await ctx.guild.unban(user)
    

    请记住,如果您的机器人在休眠解除用户封禁过程中因任何原因崩溃或离线,则该机器人在恢复后不会解除对用户的封禁,因此需要考虑使用的可能是数据库并存储禁令的结束时间。然后,您可以在 bot 启动期间查询所有保存的日期,以确定睡眠时间。此外,您必须获取他们的 User 对象而不是 Member 对象,因为他们不再是公会的一部分。

    【讨论】:

    • 问题是,当这个命令执行时,机器人将无法做任何其他事情,如果持续时间超过 60 秒,机器人将崩溃并出现错误“任务被破坏但它正在等待中'
    • @Sujit 我不认为 API 的重写分支有这个问题,它使用的是 asyncio.sleep() 而不是 time.sleep() 所以它不会阻塞任何东西。我只是将持续时间设置为 5 分钟以上,并在运行时使用了各种命令,没有任何问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-16
    • 2023-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-11-07
    • 2022-01-19
    相关资源
    最近更新 更多