async def on_ready(self): log.info("Logged in as {}, with ID {}".format(self.user.name, self.user.id)) await self.change_presence(activity=discord.Activity( name="for bruh moments", type=discord.ActivityType.watching)) self.servers = {} try: with open("servers.json", "r") as f: self.servers = json.load(f) except FileNotFoundError as e: log.exception("No server file yet") with open("servers.json", "w+") as f: json.dump(self.servers, f) self.intervals = {} for id, value in self.servers.items(): if value["disappearing"]: self.intervals[id] = Set.Interval(value["wipe_time"], self.wipe, id=value["wipe_channel"])
def __init__(self) -> None: """Initializes the class.""" self.callThreshold = int(os.getenv("CALL_THRESHOLD", 1)) self.debug = os.getenv("DEBUG", "true").lower() == "true" self.reportLatency = os.getenv("REPORT_LATENCY", "false").lower() == "true" self.window_minutes = int(os.getenv("WINDOW_M", 5)) self.timezone = pytz.timezone(os.getenv("TIMEZONE", "US/Pacific")) # The actual look back is the length of this lookback + lag compensation. For example: 300+45=345 seconds self.lookback = os.getenv("LOOKBACK_S", 300) self.cachedTweet: int = None self.cachedTime: datetime = None self.cache = TTLCache(maxsize=100, ttl=self.lookback) self.scraper = Scraper.Instance(self.BASE_URL, self.lookback) self.latency = [timedelta(seconds=0)] if not self.debug: # Does not need to be saved for later. # If the keys aren't in env this will still run. auth = tweepy.OAuthHandler(os.getenv("CONSUMER_KEY", ""), os.getenv("CONSUMER_SECRET", "")) auth.set_access_token(os.getenv("ACCESS_TOKEN_KEY", ""), os.getenv("ACCESS_TOKEN_SECRET", "")) self.api = tweepy.API(auth) # Test the authentication. This will gracefully fail if the keys aren't present. try: self.api.rate_limit_status() except TweepError as e: if e.api_code == 215: log.error("No keys or bad keys") else: log.error("Other API error: {}".format(e)) exit(1) self.interval = Set.Interval(30, self._check)
async def on_message(self, message): log.info("Message event dispatched") key_name = await self.get_key(message.guild) if message.author.id == self.user.id: return if (message.author.guild_permissions.administrator or message.author.id == keys["admin_id"]): if message.content.startswith("!set"): if key_name is not None: if (self.servers[key_name]["channel"] is None or self.servers[key_name]["channel"] != message.channel.id): self.servers[key_name]["channel"] = message.channel.id await message.channel.send("Set channel to %s!" % message.channel.mention) try: log.info( "Dumping channel: %s, in Guild: %s" % (message.channel.name, message.guild.name)) with open("servers.json", "w") as f: json.dump(self.servers, f, indent=4) except IOError as e: log.error("Servers.json went missing, yikes") exit() elif message.content.startswith("!test"): log.info("Sending test message") await self.on_member_remove(message.author) elif message.content.startswith("!deltoggle"): self.servers[key_name]["delete_message"] = not self.servers[ key_name]["delete_message"] try: log.info("Dumping delete msg in Guild: %s" % (message.guild.name)) with open("servers.json", "w") as f: json.dump(self.servers, f, indent=4) except IOError as e: log.error("Servers.json went missing, yikes") exit() await message.channel.send( "Turned delete message on." if self.servers[key_name] ["delete_message"] else "Turned delete message off.") elif message.content.startswith("!disappearing"): await message.channel.send( "Turning channel wipes on." if not self.servers[key_name] ["disappearing"] else "Turning channel wipes off.") if self.servers[key_name]["disappearing"]: self.servers[key_name]["disappearing"] = False self.intervals[key_name].cancel() else: self.servers[key_name]["disappearing"] = True self.servers[key_name]["wipe_channel"] = message.channel.id time = re.match(r"(?:\d*\.){0,1}\d+", message.content) if not time == None: time = float(time) else: log.warning("No time provided") await message.channel.send( "No time provided, defaulting to 12 hours") time = 0.5 # * hours * minutes * seconds time *= 24 * 60 * 60 self.servers[key_name]["wipe_time"] = time self.intervals[key_name] = Set.Interval( time, self.wipe, message.channel.id) try: log.info("Dumping disappearing messages") with open("servers.json", "w") as f: json.dump(self.servers, f, indent=4) except IOError as e: log.error("Servers.json went missing, yikes") exit() elif message.content.startswith("!changeWipe"): if self.servers[key_name]["disappearing"]: time = re.match(r"(?:\d*\.){0,1}\d+", message.content) if not time == None: time = float(time) else: log.warning("No time provided") await message.channel.send( "No time provided, defaulting to 12 hours") time = 0.5 # * hours * minutes * seconds time *= 24 * 60 * 60 self.servers[key_name]["wipe_time"] = time self.intervals[key_name].cancel() self.intervals[key_name] = Set.Interval( time, self.wipe, message.channel.id) try: log.info("Dumping disappearing messages") with open("servers.json", "w") as f: json.dump(self.servers, f, indent=4) except IOError as e: log.error("Servers.json went missing, yikes") exit() if message.content.startswith("!bruh"): await message.channel.send("%s bruh" % message.author.mention) elif message.content.startswith("!bhelp"): await message.channel.send("""Hi I'm BruhBot! My commands are: ```!bhelp: sends this message. !bruh: sends bruh back. !set*: sets the channel to send leave messages to. !deltoggle*: toggles message delete messages. !test*: sends a test message.``` * admin only commands""") else: return