def get_var(id, key): if id is None: raise ValueError("Where is this coming from?") if id not in SERVER_CONFIGS.keys(): Logging.info(f"Config entry requested before config was loaded for guild {id}, loading config for it") load_config(id) return SERVER_CONFIGS[id][key]
async def _terminate_raid(self, guild, dismised=False): guild_id = guild.id raid_info = self.under_raid[guild_id] raid_id = raid_info['ID'] raid_info["ENDED"] = str(datetime.utcfromtimestamp(time.time())) self._save_raid(raid_info) Logging.info(f"Lifted alarm in {guild}") del self.under_raid[guild_id] channel = self._get_mod_channel(guild_id) if channel is not None: total = len(raid_info['RAIDERS']) left = len(raid_info["TODO"]) handled = total - left await channel.send( f"Raid party is over :( Guess i'm done handing out special roles (for now).\n**Summary:**\nRaid ID: {raid_info['ID']}\n{total} guests showed up for the party\n{left} are still hanging out, enjoying that oh so special role they got\n{handled} are no longer with us." ) # notify other server if we didn't dismiss it, if we did they already got notified about the false alarm if not dismised: for other_guild in self.bot.guilds: if other_guild != channel.guild: new_channel = self.bot.get_channel( Configuration.get_var(other_guild.id, f"MOD_CHANNEL")) if new_channel is not None: await new_channel.send( f"Raid party over at {guild} has ended (raid ID {raid_id}. If you want to cross ban now would be a great time.\nFor more info on the raid: ``!raid_info pretty {raid_id}``\nFor crossbanning: ``!raid_act ban {raid_id}``" )
async def _terminate_raid(self, guild, dismised=False): guild_id = guild.id raid_info = self.under_raid[guild_id] raid_id = raid_info['ID'] raid_info["ENDED"] = str(datetime.utcfromtimestamp(time.time())) self._save_raid(raid_info) Logging.info(f"Lifted alarm in {guild}") del self.under_raid[guild_id] channel = self._get_mod_channel(guild_id) if channel is not None: total = len(raid_info['RAIDERS']) left = len(raid_info["TODO"]) handled = total - left await channel.send( f"No longer detecting any signs of a raid\n**Summary:**\nRaid ID: {raid_info['ID']}\n{total} raiders spotted\n{left} of them are still in the server but muted\n{handled} are no longer on this server²." ) # notify other server if we didn't dismiss it, if we did they already got notified about the false alarm if not dismised: for other_guild in self.bot.guilds: if other_guild != channel.guild: new_channel = self.bot.get_channel( Configuration.get_var(other_guild.id, f"MOD_CHANNEL")) if new_channel is not None: await new_channel.send( f"No longer detecting any signs of a raid in {guild}\nRaid ID {raid_id}. For more info on the raid: ``!raid_info pretty {raid_id}``\nFor crossbanning: ``!raid_act ban {raid_id}``" )
async def upgrade(self, ctx): await ctx.send( "<:BCWrench:344163417981976578> I'll be right back with new gears! <:woodGear:344163118089240596> <:stoneGear:344163146325295105> <:ironGear:344163170664841216> <:goldGear:344163202684289024> <:diamondGear:344163228101640192>" ) await Logging.bot_log(f"Upgrade initiated by {ctx.author.name}") Logging.info(f"Upgrade initiated by {ctx.author.name}") await ctx.invoke(self.pull) await ctx.invoke(self.restart)
async def sound_the(self, kind, guild): Logging.info(f"Anti-raid {kind} triggered for {guild.name} ({guild.id})!") channel = self.bot.get_channel(Configuration.get_var(guild.id, f"MOD_CHANNEL")) if channel is not None: await channel.send(Configuration.get_var(guild.id, f"RAID_{kind}_MESSAGE")) else: Logging.warn(f"Unable to sound the {kind} in {guild.name} ({guild.id})") await guild.owner.send( f"🚨 Anti-raid {kind} triggered for {guild.name} but the mod channel is misconfigured 🚨") if kind == "ALARM": for m in self.trackers[kind][guild.id]: await self.mute(m)
def update_config(guild, config): v = config["VERSION"] while config["VERSION"] < CONFIG_VERSION: Logging.info(f"Upgrading config version from version {v} to {v+1}") d = f"config/backups/v{v}" if not os.path.isdir(d): os.makedirs(d) Utils.save_to_disk(f"{d}/{guild}", config) config = MIGRATORS[config["VERSION"] - 1](config) config["VERSION"] += 1 Utils.save_to_disk(f"config/{guild}", config) return config
def load_config(guild): global SERVER_CONFIGS config = Utils.fetch_from_disk(f'config/{guild}') if "VERSION" not in config and len(config) < 15: Logging.info(f"The config for {guild} is to old to migrate, resetting") config = dict() else: if "VERSION" not in config: config["VERSION"] = 0 SERVER_CONFIGS[guild] = update_config(guild, config) if len(config) is 0: Logging.info(f"No config available for {guild}, creating a blank one.") SERVER_CONFIGS[guild] = Utils.fetch_from_disk("config/template") save(guild)
async def _sound_the_alarm(self, guild): Logging.info(f"Sounding the alarm for {guild} ({guild.id})!") guild_id = guild.id # apply alarm, grab id later reference raid_id = self.last_raid = self.last_raid + 1 with open("raids/counter", "w") as file: file.write(str(raid_id)) now = datetime.utcfromtimestamp(time.time()) self.under_raid[guild_id] = { "ID": raid_id, "GUILD": guild_id, "RAIDERS": {}, "MESSAGE": None, "TODO": [], "LAST_JOIN": now, "DETECTED": str(now), "ENDED": "NOT YET" } channel = self.bot.get_channel( Configuration.get_var(guild_id, f"MOD_CHANNEL")) if channel is not None: await channel.send( Configuration.get_var(guild_id, f"RAID_ALARM_MESSAGE")) else: Logging.warn( f"Unable to sound the alarm in {guild.name} ({guild_id})") await guild.owner.send( f"🚨 Anti-raid alarm triggered for {guild.name} but the mod channel is misconfigured, please use ``!status`` somewhere in that server to get the raid status 🚨" ) # deal with current raiders for raider in self.trackers[guild.id]: await self._handle_raider(raider) self.bot.loop.create_task(self._alarm_checker(guild)) # server has the tools the need to deal with it, notify other servers for other_guild in self.bot.guilds: if other_guild is not guild: channel = self.bot.get_channel( Configuration.get_var(other_guild.id, f"MOD_CHANNEL")) if channel is not None: await channel.send( f"⚠ Heads up: {guild} is being raided (raid ID: {raid_id}! They might try to raid this server as well. Spoiler alert: They'll fail" )
async def on_ready(): global STARTED if not STARTED: await Logging.onReady(bot, Configuration.get_master_var("BOT_LOG_CHANNEL")) await Configuration.on_ready(bot) for e in ["Maintenance", "Moderation", "BadNames"]: try: bot.load_extension("Cogs." + e) except Exception as ex: Logging.error(f"Failed to load cog {e}") await handle_exception(f"Loading cog {e}", ex) Logging.info("Cogs loaded") await Logging.bot_log("Outboard engine running at full speed!") STARTED = True
async def track_for(self, kind, member): guild = member.guild tracker = self.trackers[kind] if guild.id not in tracker: tracker[guild.id] = set() tracker[guild.id].add(member) amount = Configuration.get_var(guild.id, f"RAID_{kind}_AMOUNT") if len(tracker[guild.id]) >= amount: if guild.id not in self.active[kind]: self.active[kind].add(guild.id) await self.sound_the(kind, guild) if kind == "ALARM": await self.mute(member) await asyncio.sleep(Configuration.get_var(guild.id, f"RAID_{kind}_TIMEFRAME")) tracker[guild.id].remove(member) if len(tracker[guild.id]) < amount and guild.id in self.active[kind]: self.active[kind].remove(guild.id) Logging.info(f"{kind} lifted for {guild.name}") channel = self.bot.get_channel(Configuration.get_var(guild.id, f"MOD_CHANNEL")) if channel is not None: await channel.send(f"{kind} has been lifted")
async def on_ready(bot: commands.Bot): global CONFIG_VERSION CONFIG_VERSION = Utils.fetch_from_disk("config/template")["VERSION"] Logging.info(f"Current template config version: {CONFIG_VERSION}") Logging.info(f"Loading configurations for {len(bot.guilds)} guilds.") for guild in bot.guilds: Logging.info(f"Loading info for {guild.name} ({guild.id}).") load_config(guild.id)
async def on_guild_join(guild: discord.Guild): Logging.info(f"A new guild came up: {guild.name} ({guild.id}).") Configuration.load_config(guild.id)
import datetime import sys import time import traceback import discord from discord.ext import commands from Util import Logging, Configuration, Utils # Initialize logging Logging.initialize() Logging.info("Outboard initializing") bot = commands.Bot(command_prefix="!", case_insensitive=True) STARTED = False @bot.event async def on_ready(): global STARTED if not STARTED: await Logging.onReady(bot, Configuration.get_master_var("BOT_LOG_CHANNEL")) await Configuration.on_ready(bot) for e in ["Maintenance", "Moderation", "BadNames"]: try: bot.load_extension("Cogs." + e) except Exception as ex: Logging.error(f"Failed to load cog {e}")