Tasty refactor
This commit is contained in:
46
yusarin.py
46
yusarin.py
@@ -1,28 +1,24 @@
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import shutil
|
||||
import requests # type: ignore
|
||||
import threading
|
||||
|
||||
try:
|
||||
import discord # type: ignore
|
||||
from discord import ApplicationContext, Option, Intents # type: ignore
|
||||
from requests import get
|
||||
from discord import ApplicationContext, Option, Intents, Bot, ActivityType, Activity
|
||||
except Exception as exp:
|
||||
print(f"Module py-cord is not installed. Make sure to run 'pip install -r requirements.txt' before first start")
|
||||
sys.exit()
|
||||
print(f"Dependencies not installed. Make sure to run 'pip install -r requirements.txt' before first start")
|
||||
exit()
|
||||
|
||||
from os import getpid, system
|
||||
from shutil import rmtree
|
||||
from functions import *
|
||||
pid = os.getpid()
|
||||
version = 1.7
|
||||
|
||||
pid = getpid()
|
||||
version = 1.8
|
||||
|
||||
if loadJson("config.json")["owner"] == "SET-OWNER-ID" or loadJson("config.json")["bot_token"] == "SET-BOT-TOKEN":
|
||||
print(f"Bot is not correctly configured.\nMake sure you've set up owner id and bot token in {path}/config.json\nLearn more here: https://git.end-play.xyz/profitroll/YusarinBot")
|
||||
sys.exit()
|
||||
exit()
|
||||
|
||||
if loadJson("config.json")["check_for_updates"]:
|
||||
try:
|
||||
serv_ver = json.loads(requests.get("https://api.end-play.xyz/version?app=yusarinbot&apikey=publickey").text)["version"]
|
||||
serv_ver = loads(get("https://api.end-play.xyz/version?app=yusarinbot&apikey=publickey").text)["version"]
|
||||
if float(serv_ver) > version:
|
||||
appendLog(f"YusarinBot version {serv_ver} is available. Download new version here: https://git.end-play.xyz/profitroll/YusarinBot/releases/latest")
|
||||
appendLog(f"Currently using YusarinBot v{str(version)}")
|
||||
@@ -30,7 +26,7 @@ if loadJson("config.json")["check_for_updates"]:
|
||||
appendLog(f"Could not get YusarinBot cloud version due to {exp}. Currently using {str(version)}")
|
||||
|
||||
intents = Intents().all()
|
||||
client = discord.Bot(intents=intents)
|
||||
client = Bot(intents=intents)
|
||||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
@@ -39,7 +35,7 @@ async def on_ready():
|
||||
|
||||
config = loadJson("config.json")
|
||||
|
||||
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=config["bot_activity"]))
|
||||
await client.change_presence(activity=Activity(type=ActivityType.listening, name=config["bot_activity"]))
|
||||
|
||||
await clearTrash(client)
|
||||
|
||||
@@ -48,8 +44,8 @@ async def on_guild_join(guild):
|
||||
|
||||
global path
|
||||
|
||||
os.mkdir(f"{path}/guilds/{str(guild.id)}")
|
||||
os.mkdir(f"{path}/guilds/{str(guild.id)}/channels")
|
||||
makedirs(f"{path}/guilds/{str(guild.id)}", exist_ok=True)
|
||||
makedirs(f"{path}/guilds/{str(guild.id)}/channels", exist_ok=True)
|
||||
saveJson({}, f"{path}/guilds/{str(guild.id)}/config.json")
|
||||
|
||||
appendLog(f"Joined guild '{guild}' with id {str(guild.id)}")
|
||||
@@ -60,7 +56,7 @@ async def on_guild_remove(guild):
|
||||
global path
|
||||
|
||||
try:
|
||||
shutil.rmtree(f"{path}/guilds/{str(guild.id)}")
|
||||
rmtree(f"{path}/guilds/{str(guild.id)}")
|
||||
except:
|
||||
pass
|
||||
|
||||
@@ -99,7 +95,7 @@ async def on_voice_state_update(member, before, after):
|
||||
await changeNomicPerms("allow", vc_to, member)
|
||||
if vc_to.id == guildConfGet(vc_to.guild, "channel"):
|
||||
if guildConfGet(vc_to.guild, "category") is not None:
|
||||
voice_chan = await createUserVoice(vc_to, discord.utils.get(vc_to.guild.categories, id=guildConfGet(vc_to.guild, "category")), member)
|
||||
voice_chan = await createUserVoice(vc_to, utils.get(vc_to.guild.categories, id=guildConfGet(vc_to.guild, "category")), member)
|
||||
try:
|
||||
await member.move_to(voice_chan)
|
||||
except:
|
||||
@@ -116,7 +112,7 @@ async def shutdown(ctx: ApplicationContext):
|
||||
config = loadJson("config.json")
|
||||
if ctx.author.id == config["owner"]:
|
||||
await ctx.respond(embed=makeEmbed(description=getMsg("shutdown", ctx.guild).format(ctx.author), color=strToColor(config["color_default"])))
|
||||
os.system(f"kill -9 {str(pid)}")
|
||||
system(f"kill -9 {str(pid)}")
|
||||
else:
|
||||
await ctx.respond(embed=makeEmbed(title=getMsg("admin_title", ctx.guild), description=getMsg("admin_description", ctx.guild), color=strToColor(config["color_error"])))
|
||||
#=========================================================================================================================
|
||||
@@ -131,7 +127,7 @@ async def help(ctx: ApplicationContext):
|
||||
locale = client.create_group("locale", "Commands related to bot's locale")
|
||||
|
||||
valid_locales = []
|
||||
files_locales = os.listdir(f"{path}/locale/")
|
||||
files_locales = listdir(f"{path}/locale/")
|
||||
for entry in files_locales:
|
||||
valid_locales.append(".".join(entry.split(".")[:-1]))
|
||||
|
||||
@@ -139,13 +135,13 @@ for entry in files_locales:
|
||||
async def locale_set(ctx: ApplicationContext, language: Option(str, "One of the languages in list", choices=valid_locales)): # type: ignore
|
||||
config = loadJson("config.json")
|
||||
if ctx.guild is not None:
|
||||
if language+".json" in os.listdir(f"{path}/locale/"):
|
||||
if language+".json" in listdir(f"{path}/locale/"):
|
||||
guildConfSet(ctx.guild, "locale", language)
|
||||
appendLog(f"Server's locale is now set to {language}", ctx.guild)
|
||||
await ctx.respond(embed=makeEmbed(title=getMsg("set_locale_title", ctx.guild), description=getMsg("set_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_ok"])))
|
||||
else:
|
||||
valid_locales = []
|
||||
files_locales = os.listdir(f"{path}/locale/")
|
||||
files_locales = listdir(f"{path}/locale/")
|
||||
for entry in files_locales:
|
||||
valid_locales.append(entry.split(".")[:-1])
|
||||
await ctx.respond(embed=makeEmbed(title=getMsg("error_locale_title", ctx.guild), description=getMsg("error_locale_description", ctx.guild).format(", ".join(valid_locales)), color=strToColor(config["color_error"])))
|
||||
|
Reference in New Issue
Block a user