From 6983cf8d6bb7eb7000a0ac19b5dcacad288ee321 Mon Sep 17 00:00:00 2001 From: Phxntxm Date: Sat, 30 Jul 2016 09:59:35 -0500 Subject: [PATCH] Added an uptime command to the bot --- bot.py | 10 ++++++++++ cogs/core.py | 22 +++++++++++++++++++--- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/bot.py b/bot.py index ca4bd46..e845748 100644 --- a/bot.py +++ b/bot.py @@ -2,6 +2,8 @@ import discord import traceback +import logging +import datetime from discord.ext import commands from cogs.utils import config @@ -17,7 +19,13 @@ extensions = ['cogs.interaction', 'cogs.tags'] bot = commands.Bot(command_prefix=config.commandPrefix, description=config.botDescription, pm_help=None) +discord_logger = logging.getLogger('discord') +discord_logger.setLevel(logging.WARNING) +log = logging.getLogger() +log.setLevel(logging.INFO) +handler = logging.FileHandler(filename='bonfire.log', encoding='utf-8', mode='a') +log.addHandler(handler) # Bot event overrides @bot.event @@ -29,6 +37,8 @@ async def on_ready(): destination = discord.utils.find(lambda m: m.id == channel_id, bot.get_all_channels()) await bot.send_message(destination, "I have just finished restarting!") config.saveContent('restart_server', 0) + if not hasattr(bot, 'uptime'): + bot.uptime = datetime.datetime.utcnow() @bot.event diff --git a/cogs/core.py b/cogs/core.py index f799a14..154df47 100644 --- a/cogs/core.py +++ b/cogs/core.py @@ -16,9 +16,19 @@ class Core: def __init__(self, bot): self.bot = bot - @commands.command() - async def test(self): - await self.bot.say((await self.bot.application_info()).id) + + def get_bot_uptime(self): + now = datetime.datetime.utcnow() + delta = now - self.bot.uptime + hours, remainder = divmod(int(delta.total_seconds()), 3600) + minutes, seconds = divmod(remainder, 60) + days, hours = divmod(hours, 24) + if days: + fmt = '{d} days, {h} hours, {m} minutes, and {s} seconds' + else: + fmt = '{h} hours, {m} minutes, and {s} seconds' + + return fmt.format(d=days, h=hours, m=minutes, s=seconds) @commands.command() @checks.customPermsOrRole(send_messages=True) @@ -50,6 +60,12 @@ class Core: year = datetime.datetime.today().year cal = calendar.TextCalendar().formatmonth(year, month) await self.bot.say("```{}```".format(cal)) + + @commands.command() + @checks.customPermsOrRole(send_messages=True) + async def uptime(self): + """Provides a printout of the current bot's uptime""" + awayt self.bot.say("Uptime = ```{}```".format(get_bot_uptime())) @commands.command() @checks.customPermsOrRole(send_messages=True)