@@ -400,21 +400,51 @@ async def server_stats(self, ctx):
400400
401401 @commands .command (
402402 name = "leave" ,
403- help = "Allows the bot to leave a server via DM by using the guild name" ,
403+ help = "Bans the bot from a server via DM by using the guild name (fallback: leave) " ,
404404 )
405405 @commands .is_owner () # Only the bot owner can execute this command
406406 async def leave (self , ctx , * , guild_name : str ):
407- """Allows the bot to leave a server via DM by using the guild name"""
407+ """Bans the bot from a server via DM by using the guild name. """
408408 if isinstance (ctx .message .channel , discord .DMChannel ):
409409 guild , suggestions = self ._find_guild_by_name (guild_name )
410410 if guild :
411411 try :
412- await guild .leave ()
413- await ctx .send (f"Successfully left the server: { guild .name } " )
412+ # Prefer self-ban so accidental re-adds are blocked unless manually unbanned.
413+ await guild .ban (
414+ self .bot .user ,
415+ reason = f"Self-ban requested by owner { ctx .author } via DM command." ,
416+ )
417+ await ctx .send (
418+ f"Successfully banned myself from the server: { guild .name } "
419+ )
414420 except discord .Forbidden :
421+ # If ban permission is missing, still leave as a fallback.
422+ try :
423+ await guild .leave ()
424+ await ctx .send (
425+ f"I couldn't ban myself in { guild .name } (missing permissions), but I left the server instead."
426+ )
427+ except discord .Forbidden :
428+ await ctx .send (
429+ f"Failed to self-ban or leave { guild .name } . I don't have the required permissions."
430+ )
431+ except discord .HTTPException as e :
432+ await ctx .send (
433+ f"Failed to self-ban and failed to leave { guild .name } : { e } "
434+ )
435+ except discord .HTTPException as e :
415436 await ctx .send (
416- f"Failed to leave the server: { guild .name } . I don't have the required permissions ."
437+ f"Failed to self-ban from { guild .name } : { e } . Attempting to leave instead ."
417438 )
439+ try :
440+ await guild .leave ()
441+ await ctx .send (
442+ f"Fallback successful: left the server { guild .name } ."
443+ )
444+ except Exception as leave_error :
445+ await ctx .send (
446+ f"Fallback leave failed for { guild .name } : { leave_error } "
447+ )
418448 else :
419449 if suggestions :
420450 suggestion_text = "\n " .join (f"- { name } " for name in suggestions )
0 commit comments