From c796210a986a817990e763bd8c90438e7b0d707b Mon Sep 17 00:00:00 2001 From: James Gayfer Date: Sun, 24 Sep 2017 21:36:27 -0700 Subject: [PATCH] add about command --- cogs/general.py | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ cogs/owner.py | 22 ----------------- requirements.txt | 1 + spirit.py | 6 +++++ 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/cogs/general.py b/cogs/general.py index f821fdd..a66b3f7 100644 --- a/cogs/general.py +++ b/cogs/general.py @@ -3,6 +3,7 @@ import discord from discord.ext import commands import pytz +import psutil from cogs.utils.messages import MessageManager from cogs.utils import constants @@ -12,6 +13,7 @@ class General: def __init__(self, bot): self.bot = bot + self.process = psutil.Process() @commands.command() @@ -53,6 +55,67 @@ async def feedback_error(self, ctx, error): await manager.clear() + @commands.command() + async def about(self, ctx): + """Display information about the bot itself""" + embed = discord.Embed(colour=constants.BLUE) + #embed.url = 'https://discordapp.com/oauth2/authorize?client_id=335084645743984641&scope=bot&permissions=523344' + + embed.description = ("[Invite Spirit](https://discordapp.com/oauth2/authorize?client_id=335084645743984641&scope=bot&permissions=523344)\n" + + "[Spirit Support Server](https://discord.gg/GXCFpkr)") + + owner = self.bot.get_user(118926942404608003) + embed.set_author(name=str(owner), icon_url=owner.avatar_url) + + # statistics + total_members = sum(1 for _ in self.bot.get_all_members()) + total_online = len({m.id for m in self.bot.get_all_members() if m.status is discord.Status.online}) + total_unique = len(self.bot.users) + + voice_channels = [] + text_channels = [] + for guild in self.bot.guilds: + voice_channels.extend(guild.voice_channels) + text_channels.extend(guild.text_channels) + + text = len(text_channels) + voice = len(voice_channels) + + embed.add_field(name='Members', value='{} total\n{} unique\n{} unique online'.format(total_members, total_unique, total_online)) + embed.add_field(name='Channels', value='{} total\n{} text\n{} voice'.format(text + voice, text, voice)) + + memory_usage = "%0.2f" % (self.process.memory_full_info().uss / 1024**2) + cpu_usage = "%0.2f" % (self.process.cpu_percent() / psutil.cpu_count()) + embed.add_field(name='Process', value='{} MiB\n{}% CPU'.format(memory_usage, cpu_usage)) + + embed.add_field(name='Guilds', value=len(self.bot.guilds)) + embed.add_field(name='Commands Run', value=self.bot.command_count) + embed.add_field(name='Uptime', value=self.get_bot_uptime(brief=True)) + + embed.set_footer(text='Made with discord.py', icon_url='http://i.imgur.com/5BFecvA.png') + await ctx.send(embed=embed) + + + def get_bot_uptime(self, *, brief=False): + now = 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 not brief: + if days: + fmt = '{d} days, {h} hours, {m} minutes, and {s} seconds' + else: + fmt = '{h} hours, {m} minutes, and {s} seconds' + else: + fmt = '{h}h {m}m {s}s' + if days: + fmt = '{d}d ' + fmt + + return fmt.format(d=days, h=hours, m=minutes, s=seconds) + + async def on_guild_join(self, guild): """Send welcome message to the server owner""" message = ("Greetings! My name is **{}**, and my sole responsibility is to help you and " diff --git a/cogs/owner.py b/cogs/owner.py index 079258e..c5f7016 100644 --- a/cogs/owner.py +++ b/cogs/owner.py @@ -70,25 +70,3 @@ async def broadcast_error(self, ctx, error): manager = MessageManager(self.bot, ctx.author, ctx.channel, ctx.prefix, [ctx.message]) await manager.say("You didn't include a broadcast message") return await manager.clear() - - - @commands.command(hidden=True) - async def botstats(self, ctx): - """Displays the bot's stats""" - manager = MessageManager(self.bot, ctx.author, ctx.channel, ctx.prefix, [ctx.message]) - - if ctx.author.id not in constants.OWNERS: - return - - num_guilds = len(self.bot.guilds) - users = [] - for guild in self.bot.guilds: - if guild.id not in constants.SERVERS_NO_COUNT: - guild_users = [user for user in guild.members if not user.bot] - users.extend(guild_users) - num_users = len(set(users)) - - e = discord.Embed(title='{} Stats'.format(self.bot.user.name), colour=constants.BLUE) - e.description = "**Servers**: {}\n**Users**: {}".format(num_guilds, num_users) - await ctx.channel.send(embed=e) - await manager.clear() diff --git a/requirements.txt b/requirements.txt index fc95717..d5d6ee8 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pymysql==0.7.11 pytz==2017.2 +psutil==5.3.1 diff --git a/spirit.py b/spirit.py index 929c71d..6c28081 100644 --- a/spirit.py +++ b/spirit.py @@ -1,6 +1,7 @@ import json import logging import asyncio +import datetime import discord from discord.ext import commands @@ -41,10 +42,15 @@ def __init__(self, token): super().__init__(command_prefix=_prefix_callable) self.token = token self.db = DBase('credentials.json') + self.uptime = datetime.datetime.utcnow() + self.command_count = 0 def run(self): super().run(token, reconnect=True) + async def on_command(self, ctx): + self.command_count += 1 + if __name__ == '__main__':