async def handle(self, message, command): if await author_is_mod(message.author, self.storage): if len(command) == 2: if is_number(command[1]): guild_id = str(message.guild.id) user_id = int(command[1]) muted_role_id = int(self.storage.settings["guilds"][guild_id]["muted_role_id"]) user = message.guild.get_member(user_id) muted_role = message.guild.get_role(muted_role_id) if user is not None: # Remove the muted role from the user and remove them from the guilds muted users list await user.remove_roles(muted_role, reason=f"Unmuted by {message.author.name}") self.storage.settings["guilds"][guild_id]["muted_users"].pop(str(user_id)) await self.storage.write_settings_file_to_disk() # Message the channel await message.channel.send(f"**Unmuted user:** `{user.name}`**.**") # Build the embed and message it to the log channel embed_builder = EmbedBuilder(event="unmute") await embed_builder.add_field(name="**Executor**", value=f"`{message.author.name}`") await embed_builder.add_field(name="**Unmuted user**", value=f"`{user.name}`") embed = await embed_builder.get_embed() log_channel_id = int(self.storage.settings["guilds"][guild_id]["log_channel_id"]) log_channel = message.guild.get_channel(log_channel_id) if log_channel is not None: await log_channel.send(embed=embed) else: await message.channel.send(self.invalid_user.format(user_id=user_id, usage=self.usage)) else: await message.channel.send(self.not_a_user_id.format(user_id=command[1], usage=self.usage)) else: await message.channel.send(self.not_enough_arguments.format(usage=self.usage)) else: await message.channel.send("**You must be a moderator to use this command.**")
async def handle(self, guild): guild_id = str(guild.id) log_channel_id = int( self.storage.settings["guilds"][guild_id]["log_channel_id"]) log_channel = guild.get_channel(log_channel_id) # Get the actions we already logged recently logged_actions = [] async for message in log_channel.history(limit=25): for embed in message.embeds: for field in embed.fields: if field.name == "**Audit Log ID**": logged_actions.append(int(field.value.replace("`", ""))) # Get recent kick actions async for entry in guild.audit_logs(action=discord.AuditLogAction.kick, limit=5): # If the entry was made by the bot or it's entry ID has already been logged, skip it. if entry.user == self.client.user or entry.id in logged_actions: continue else: # Build a kick embed with the info. embed_builder = EmbedBuilder(event="kick") await embed_builder.add_field(name="**Executor**", value=f"`{entry.user.name}`") await embed_builder.add_field(name="**Kicked User**", value=f"`{entry.target.name}`") await embed_builder.add_field(name="**Reason**", value=f"`{entry.reason}`") await embed_builder.add_field(name="**Audit Log ID**", value=f"`{entry.id}`") embed = await embed_builder.get_embed() await log_channel.send(embed=embed)
async def execute(self, message, **kwargs): command = kwargs.get("args") if await author_is_mod(message.author, self.storage): if len(command) >= 2: if is_number(command[0]): user_id = int(command[0]) duration = parse_duration(command[1]) if is_valid_duration(duration): guild_id = str(message.guild.id) mute_duration = int(time.time()) + duration muted_role_id = int(self.storage.settings["guilds"][guild_id]["muted_role_id"]) user = await message.guild.fetch_member(user_id) muted_role = message.guild.get_role(muted_role_id) if len(command) >= 3: # Collects everything after the first two items in the command and uses it as a reason. temp = [item for item in command if command.index(item) > 1] reason = " ".join(temp) else: reason = f"Temp muted by {message.author.name}" if user is not None: # Add the muted role and store them in guilds muted users list. We use -1 as the duration to state that it lasts forever. await user.add_roles(muted_role, reason=f"Muted by {message.author.name}") self.storage.settings["guilds"][guild_id]["muted_users"][str(user_id)] = {} self.storage.settings["guilds"][guild_id]["muted_users"][str(user_id)]["duration"] = mute_duration self.storage.settings["guilds"][guild_id]["muted_users"][str(user_id)]["reason"] = reason self.storage.settings["guilds"][guild_id]["muted_users"][str(user_id)]["normal_duration"] = command[1] await self.storage.write_settings_file_to_disk() # Message the channel await message.channel.send(f"**Temporarily muted user:** `{user.name}` **for:** `{command[1]}`**. Reason:** `{reason}`") # Build the embed and message it to the log channel embed_builder = EmbedBuilder(event="tempmute") await embed_builder.add_field(name="**Executor**", value=f"`{message.author.name}`") await embed_builder.add_field(name="**Muted user**", value=f"`{user.name}`") await embed_builder.add_field(name="**Reason**", value=f"`{reason}`") await embed_builder.add_field(name="**Duration**", value=f"`{command[1]}`") embed = await embed_builder.get_embed() log_channel_id = int(self.storage.settings["guilds"][guild_id]["log_channel_id"]) log_channel = message.guild.get_channel(log_channel_id) if log_channel is not None: await log_channel.send(embed=embed) else: await message.channel.send(self.invalid_user.format(user_id=user_id, usage=self.usage)) else: await message.channel.send(self.invalid_duration.format(user_id=user_id, usage=self.usage)) else: await message.channel.send(self.not_a_user_id.format(user_id=command[0], usage=self.usage)) else: await message.channel.send(self.not_enough_arguments.format(usage=self.usage)) else: await message.channel.send("**You must be a moderator to use this command.**")
async def execute(self, message, **kwargs): command = kwargs.get("args") if await author_is_mod(message.author, self.storage): if len(command) == 1: if is_integer(command[0]): user_id = int(command[0]) guild_id = str(message.guild.id) try: user = await message.guild.fetch_member(user_id) except discord.errors.NotFound or discord.errors.HTTPException: user = None if user is not None: # Unban the user and remove them from the guilds banned users list await message.guild.unban( user, reason=f"Unbanned by {message.author.name}") self.storage.settings["guilds"][guild_id][ "banned_users"].pop(str(user_id)) await self.storage.write_file_to_disk() # Message the channel await message.channel.send( f"**Unbanned user:** `{user.name}`**.**") # Build the embed and message it to the log channel embed_builder = EmbedBuilder(event="unban") await embed_builder.add_field( name="**Executor**", value=f"`{message.author.name}`") await embed_builder.add_field(name="**Unbanned user**", value=f"`{user.name}`") embed = await embed_builder.get_embed() log_channel_id = int(self.storage.settings["guilds"] [guild_id]["log_channel_id"]) log_channel = message.guild.get_channel(log_channel_id) if log_channel is not None: await log_channel.send(embed=embed) else: await message.channel.send( self.invalid_user.format(user_id=user_id, usage=self.usage)) else: await message.channel.send( self.not_a_user_id.format(user_id=command[0], usage=self.usage)) else: await message.channel.send( self.not_enough_arguments.format(usage=self.usage)) else: await message.channel.send( "**You must be a moderator to use this command.**")
async def handle(self, message): # Ignore deletes of bot messages or messages from ourselves if message.author == self.client.user or message.author.bot: return # Build an embed that will log the deleted message embed_builder = EmbedBuilder(event="delete") await embed_builder.add_field(name="**Channel**", value=f"`#{message.channel.name}`") await embed_builder.add_field(name="**Author**", value=f"`{message.author.name}`") await embed_builder.add_field(name="**Message**", value=f"`{message.content}`") await embed_builder.add_field(name="**Created at**", value=f"`{message.created_at}`") embed = await embed_builder.get_embed() # Message the log channel the embed of the deleted message guild_id = str(message.guild.id) log_channel_id = int(self.storage.settings["guilds"][guild_id]["log_channel_id"]) log_channel = discord.utils.get(message.guild.text_channels, id=log_channel_id) if log_channel is not None: await log_channel.send(embed=embed)
async def handle(self, member): guild = member.guild guild_id = str(guild.id) muted_role_id = int( self.storage.settings["guilds"][guild_id]["muted_role_id"]) log_channel_id = int( self.storage.settings["guilds"][guild_id]["log_channel_id"]) muted_role = guild.get_role(muted_role_id) log_channel = guild.get_channel(log_channel_id) muted_users = self.storage.settings["guilds"][guild_id]["muted_users"] mutes_to_remove = [] # Loop over the muted users for user_info in muted_users.items(): user_id = int(user_info[0]) duration = int(user_info[1]["duration"]) normal_duration = user_info[1]["normal_duration"] user = await guild.fetch_member(user_id) # if the user_id for this user_info matches the member who joined the guild if user_id == member.id: if -1 < duration < int(time.time()): # Mute is expired. Remove it from the guild's storage mutes_to_remove.append(user_id) # Build a mute expire embed and message it to the log channel embed_builder = EmbedBuilder(event="muteexpire") await embed_builder.add_field(name="**Unmuted user**", value=f"`{user.name}`") await embed_builder.add_field(name="**Mute duration**", value=f"`{normal_duration}`") embed = await embed_builder.get_embed() await log_channel.send(embed=embed) else: # Mute is not expired. Re-add it to the offender await user.add_roles( muted_role, reason= "Remuted user since they had an active mute when they rejoined the server" ) for user_id in mutes_to_remove: self.storage.settings["guilds"][guild_id]["muted_users"].pop( str(user_id)) await self.storage.write_settings_file_to_disk()
async def check_punishments(client): while True: for guild in client.guilds: guild_id = str(guild.id) muted_role_id = int(client.storage.settings["guilds"][guild_id]["muted_role_id"]) log_channel_id = int(client.storage.settings["guilds"][guild_id]["log_channel_id"]) muted_role = guild.get_role(muted_role_id) log_channel = guild.get_channel(log_channel_id) # Get the muted users for the server muted_users = client.storage.settings["guilds"][guild_id]["muted_users"] mutes_to_remove = [] for user_info in muted_users.items(): user_id = int(user_info[0]) duration = int(user_info[1]["duration"]) normal_duration = user_info[1]["normal_duration"] if -1 < duration < int(time.time()): # Mute is expired. Remove it from the user and remove it from the guild's storage user = guild.get_member(user_id) # Happens if they leave the guild. We re-add the role when they return though so this doesn't bypass the mute. if user is None: continue await user.remove_roles(muted_role, reason="Temp mute expired.") mutes_to_remove.append(user_id) # Build a mute expire embed and message it to the log channel embed_builder = EmbedBuilder(event="muteexpire") await embed_builder.add_field(name="**Unmuted user**", value=f"`{user.name}`") await embed_builder.add_field(name="**Mute duration**", value=f"`{normal_duration}`") embed = await embed_builder.get_embed() await log_channel.send(embed=embed) # Loop over all the mutes to remove and remove them from the storage. # (This is done aftewards since if we do it in the loop, python complains the dict size changed) for user_id in mutes_to_remove: client.storage.settings["guilds"][guild_id]["muted_users"].pop(str(user_id)) await client.storage.write_settings_file_to_disk() # Not added yet so I left it blank for now banned_users = client.storage.settings["guilds"][guild_id]["banned_users"] bans_to_remove = [] for user_info in banned_users.items(): user_id = int(user_info[0]) duration = int(user_info[1]["duration"]) normal_duration = user_info[1]["normal_duration"] if -1 < duration < int(time.time()): # Ban is expired. Unban the user and remove it from the guild's storage user = await client.fetch_user(user_id) if user is None: print(f"No user with id {user_id}") continue await guild.unban(user, reason="Temp ban expired") bans_to_remove.append(user_id) # Build a ban expire embed and message it to the log channel. embed_builder = EmbedBuilder(event="banexpire") await embed_builder.add_field(name="**Unbanned user**", value=f"`{user.name}`") await embed_builder.add_field(name="**Ban duration**", value=f"`{normal_duration}`") embed = await embed_builder.get_embed() await log_channel.send(embed=embed) # Loop over all the mutes to remove and remove them from the storage. # (This is done aftewards since if we do it in the loop, python complains the dict size changed) for user_id in bans_to_remove: client.storage.settings["guilds"][guild_id]["banned_users"].pop(str(user_id)) await client.storage.write_settings_file_to_disk() # Run every 5 seconds await asyncio.sleep(5)