11import asyncio
22import logging
3+ import contextlib
34from datetime import datetime , timedelta
45from typing import Optional , Union
56
@@ -31,11 +32,11 @@ def __init__(self, bot):
3132 self , identifier = 95932766180343808 , force_registration = True
3233 )
3334 defaultsguild = {"muterole" : None , "respect_hierarchy" : True }
34- defaults = {"muted" : {}, "tempbanned " : {}}
35+ defaults = {"muted" : {}, "tbans " : {}}
3536 self .__config .register_guild (** defaultsguild )
3637 self .__config .register_global (** defaults )
3738 self .loop = bot .loop .create_task (self .unmute_loop ())
38- # self.loop2 = bot.loop.create_task(self.unban_loop())
39+ self .loop2 = bot .loop .create_task (self .unban_loop ())
3940
4041 # Removes main mods mute commands.
4142 voice_mute = None
@@ -53,7 +54,11 @@ def __init__(self, bot):
5354
5455 def cog_unload (self ):
5556 self .loop .cancel ()
56- # self.loop2.cancel()
57+ self .loop2 .cancel ()
58+
59+ @commands .command ()
60+ async def banloop (self , ctx ):
61+ await self .unban_loop ()
5762
5863 async def unmute_loop (self ):
5964 while True :
@@ -66,18 +71,39 @@ async def unmute_loop(self):
6671
6772 async def unban_loop (self ):
6873 while True :
69- tempbanned = await self .__config .tempbanned ()
70- for guildid in tempbanned :
71- for user_id in tempbanned [guildid ]:
72- if datetime .fromtimestamp (tempbanned [guildid ][user_id ]["expiry" ]) < datetime .now ():
73- guild = self .bot .get_guild (int (guildid ))
74- if not guild :
74+ tbans = await self .__config .tbans ()
75+ for guild in tbans :
76+ for user in tbans [guild ]:
77+ if datetime .fromtimestamp (tbans [guild ][user ]["expiry" ]) < datetime .now ():
78+ _guild = await self .bot .fetch_guild (int (guild ))
79+ _user = await self .bot .fetch_user (int (user ))
80+ reason = "Automatic ban (Expired temporary ban)"
81+ audit_reason = get_audit_reason (self .bot .user , reason )
82+ queue_entry = (_guild .id , _user )
83+ try :
84+ await _guild .unban (_user , reason = audit_reason )
85+ except discord .HTTPException :
86+ print ("Something went wrong while attempting to unban that user." )
87+ async with self .__config .tbans () as tbans :
88+ if user in tbans [guild ]:
89+ del tbans [guild ][user ]
7590 return
76- bans = await guild .bans ()
77- bans = [be .user for be in bans ]
78- user = discord .utils .get (bans , id = user_id )
79- await guild .unban (user , reason = "Expired temporary ban." )
80- del tempbanned [guildid ][user_id ]
91+ else :
92+ try :
93+ await modlog .create_case (
94+ self .bot ,
95+ _guild ,
96+ datetime .utcnow (),
97+ "unban" ,
98+ _user ,
99+ reason = reason
100+ )
101+ except RuntimeError as e :
102+ print (e )
103+ else :
104+ async with self .__config .tbans () as tbans :
105+ if user in tbans [guild ]:
106+ del tbans [guild ][user ]
81107 await asyncio .sleep (15 )
82108
83109 async def unmute (self , user , guildid , * , moderator : discord .Member = None ):
@@ -350,17 +376,20 @@ async def ban(
350376 ctx : commands .Context ,
351377 user_id : int ,
352378 duration : Optional [commands .TimedeltaConverter ] = None ,
353- days : Optional [int ] = None ,
379+ purge_days : Optional [int ] = None ,
354380 * ,
355381 reason : str = None ,
356382 ):
357383 """Ban a user from this server and optionally temporary or delete days of messages.
358- If days is not a number, it's treated as the first word of the reason.
384+ If purge days is not a number, it's treated as the first word of the reason.
359385 Minimum 0 days, maximum 7. If not specified, defaultdays setting will be used instead."""
360386 guild = ctx .guild
361387 author = ctx .author
388+ days = purge_days
362389
363- user = discord .Object (id = user_id )
390+ user = guild .get_member (user_id )
391+ if not user :
392+ user = await self .bot .fetch_user (user_id )
364393
365394 if not user :
366395 await ctx .send (
@@ -412,15 +441,17 @@ async def ban(
412441
413442 queue_entry = (guild .id , user .id )
414443
415- async with self .__config .tempbanned () as tempbanned :
416- if str (ctx .guild .id ) not in tempbanned :
417- tempbanned [str (ctx .guild .id )] = {}
444+ async with self .__config .tbans () as tbans :
445+ if str (ctx .guild .id ) not in tbans :
446+ tbans [str (ctx .guild .id )] = {}
418447 expiry = datetime .now () + timedelta (seconds = duration .total_seconds ())
419- tempbanned [str (ctx .guild .id )][str (user .id )] = {
448+ tbans [str (ctx .guild .id )][str (user .id )] = {
420449 "time" : datetime .now ().timestamp (),
421450 "expiry" : expiry .timestamp (),
422451 }
423452
453+ unban_time = datetime .utcnow () + duration
454+
424455 if guild .get_member (user_id ):
425456 with contextlib .suppress (discord .HTTPException ):
426457 # We don't want blocked DMs preventing us from banning
@@ -458,8 +489,8 @@ async def ban(
458489 @commands .command (name = "tbanlist" )
459490 async def _tbanlist (self , ctx ):
460491 """List those who are temporary banned."""
461- tempbanned = await self .__config .tempbanned ()
462- guildmuted = tempbanned .get (str (ctx .guild .id ))
492+ tbans = await self .__config .tbans ()
493+ guildmuted = tbans .get (str (ctx .guild .id ))
463494 if guildmuted is None :
464495 return await ctx .send ("There is currently nobody banned in {}" .format (ctx .guild ))
465496 msg = ""
0 commit comments