YusarinBot/yusarin.py

224 lines
12 KiB
Python
Raw Normal View History

2022-02-07 03:06:01 +02:00
import os
import sys
import json
2022-02-08 23:37:24 +02:00
import shutil
2022-02-07 03:06:01 +02:00
import requests
import threading
2022-02-06 02:58:55 +02:00
try:
2022-05-09 00:09:19 +03:00
import discord # type: ignore
from discord import ApplicationContext, Option, Intents # type: ignore
2022-02-06 02:58:55 +02:00
except Exception as exp:
2022-05-09 00:09:19 +03:00
print(f"Module py-cord is not installed. Make sure to run 'pip install -r requirements.txt' before first start")
2022-02-06 02:58:55 +02:00
sys.exit()
2022-02-05 02:31:32 +02:00
from functions import *
pid = os.getpid()
2022-07-15 23:28:01 +03:00
version = 1.6
2022-02-14 19:07:31 +02:00
if loadJson("config.json")["owner"] == "SET-OWNER-ID" or loadJson("config.json")["bot_token"] == "SET-BOT-TOKEN":
2022-07-15 23:28:01 +03:00
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")
2022-02-14 19:07:31 +02:00
sys.exit()
2022-02-07 03:06:01 +02:00
if loadJson("config.json")["check_for_updates"]:
try:
2022-07-22 10:54:57 +03:00
serv_ver = json.loads(requests.get("https://api.end-play.xyz/version?app=yusarinbot&apikey=publickey").text)["version"]
2022-02-07 03:06:01 +02:00
if float(serv_ver) > version:
2022-07-15 23:28:01 +03:00
appendLog(f"YusarinBot version {serv_ver} is available. Download new version here: https://git.end-play.xyz/profitroll/YusarinBot/releases/latest")
2022-02-07 03:06:01 +02:00
appendLog(f"Currently using YusarinBot v{str(version)}")
except Exception as exp:
appendLog(f"Could not get YusarinBot cloud version due to {exp}. Currently using {str(version)}")
2022-02-05 02:31:32 +02:00
2022-05-09 00:09:19 +03:00
intents = Intents().all()
client = discord.Bot(intents=intents)
2022-02-05 02:31:32 +02:00
@client.event
async def on_ready():
2022-02-07 03:06:01 +02:00
appendLog(f"Logged in as {client.user}")
2022-02-05 02:31:32 +02:00
config = loadJson("config.json")
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=config["bot_activity"]))
2022-02-07 03:06:01 +02:00
await clearTrash(client)
2022-02-05 02:31:32 +02:00
2022-02-08 23:37:24 +02:00
@client.event
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")
saveJson({}, f"{path}/guilds/{str(guild.id)}/config.json")
appendLog(f"Joined guild '{guild}' with id {str(guild.id)}")
@client.event
async def on_guild_remove(guild):
global path
try:
shutil.rmtree(f"{path}/guilds/{str(guild.id)}")
except:
pass
appendLog(f"Left guild '{guild}' with id {str(guild.id)}")
2022-02-05 02:31:32 +02:00
@client.event
async def on_voice_state_update(member, before, after):
2022-02-08 23:37:24 +02:00
global debug
2022-02-05 02:31:32 +02:00
config = loadJson("config.json")
vc_from = before.channel
vc_to = after.channel
# If user left vc
if vc_to is None:
if isUserVoice(vc_from):
if isVoiceOfUser(vc_from, member):
await removeUserVoice(vc_from)
return
else:
2022-08-03 11:11:04 +03:00
if loadJson("config.json")["enable_nomic"]:
await changeNomicPerms("deny", vc_from, member)
2022-02-05 02:31:32 +02:00
# If user joined vc
else:
if isUserVoice(vc_from):
if isVoiceOfUser(vc_from, member):
await removeUserVoice(vc_from)
else:
2022-08-03 11:11:04 +03:00
if loadJson("config.json")["enable_nomic"]:
await changeNomicPerms("deny", vc_from, member)
2022-02-05 02:31:32 +02:00
if isUserVoice(vc_to):
2022-08-03 11:11:04 +03:00
if loadJson("config.json")["enable_nomic"]:
await changeNomicPerms("allow", vc_to, member)
2022-02-08 23:37:24 +02:00
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)
2022-02-05 02:31:32 +02:00
try:
await member.move_to(voice_chan)
except:
await removeUserVoice(voice_chan)
else:
2022-02-08 23:37:24 +02:00
if debug:
appendLog(f"Category for guild {vc_to.guild} ({str(vc_to.guild.id)}) is not set", guild=vc_to.guild)
else:
appendLog(f"Category for guild {vc_to.guild} is not set", guild=vc_to.guild)
2022-02-05 02:31:32 +02:00
2022-05-09 00:09:19 +03:00
#=========================================================================================================================
@client.slash_command(name="shutdown", description="Restart the bot")
async def shutdown(ctx: ApplicationContext):
2022-02-05 02:31:32 +02:00
config = loadJson("config.json")
2022-05-09 00:09:19 +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"])))
os.system(f"kill -9 {str(pid)}")
2022-02-05 02:31:32 +02:00
else:
2022-05-09 00:09:19 +03:00
await ctx.respond(embed=makeEmbed(title=getMsg("admin_title", ctx.guild), description=getMsg("admin_description", ctx.guild), color=strToColor(config["color_error"])))
#=========================================================================================================================
2022-02-05 02:31:32 +02:00
2022-05-09 00:09:19 +03:00
#=========================================================================================================================
@client.slash_command(name="help", description="Get information about this server")
async def help(ctx: ApplicationContext):
await ctx.respond(embed=getHelpMessage(ctx, version))
#=========================================================================================================================
2022-02-05 02:31:32 +02:00
2022-05-09 00:09:19 +03:00
#=========================================================================================================================
locale = client.create_group("locale", "Commands related to bot's locale")
2022-02-10 21:26:16 +02:00
2022-05-09 00:09:19 +03:00
valid_locales = []
files_locales = os.listdir(f"{path}/locale/")
for entry in files_locales:
valid_locales.append(".".join(entry.split(".")[:-1]))
2022-02-10 21:26:16 +02:00
2022-05-09 00:09:19 +03:00
@locale.command(name="set", description="Set bot's messages language")
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/"):
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"])))
2022-02-05 02:31:32 +02:00
else:
2022-05-09 00:09:19 +03:00
valid_locales = []
files_locales = os.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"])))
else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"])))
2022-02-05 02:31:32 +02:00
2022-05-09 00:09:19 +03:00
@locale.command(name="reset", description="Reset the bot's language in this guild")
async def locale_reset(ctx: ApplicationContext): # type: ignore
config = loadJson("config.json")
if ctx.guild is not None:
if guildConfGet(ctx.guild, "locale") is not None:
guildConfReset(ctx.guild, "locale")
appendLog(f"Server's locale has been reset", ctx.guild)
await ctx.respond(embed=makeEmbed(title=getMsg("reset_locale_title", ctx.guild), description=getMsg("reset_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_ok"])))
2022-02-07 03:06:01 +02:00
else:
2022-05-09 00:09:19 +03:00
await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_locale_title", ctx.guild), description=getMsg("hint_none_locale_description", ctx.guild).format(getMsg("locale_name", ctx.guild)), color=strToColor(config["color_warn"])))
else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"])))
#=========================================================================================================================
2022-02-07 03:06:01 +02:00
2022-05-09 00:09:19 +03:00
#=========================================================================================================================
channel = client.create_group("channel", "Commands related to parent voice channel")
@channel.command(name="set", description="Select the voice channel that will be parent to private ones")
async def channel_set(ctx: ApplicationContext, channel: Option(discord.VoiceChannel, "Parent Voice Channel")): # type: ignore
config = loadJson("config.json")
if ctx.guild is not None:
guildConfSet(ctx.guild, "channel", channel.id)
await ctx.respond(embed=makeEmbed(title=getMsg("set_channel_title", ctx.guild), description=getMsg("set_channel_description", ctx.guild).format(channel.name), color=strToColor(config["color_ok"])))
if guildConfGet(ctx.guild, "category") is None:
await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_category_title", ctx.guild), description=getMsg("hint_none_category_description", ctx.guild), color=strToColor(config["color_warn"])))
else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"])))
@channel.command(name="reset", description="Reset the currently selected parent voice channel")
async def channel_reset(ctx: ApplicationContext): # type: ignore
config = loadJson("config.json")
if ctx.guild is not None:
if guildConfGet(ctx.guild, "channel") is not None:
guildConfReset(ctx.guild, "channel")
await ctx.respond(embed=makeEmbed(title=getMsg("reset_channel_title", ctx.guild), description=getMsg("reset_channel_description", ctx.guild), color=strToColor(config["color_ok"])))
2022-02-05 02:31:32 +02:00
else:
2022-05-09 00:09:19 +03:00
await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_channel_title", ctx.guild), description=getMsg("hint_none_channel_description", ctx.guild), color=strToColor(config["color_warn"])))
else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"])))
#=========================================================================================================================
2022-02-05 02:31:32 +02:00
2022-05-09 00:09:19 +03:00
#=========================================================================================================================
category = client.create_group("category", "Commands related to parent channels category")
2022-02-07 03:06:01 +02:00
2022-05-09 00:09:19 +03:00
@category.command(name="set", description="Select the voice channel that will be parent to private ones")
async def category_set(ctx: ApplicationContext, category: Option(discord.CategoryChannel, "Parent Channel Category")): # type: ignore
config = loadJson("config.json")
if ctx.guild is not None:
guildConfSet(ctx.guild, "category", category.id)
await ctx.respond(embed=makeEmbed(title=getMsg("set_category_title", ctx.guild), description=getMsg("set_category_description", ctx.guild).format(category.name), color=strToColor(config["color_ok"])))
if guildConfGet(ctx.guild, "channel") is None:
await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_channel_title", ctx.guild), description=getMsg("hint_none_channel_description", ctx.guild), color=strToColor(config["color_warn"])))
else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"])))
2022-02-07 03:06:01 +02:00
2022-05-09 00:09:19 +03:00
@category.command(name="reset", description="Reset the currently selected parent channel category")
async def category_reset(ctx: ApplicationContext): # type: ignore
config = loadJson("config.json")
if ctx.guild is not None:
if guildConfGet(ctx.guild, "category") is not None:
guildConfReset(ctx.guild, "category")
await ctx.respond(embed=makeEmbed(title=getMsg("reset_category_title", ctx.guild), description=getMsg("reset_category_description", ctx.guild), color=strToColor(config["color_ok"])))
else:
await ctx.respond(embed=makeEmbed(title=getMsg("hint_none_category_title", ctx.guild), description=getMsg("hint_none_category_description", ctx.guild), color=strToColor(config["color_warn"])))
else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"])))
#=========================================================================================================================
2022-02-05 02:31:32 +02:00
2022-05-09 00:09:19 +03:00
appendLog(f"Trying to log in...")
client.run(loadJson("config.json")["bot_token"])