From b44210e922dfd089b4e164bc11ae07b51455e859 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Wed, 12 Dec 2018 15:35:57 -0600 Subject: [PATCH] Bot Changes Decommissioned OreAlert since it was implemented in https://git.etztech.xyz/Etzelia/MinecraftManagerPlugin/commit/f2f2bc32335d2b6594dce16f914795e512ccada7 Renamed the MCM Discord bot to clarify --- api/bot.py | 116 ------------------ .../{Discord.bot.py => Discord-MCM.bot.py} | 0 assets/bots/OreAlert.bot.py | 16 --- 3 files changed, 132 deletions(-) rename assets/bots/{Discord.bot.py => Discord-MCM.bot.py} (100%) delete mode 100644 assets/bots/OreAlert.bot.py diff --git a/api/bot.py b/api/bot.py index 019e9f4..4ba497b 100644 --- a/api/bot.py +++ b/api/bot.py @@ -235,7 +235,6 @@ class Discord(discord.Client): header = "**The following users do not have an application or player match on the whitelist:**\n" yield from self.discord_message(message.author, "{}```{}```".format(header, "\n".join(no_application))) - def build_info(self, application): embed = discord.Embed(colour=discord.Colour(0x417505)) embed.set_thumbnail( @@ -266,123 +265,8 @@ class Discord(discord.Client): except: pass - def run_bot(self): self.run(self.token) -class OreAlert: - # Options - log = os.path.join(settings.MINECRAFT_BASE_DIR, 'logs/latest.log') - purge = 30 # How long until we purge, in minutes - rotate = 5 # How long without input before we assume the log has rotated - notify_start = 5 # How many veins found within the above purge minutes to notify - notify_each = 1 # After the initial alert, how many should be found in addition before more alerts? - notify_ping = 5 # After the initial alert, how many should be found in addition before more pings? - - playerList = [] - - class Player: - - def __init__(self, name, time, prev=[]): - self.name = name - self.time = time - self.prev = prev - - def compare(self, player): - if self.name == player.name: - return True - else: - return False - - def to_string(self): - return str(self.name) + ": " + str(self.time) + ", previous: " + self.prev_to_string() - - def prev_to_string(self): - ret = "" - if len(self.prev) > 0: - for p in self.prev: - ret += str(p) + " " - - return ret.rstrip() - else: - return ret - - def minute_interval(self, start, end): - reverse = False - if start > end: - start, end = end, start - reverse = True - delta = (end.hour - start.hour) * 60 + end.minute - start.minute + (end.second - start.second) / 60.0 - if reverse: - delta = 24 * 60 - delta - return delta - - def follow(self, filename): - thefile = open(filename, 'r', encoding='utf-8') - thefile.seek(0, 2) # Go to the end of the file - start = datetime.datetime.now() - end = datetime.datetime.now() - api.discord_notification('OreAlert has started successfully.') - while True: - line = thefile.readline() - if not line: - if self.minute_interval(start, end) > self.rotate: - thefile.close() - time.sleep(5) - thefile = open(filename, 'r', encoding='utf-8') - thefile.seek(0, 2) # Go to the end of the file - start = datetime.datetime.now() - end = datetime.datetime.now() - # api.discord_notification('OreAlert has closed and re-opened the log...hopefully it just rotated.') - continue - end = end + datetime.timedelta(milliseconds=100) - time.sleep(0.1) # Sleep briefly - continue - start = datetime.datetime.now() - end = datetime.datetime.now() - yield line - - def run_bot(self): - cur_line = "" - try: - loglines = self.follow(self.log) - for line in loglines: - # [00: 03:46] [Server thread / INFO]: [MinecraftManager]: [OreAlert]: Etzelia - if "MinecraftManager" in line and "OreAlert" in line: # Filter out non-OreAlert log statements - cur_time = line.split()[0].replace("[", "").replace("]", "") - if ":" in cur_time: # Make sure we have a time. - time_array = cur_time.split(":") - name = line.split()[-1] - dt = datetime.time(int(time_array[0]), int(time_array[1]), int(time_array[2])) - p = self.Player(name, dt) - new_player = True - for player in self.playerList: - if p.compare(player): - new_player = False - player.prev[:] = [x for x in player.prev if not self.minute_interval(x, - dt) > self.purge] # First, purge any times older than our configured amount - player.prev.append(dt) # Add the new time - if len(player.prev) >= self.notify_start: - # MCM Alert - if len(player.prev) == self.notify_start: - api.create_alert("OreAlert: {0}".format(player.name)) - if len(player.prev) % self.notify_each == 0: - # In-game Notification - api.plugin(api.PLUGIN_STAFF_CHAT, - "OreAlert {0} has found {1} diamond ore veins within {2} minutes.".format( - player.name, len(player.prev), self.purge)) - # Discord Notification - ping = True if len(player.prev) % self.notify_ping == 0 else False - api.discord_notification( - '{0} has found {1} diamond ore veins within {2} minutes.'.format( - player.name.replace("_", "\\_"), len(player.prev), self.purge), ping=ping) - if new_player: - p.prev = [p.time] - self.playerList.append(p) - except KeyboardInterrupt: - api.discord_notification("OreAlert has been stopped manually.") - except: - api.discord_notification("OreAlert has crashed!", ping=True) - diff --git a/assets/bots/Discord.bot.py b/assets/bots/Discord-MCM.bot.py similarity index 100% rename from assets/bots/Discord.bot.py rename to assets/bots/Discord-MCM.bot.py diff --git a/assets/bots/OreAlert.bot.py b/assets/bots/OreAlert.bot.py deleted file mode 100644 index 16f6617..0000000 --- a/assets/bots/OreAlert.bot.py +++ /dev/null @@ -1,16 +0,0 @@ -import os, sys, django - -sep = os.sep -path = os.path.dirname(os.path.abspath(__file__)) -path = path.split(sep)[:-3] -project = path[-1] -path = sep.join(path) -sys.path.append(path) -print("Setting path for {0}: {1}".format(project, path)) -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "{}.settings".format(project)) -django.setup() - -from minecraft_manager.api.bot import OreAlert - -bot = OreAlert() -bot.run_bot()