async def setup(bot: Red): if bot.get_cog("OnJoin"): LOG.warning("OnJoin already loaded, attempting to unload first...") bot.remove_cog("OnJoin") await bot.remove_loaded_package("OnJoin") bot.unload_extension("OnJoin") n = OnJoin(bot) bot.add_listener(n.voice_state_update, "on_voice_state_update") bot.add_cog(n)
async def _init(bot: Red): global _config global _bot_ref _bot_ref = bot _config = Config.get_conf(None, 1354799444, cog_name="ModLog") _config.register_global(schema_version=1) _config.register_guild(mod_log=None, casetypes={}, latest_case_number=0) _config.init_custom(_CASETYPES, 1) _config.init_custom(_CASES, 2) _config.register_custom(_CASETYPES) _config.register_custom(_CASES) await _migrate_config(from_version=await _config.schema_version(), to_version=_SCHEMA_VERSION) await register_casetypes(all_generics) async def on_member_ban(guild: discord.Guild, member: discord.Member): if not guild.me.guild_permissions.view_audit_log: return try: await get_modlog_channel(guild) except RuntimeError: return # No modlog channel so no point in continuing when = datetime.utcnow() before = when + timedelta(minutes=1) after = when - timedelta(minutes=1) await asyncio.sleep(10) # prevent small delays from causing a 5 minute delay on entry attempts = 0 # wait up to an hour to find a matching case while attempts < 12 and guild.me.guild_permissions.view_audit_log: attempts += 1 try: entry = await guild.audit_logs( action=discord.AuditLogAction.ban, before=before, after=after ).find(lambda e: e.target.id == member.id and after < e.created_at < before) except discord.Forbidden: break except discord.HTTPException: pass else: if entry: if entry.user.id != guild.me.id: # Don't create modlog entires for the bot's own bans, cogs do this. mod, reason = entry.user, entry.reason date = entry.created_at.replace(tzinfo=timezone.utc) await create_case(_bot_ref, guild, date, "ban", member, mod, reason) return await asyncio.sleep(300) async def on_member_unban(guild: discord.Guild, user: discord.User): if not guild.me.guild_permissions.view_audit_log: return try: await get_modlog_channel(guild) except RuntimeError: return # No modlog channel so no point in continuing when = datetime.utcnow() before = when + timedelta(minutes=1) after = when - timedelta(minutes=1) await asyncio.sleep(10) # prevent small delays from causing a 5 minute delay on entry attempts = 0 # wait up to an hour to find a matching case while attempts < 12 and guild.me.guild_permissions.view_audit_log: attempts += 1 try: entry = await guild.audit_logs( action=discord.AuditLogAction.unban, before=before, after=after ).find(lambda e: e.target.id == user.id and after < e.created_at < before) except discord.Forbidden: break except discord.HTTPException: pass else: if entry: if entry.user.id != guild.me.id: # Don't create modlog entires for the bot's own unbans, cogs do this. mod, reason = entry.user, entry.reason date = entry.created_at.replace(tzinfo=timezone.utc) await create_case(_bot_ref, guild, date, "unban", user, mod, reason) return await asyncio.sleep(300) bot.add_listener(on_member_ban) bot.add_listener(on_member_unban)
async def setup(bot: Red): cog = Presence(bot) bot.add_cog(cog) bot.add_listener(cog._on_member_update, "on_member_update")