YusarinBot/cogs/cogUtility.py

54 lines
1.9 KiB
Python
Raw Normal View History

2023-01-17 16:50:47 +02:00
from os import getpid, makedirs, system
from shutil import rmtree
2023-04-19 15:23:41 +03:00
from discord import ApplicationContext, Cog, Guild, slash_command
2023-01-17 16:50:47 +02:00
from discord.ext import commands
2023-04-19 15:23:41 +03:00
2023-01-17 16:50:47 +02:00
from functions import appendLog, getMsg, loadJson, makeEmbed, saveJson, strToColor
2023-04-19 15:21:39 +03:00
# =========================================================================================================================
class CogUtility(commands.Cog):
2023-01-17 16:50:47 +02:00
def __init__(self, client):
self.client = client
@Cog.listener()
async def on_guild_join(self, guild: Guild):
makedirs(f"guilds/{str(guild.id)}", exist_ok=True)
makedirs(f"guilds/{str(guild.id)}/channels", exist_ok=True)
saveJson({}, f"guilds/{str(guild.id)}/config.json")
2023-04-19 15:21:39 +03:00
2023-01-17 16:50:47 +02:00
appendLog(f"Joined guild '{guild}' with id {str(guild.id)}")
@Cog.listener()
async def on_guild_remove(self, guild: Guild):
try:
rmtree(f"guilds/{str(guild.id)}")
except:
pass
2023-04-19 15:21:39 +03:00
2023-01-17 16:50:47 +02:00
appendLog(f"Left guild '{guild}' with id {str(guild.id)}")
@slash_command(name="shutdown", description="Restart the bot")
async def shutdown(self, ctx: ApplicationContext):
config = loadJson("config.json")
2023-04-19 15:21:39 +03:00
if ctx.author.id == config["owner"]:
await ctx.respond(
embed=makeEmbed(
description=getMsg("shutdown", ctx.guild).format(ctx.author),
color=strToColor(config["color_default"]),
)
)
2023-01-17 16:50:47 +02:00
system(f"kill -9 {str(getpid())}")
else:
2023-04-19 15:21:39 +03:00
await ctx.respond(
embed=makeEmbed(
title=getMsg("admin_title", ctx.guild),
description=getMsg("admin_description", ctx.guild),
color=strToColor(config["color_error"]),
)
)
# =========================================================================================================================