Passed
Branch main (f64b1b)
by Bartosz
01:11
created

build.cogs.utilscog.UtilsCog.reload_all_cogs()   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nop 2
1
import json
2
import math
3
import os
4
from datetime import datetime
5
import time
6
import discord
7
import psutil
8
from discord.utils import get
9
from modules.get_settings import get_settings
10
import cogs.cogbase as cogbase
11
from discord.ext import commands
12
from discord_slash import cog_ext, SlashContext
13
from cogs.databasecog import DatabaseCog
14
from modules.pull_config.pull_config import get_config
15
16
17
class UtilsCog(cogbase.BaseCog):
18
    def __init__(self, base):
19
        super().__init__(base)
20
21
    # OTHER
22
    # Pull config.json from Google Sheets
23
    @cog_ext.cog_slash(name="pullConfig", guild_ids=cogbase.GUILD_IDS,
24
                       description="Pull config from google sheets",
25
                       default_permission=False,
26
                       permissions=cogbase.PERMISSION_ADMINS)
27
    async def pull_config(self, ctx: SlashContext):
28
        get_config()
29
        with open('server_files/config.json', 'r', encoding='utf-8-sig') as fp:
30
            self.bot.config = json.load(fp)
31
            await self.create_roles(ctx, True)
32
            await self.create_roles(ctx, False)
33
            dt_string = self.bot.get_current_time()
34
            print(f"({dt_string})\t[{self.__class__.__name__}]: Finished data pull")
35
        await ctx.send(f"Config.json updated", hidden=True)
36
37
    # Create roles if pull_config gets non existent roles
38
    async def create_roles(self, ctx: SlashContext, common: bool):
39
        milestones = "common_milestones" if common else "total_milestones"
40
        for mon_type in self.bot.config[milestones][0]:
41
            if get(ctx.guild.roles, name=mon_type):
42
                continue
43
            else:
44
                await ctx.guild.create_role(name=mon_type)
45
                dt_string = self.bot.get_current_time()
46
                print(f"({dt_string})\t[{self.__class__.__name__}]: {mon_type} role created")
47
48
    # Clear temp spots table in database
49
    @cog_ext.cog_slash(name="clearTempSpots", guild_ids=cogbase.GUILD_IDS,
50
                       description="Clear temp spots table in database",
51
                       permissions=cogbase.PERMISSION_ADMINS)
52
    async def clear_temp_spots_table(self, ctx):
53
        await DatabaseCog.db_clear_spots_temp_table()
54
        await ctx.send(f"Temp spots table was cleared", hidden=True)
55
        await self.reload_cog(ctx, "cogs.databasecog")
56
57
    # Reloads cog, very useful because there is no need to exit the bot after updating cog
58
    async def reload_cog(self, ctx: SlashContext, module: str):
59
        """Reloads a module."""
60
        dt_string = self.bot.get_current_time()
61
        try:
62
            self.bot.load_extension(f"{module}")
63
            await ctx.send(f'[{module}] loaded', hidden=True)
64
            print(f'({dt_string})\t[{self.__class__.__name__}]: {module} loaded')
65
        except commands.ExtensionAlreadyLoaded:
66
            self.bot.unload_extension(module)
67
            self.bot.load_extension(module)
68
            await ctx.send(f'[{module}] reloaded', hidden=True)
69
            print(f'({dt_string})\t[{self.__class__.__name__}]: {module} reloaded')
70
        except commands.ExtensionNotFound:
71
            await ctx.send(f'[{module}] not found', hidden=True)
72
            print(f'({dt_string})\t[{self.__class__.__name__}]: {module} not found')
73
74
    # Command for reloading specific cog
75
    @cog_ext.cog_slash(name="reloadCog", guild_ids=cogbase.GUILD_IDS,
76
                       description="Reload cog",
77
                       permissions=cogbase.PERMISSION_ADMINS)
78
    async def reload_cog_command(self, ctx: SlashContext, module: str):
79
        await self.reload_cog(ctx, module)
80
81
    # Command for reloading all cogs
82
    @cog_ext.cog_slash(name="reloadAllCogs", guild_ids=cogbase.GUILD_IDS,
83
                       description="Reload cog",
84
                       permissions=cogbase.PERMISSION_ADMINS)
85
    async def reload_all_cogs(self, ctx: SlashContext = None):
86
        for cog in list(self.bot.extensions.keys()):
87
            await self.reload_cog(ctx, cog)
