4

i am making a management discord bot with discord.py, so i realized that i need to add a command to temp ban someone for some time, this ban can be by roles or by kicking the member out of the channel and then banning him, but i don't know how to do that. Can someone help me?

2

2 Answers 2

3

After a lot of trial and error I finally got it! Given bellow is a discord.py bot with a command to temporarily ban a user and can be used for multiple users

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())

enter image description here

I tested this program by banning three users for distinct amounts of time and it worked like a charm. Please note that the time may not be too accurate. The greater the time you choose, the greater the error.

For some reason users that are offline cannot be banned by a Bot.

The bot has to be online full time for this to work... If you reboot the bot or the bot crashes all lists get cleared.

0
3

It depends on what you mean by "temporary ban".

Do you want the user actually kicked out and banned from the server for a certain period of time, or do you want the user to be temporarily restricted from certain permissions such as chatting?

I recommend the latter and using the Discord rewrite branch of the API which is new and improved.

Restrict a member via role assignment and unrestrict after x seconds:

@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)

Ban a user and unban after x seconds:

@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)

Keep in mind, if your bot crashes or goes offline for whatever reason while it's in the process of sleeping to unban a user, the bot will not unban the user after it comes back up, so something to consider using may be a database and storing the end time of the ban. You can then query all saved dates during bot startup to figure out how long to sleep for. Also, you will have to get their User object rather than Member object as they are not a part of the guild anymore.

2
  • The problem is, while this command is going on the bot will not be able to do anything else, and if the duration is more than 60 seconds the bot will crash with error 'Task was destroyed but it is pending'
    – Sujit
    Mar 8, 2019 at 6:04
  • 2
    @Sujit I don't think the rewrite branch of the API has this problem, and it's using asyncio.sleep() rather than time.sleep() so it's not blocking anything. I just set the duration to 5+ minutes and used various commands while it was running, with no issues.
    – Mary
    Mar 8, 2019 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.