/
about.py
111 lines (90 loc) · 2.79 KB
/
about.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import datetime
import discord
import psutil
import os
import asyncio
import aiohttp
import datetime
import core.help
from discord.ext.commands import command, Cog
import codecs
from modules.reporter import report
from core.settings import command_allowed
BOT_COLOUR = 0x19BAE5
STARTUP_TIME = datetime.datetime.now()
STATS_MESSAGE = """\
Servers: {}
Uptime: {} days {} hours {} minutes {} seconds
"""
core.help.load_from_file('./help/help.md', topics = [''])
core.help.load_from_file('./help/about.md')
core.help.load_from_file('./help/management.md')
core.help.load_from_file('./help/commands.md')
async def get_bot_total_servers(id):
async with aiohttp.ClientSession() as session:
url = 'https://discordbots.org/api/bots/{}/stats'.format(id)
async with session.get(url) as response:
jdata = await response.json()
return jdata.get('server_count')
class AboutModule(Cog):
# Send a message detailing the shard number, server count,
# uptime and and memory using of this shard
@command()
async def stats(self, context):
embed = discord.Embed(title='MathBot Stats', colour=BOT_COLOUR)
embed.add_field(
name='Total Servers',
# MathBot's ID, hard coded for proper testing.
value=await get_bot_total_servers('134073775925886976'),
inline=True
)
embed.add_field(
name='Visible Servers',
value=len(context.bot.guilds),
inline=True
)
embed.add_field(
name='Shard IDs',
value=', '.join([str(i + 1) for i in context.bot.shard_ids]),
inline=True
)
embed.add_field(
name='Uptime',
value=get_uptime(),
inline=True
)
embed.add_field(
name='Memory Usage',
value='{} MB'.format(get_memory_usage()),
inline=True
)
embed.set_footer(text='Time is in hh:mm')
await context.send(embed=embed)
@command()
async def ping(self, context):
await context.send(f'Pong! Latency {context.bot.latency}.')
# Aliases for the help command
@command()
async def about(self, context):
cmd = context.bot.get_command('help')
await context.invoke(cmd, topic='about')
@command(name=codecs.encode('shefhvg', 'rot_13'))
@command_allowed('x-bonus')
async def ignore_pls(self, context):
with open('not_an_image', 'rb') as f:
await context.send(file=discord.File(f, 'youaskedforit.png'))
await report(context.bot, ':fox:')
def get_uptime():
''' Returns a string representing how long the bot has been running for '''
cur_time = datetime.datetime.now()
up_time = cur_time - STARTUP_TIME
up_hours = up_time.seconds // (60 * 60) + (up_time.days * 24)
up_minutes = (up_time.seconds // 60) % 60
return '{:02d}:{:02d}'.format(up_hours, up_minutes)
def get_memory_usage():
''' Returns the amount of memory the bot is using, in MB '''
proc = psutil.Process(os.getpid())
mem = proc.memory_info().rss
return mem // (1024 * 1024)
def setup(bot):
bot.add_cog(AboutModule())