88
        await ctx.send(f'All cogs reloaded', hidden=True)
89
90
    @cog_ext.cog_slash(name="saveCoordinates", guild_ids=cogbase.GUILD_IDS,
91
                       description="Get your spot stats",
92
                       permissions=cogbase.PERMISSION_ADMINS)
93
    async def save_coordinates(self, ctx: SlashContext):
94
        coords_df = await DatabaseCog.db_get_coords()
95
        coords_df[['latitude', 'longitude']] = coords_df['coords'].str.split(',', expand=True)
96
        coords_df.to_excel(r'server_files/coords.xlsx', index=False)
97
        await ctx.send(f"Coords saved", hidden=True)
98
        dt_string = self.bot.get_current_time()
99
        print(f'({dt_string})\t[{self.__class__.__name__}]: Coords saved to server_files/coords.xlsx')
100
101
    # Get member info
102
    @cog_ext.cog_slash(name="memberinfo", guild_ids=cogbase.GUILD_IDS,
103
                       description="Get member info",
104
                       permissions=cogbase.PERMISSION_ADMINS)
105
    async def memberinfo(self, ctx: SlashContext, *, user: discord.Member = None):
106
        if user is None:
107
            user = ctx.author
108
        date_format = "%a, %d %b %Y %I:%M %p"
109
        embed = discord.Embed(color=0xFF0000, description=user.mention)
110
        embed.set_author(name=str(user), icon_url=user.avatar_url)
111
        embed.set_thumbnail(url=user.avatar_url)
112
        embed.add_field(name="Joined Server", value=user.joined_at.strftime(date_format), inline=False)
113
        members = sorted(ctx.guild.members, key=lambda m: m.joined_at)
114
        embed.add_field(name="Join Position", value=str(members.index(user) + 1), inline=False)
115
        embed.add_field(name="Joined Discord", value=user.created_at.strftime(date_format), inline=False)
116
        if len(user.roles) > 1:
117
            role_string = ' '.join([r.mention for r in user.roles][1:])
118
            embed.add_field(name="Roles [{}]".format(len(user.roles) - 1), value=role_string, inline=False)
119
        embed.set_footer(text='ID: ' + str(user.id))
120
        return await ctx.send(embed=embed)
121
122
    # Backup database to a file
123
    @cog_ext.cog_slash(name="backupDatabase", guild_ids=cogbase.GUILD_IDS,
124
                       description="Backup database to a file",
125
                       permissions=cogbase.PERMISSION_MODS)
126
    async def backup_database(self, ctx: SlashContext):
127
        now = datetime.now()
128
        cmd = f"mysqldump -u {get_settings('DB_U')} " \
129
              f"--result-file=database_backup/backup-{now.strftime('%m-%d-%Y')}.sql " \
130
              f"-p{get_settings('DB_P')} server_database"
131
        os.system(cmd)
132
        await ctx.send(f"Database backed up", hidden=True)
133
134
    # System stats
135
    @cog_ext.cog_slash(name="systemStatus", guild_ids=cogbase.GUILD_IDS,
136
                       description="Get status of the system",
137
                       permissions=cogbase.PERMISSION_MODS)
138
    async def system(self, ctx):
139
        """Get status of the system."""
140
        process_uptime = time.time() - self.bot.start_time
141
        process_uptime = time.strftime("%ed %Hh %Mm %Ss", time.gmtime(process_uptime))
142
        system_uptime = time.time() - psutil.boot_time()
143
        system_uptime = time.strftime("%ed %Hh %Mm %Ss", time.gmtime(system_uptime))
144
        mem = psutil.virtual_memory()
145
        pid = os.getpid()
146
        memory_use = psutil.Process(pid).memory_info()[0]
147
148
        data = [
149
            ("Version", self.bot.version),
150
            ("Process uptime", process_uptime),
151
            ("Process memory", f"{memory_use / math.pow(1024, 2):.2f}MB"),
152
            ("System uptime", system_uptime),
153
            ("CPU Usage", f"{psutil.cpu_percent()}%"),
154
            ("RAM Usage", f"{mem.percent}%"),
155
        ]
156
157
        content = discord.Embed(
158
            title=":computer: System status",
159
            colour=int("5dadec", 16),
160
            description="\n".join(f"**{x[0]}** {x[1]}" for x in data),
161
        )
162
        await ctx.send(embed=content)
163
164
165
def setup(bot: commands.Bot):
166
    bot.add_cog(UtilsCog(bot))
167