def enable(self, settings): self.settings = settings self.salt = utils.random_string() with DBManager.create_session_scope() as db_session: current_timeouts = Timeout._active_timeouts(db_session) for timeout in current_timeouts: if timeout.time_left: ScheduleManager.execute_delayed( timeout.time_left + 1, self.auto_untimeout, args=[timeout.id, self.salt], )
async def is_timedout(self, bot, author, channel, message, args): command_args = message.split(" ") if message else [] member = self.bot.filters.get_member([command_args[0]], None, {})[0] if not member: await self.bot.say(channel=channel, message=f"Cant find member, {command_args[0]}") return False with DBManager.create_session_scope() as db_session: timeout = Timeout._is_timedout(db_session, str(member.id)) if not timeout: await self.bot.say( channel=channel, message=f"The user {member} has not currently timedout", ) return True embed = discord.Embed( description=f"Timeout #{timeout.id}", timestamp=timeout.created_at, colour=member.colour, ) embed.add_field( name="Banned on", value=str(timeout.created_at.strftime("%b %d %Y %H:%M:%S %Z")), inline=False, ) embed.add_field( name="Banned till" if timeout.time_left != 0 else "Unbanned on", value=str(timeout.until.strftime("%b %d %Y %H:%M:%S %Z")) if timeout.until else "Permanently", inline=False, ) if timeout.time_left != 0: embed.add_field( name="Timeleft", value=str(utils.seconds_to_resp(timeout.time_left)), inline=False, ) if timeout.issued_by: embed.add_field(name="Banned by", value=str(timeout.issued_by), inline=False) if timeout.ban_reason: embed.add_field(name="Ban Reason", value=str(timeout.ban_reason), inline=False) if timeout.time_left != 0 and timeout.unban_reason: embed.add_field(name="Unban Reason", value=str(timeout.unban_reason), inline=False) await self.bot.say(channel=channel, embed=embed)
async def on_message(self, message): member = self.bot.discord_bot.get_member(message.author.id) not_whisper = isinstance(message.author, discord.Member) if not_whisper and (message.guild != self.bot.discord_bot.guild): return if not member: return with DBManager.create_session_scope() as db_session: User._create_or_get_by_discord_id(db_session, str(member.id), str(member)) db_session.commit() if self.new_message(db_session, message) is None: log.error("Discord api running slow?") return db_session.commit() current_timeout = Timeout._is_timedout(db_session, str(member.id)) if current_timeout and not_whisper: await message.delete() await self.bot.timeout_manager.apply_timeout( member, current_timeout) return user_level = self.bot.psudo_level_member(db_session, member) if message.author.id == self.bot.discord_bot.client.user.id: return if user_level < 500: matched_phrase = self.bot.banphrase_manager.check_message( message.content) if matched_phrase: await self.bot.banphrase_manager.punish(member, matched_phrase) await message.delete() return await HandlerManager.trigger( "parse_command_from_message", message=message, content=message.content, user_level=user_level, author=message.author, not_whisper=not_whisper, channel=message.channel, )
async def auto_untimeout(self, timeout_id, salt): if self.salt != salt: return with DBManager.create_session_scope() as db_session: timeout = Timeout._by_id(db_session, timeout_id) if not timeout.active: return member = self.bot.filters.get_member([int(timeout.user_id)], None, {})[0] if member: await self.untimeout_user(db_session, member, None, "Timeout removed by timer") return timeout.unban(db_session, None, "Timeout removed by timer") if self.settings["log_untimeout"]: # TODO pass return
async def timeout_user(self, db_session, member, banner, until, ban_reason): if not self.settings["enabled"]: return False, "Module is not enabled" current_timeout = Timeout._is_timedout(db_session, str(member.id)) new_timeout = None if current_timeout is not None: if current_timeout.check_lengths(until): current_timeout.active = False new_timeout = Timeout._create(db_session, str(member.id), str(banner.id), until, ban_reason) db_session.commit() current_timeout.unban( db_session, None, f"Timeout overwritten by Timeout #{new_timeout.id}", ) db_session.commit() else: return ( False, f"{member} is currently timedout by Timeout #{current_timeout.id}", ) if not new_timeout: new_timeout = Timeout._create(db_session, str(member.id), str(banner.id), until, ban_reason) db_session.commit() await self.apply_timeout(member, new_timeout) if self.settings["log_timeout"]: embed = discord.Embed( title="Member has been timedout", timestamp=new_timeout.created_at, colour=member.colour, ) embed.set_author( name=f"{member} ({member.id})- Timeout Removed", icon_url=str(member.avatar_url), ) embed.add_field( name="Banned on", value=str( new_timeout.created_at.strftime("%b %d %Y %H:%M:%S %Z")), inline=False, ) if new_timeout.issued_by_id: issued_by = self.bot.filters.get_member( [int(new_timeout.issued_by_id)], None, {})[0] embed.add_field( name="Banned by", value=issued_by.mention if issued_by else f"{new_timeout.issued_by_id}", inline=False, ) if new_timeout.ban_reason: embed.add_field(name="Ban Reason", value=str(new_timeout.ban_reason), inline=False) await HandlerManager.trigger("aml_custom_log", embed=embed) if new_timeout.time_left: ScheduleManager.execute_delayed( new_timeout.time_left + 5, self.auto_untimeout, args=[new_timeout.id, self.salt], ) return True, None
async def untimeout_user(self, db_session, member, unbanner, unban_reason): if not self.settings["enabled"]: return False, "Module is not enabled" current_timeout = Timeout._is_timedout(db_session, str(member.id)) if not current_timeout: return False, f"{member} is not currently timedout!" current_timeout.unban(db_session, str(unbanner.id) if unbanner else None, unban_reason) db_session.commit() role = self.bot.filters.get_role([self.settings["punished_role_id"]], None, {})[0] await self.bot.remove_role( member, role, f"Untimedout by Timeout #{current_timeout.id}") if self.settings["log_untimeout"]: embed = discord.Embed( title="Member timedout has been removed", timestamp=current_timeout.unbanned_at, colour=member.colour, ) embed.set_author( name=f"{member} ({member.id})- Timeout Removed", icon_url=str(member.avatar_url), ) embed.add_field( name="Banned on", value=str( current_timeout.created_at.strftime( "%b %d %Y %H:%M:%S %Z")), inline=False, ) embed.add_field( name="Unbanned on", value=str( current_timeout.unbanned_at.strftime( "%b %d %Y %H:%M:%S %Z")), inline=False, ) if current_timeout.issued_by_id: issued_by = self.bot.filters.get_member( [int(current_timeout.issued_by_id)], None, {})[0] embed.add_field( name="Banned by", value=issued_by.mention if issued_by else f"{current_timeout.issued_by_id}", inline=False, ) if current_timeout.ban_reason: embed.add_field(name="Ban Reason", value=str(current_timeout.ban_reason), inline=False) if current_timeout.unban_reason: embed.add_field( name="Unban Reason", value=str(current_timeout.unban_reason), inline=False, ) if current_timeout.unbanned_by_id: unbanned_by = self.bot.filters.get_member( [int(current_timeout.unbanned_by_id)], None, {})[0] embed.add_field( name="Unban By", value=unbanned_by.mention if unbanned_by else f"{current_timeout.unbanned_by_id}", inline=False, ) await HandlerManager.trigger("aml_custom_log", embed=embed) return True, None
async def query_timeouts(self, bot, author, channel, message, args): command_args = message.split(" ") if message else [] member = self.bot.filters.get_member([command_args[0]], None, {})[0] if not member: await self.bot.say(channel=channel, message=f"Cant find member, {command_args[0]}") return False with DBManager.create_session_scope() as db_session: timeouts = Timeout._by_user_id(db_session, str(member.id)) if not timeouts: await self.bot.say( channel=channel, message=f"The user {member} has no timeouts") return True for timeout in timeouts: embed = discord.Embed( description=f"Timeout #{timeout.id}", timestamp=timeout.created_at, colour=member.colour, ) embed.set_author( name=f"{member} ({member.id})", icon_url=str(member.avatar_url), ) embed.add_field( name="Banned on", value=str( timeout.created_at.strftime("%b %d %Y %H:%M:%S %Z")), inline=False, ) embed.add_field( name="Banned till" if timeout.active else "Unbanned on", value=str(timeout.until.strftime("%b %d %Y %H:%M:%S %Z")) if timeout.until else "Permanently", inline=False, ) if timeout.active: embed.add_field( name="Timeleft", value=str(utils.seconds_to_resp(timeout.time_left)), inline=False, ) if timeout.issued_by_id: issued_by = self.bot.filters.get_member( [int(timeout.issued_by_id)], None, {})[0] embed.add_field( name="Banned by", value=issued_by.mention if issued_by else f"{timeout.issued_by_id}", inline=False, ) if timeout.ban_reason: embed.add_field(name="Ban Reason", value=str(timeout.ban_reason), inline=False) if timeout.active and timeout.unban_reason: embed.add_field( name="Unban Reason", value=str(timeout.unban_reason), inline=False, ) if timeout.active and timeout.unbanned_by_id: unbanned_by = self.bot.filters.get_member( [int(timeout.unbanned_by_id)], None, {})[0] embed.add_field( name="Unban By", value=unbanned_by.mention if unbanned_by else f"{timeout.issued_by_id}", inline=False, ) await self.bot.say(channel=channel, embed=embed)