import discord from discord.ext import commands from lib.checks import get_user class Mod(commands.Cog): def __init__(self, bot): self.bot = bot @commands.has_permissions(manage_messages=True) @commands.command(aliases=['hban'], pass_context=True) async def hackban(self, ctx, user_id: int): author = ctx.message.author guild = author.guild user = guild.get_member(user_id) if user is not None: return await ctx.invoke(self.ban, user=user) try: await self.bot.http.ban(user_id, guild.id, 0) await ctx.send(f'Banned user: {user_id}') except discord.NotFound: await ctx.send('Could not find user. ') except discord.errors.Forbidden: await ctx.send('Could not ban user. Not enough permissions.') @commands.has_permissions(manage_messages=True) @commands.command(aliases=['p'], pass_context=True, no_pm=True) async def purge(self, ctx, msgs: int, members="everyone", *, txt=None): @property def destination(self): return self.context.message.author if self.bot.pm_help else self.context.message.channel ''' Purge last n messages or nmessages with a word. Requires Manage Messages permission. [p]help purge for more info. Ex: [p]purge 20 - deletes the last 20 messages in a channel sent by anyone. [p]purge 20 everyone stuff - deletes any messages in the last 20 messages that contain the word 'stuff'. [p]purge 20 @appu1232 - deletes any messages in the last 20 messages that were sent by appu1232. [p]purge 20 "@appu1232, LyricLy, 435254873976547426" hello - deletes any messages in the last 20 messages that were sent by appu1232, LyricLy or thecommondude that contain the word 'stuff'. ''' await ctx.message.delete() content = f'I deleted {msgs} message(s) 👻' member_object_list = [] if members != "everyone": member_list = [x.strip() for x in members.split(",")] for member in member_list: if "@" in member: member = member[3 if "!" in member else 2:-1] if member.isdigit(): member_object = ctx.guild.get_member(int(member)) else: member_object = ctx.guild.get_member_named(member) if not member_object: return await ctx.send(self.bot.bot_prefix + "Invalid user.") else: member_object_list.append(member_object) if msgs < 10000: async for message in ctx.message.channel.history(limit=msgs): try: if txt: if not txt.lower() in message.content.lower(): continue if member_object_list: if not message.author in member_object_list: continue await message.delete() except discord.Forbidden: await ctx.send("I do not have permission to delete other users messages.") else: await ctx.send('Too many messages to delete. Enter a number < 10000') await ctx.send(content=content,delete_after=15.0) def setup(bot): bot.add_cog(Mod(bot))