YusarinBot/yusarin.py

357 lines
17 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:
import discord
except Exception as exp:
print(f"Module discord.py is not installed. Make sure to run 'pip install -r requirements.txt' before first start")
sys.exit()
2022-02-05 02:31:32 +02:00
from functions import *
pid = os.getpid()
2022-02-14 19:07:31 +02:00
version = 1.4
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://github.com/profitrollgame/YusarinBot")
sys.exit()
2022-02-07 03:06:01 +02:00
if loadJson("config.json")["check_for_updates"]:
try:
serv_ver = requests.get("https://www.end-play.xyz/yusarin/version.txt").text.replace('\n', '')
if float(serv_ver) > version:
appendLog(f"YusarinBot version {serv_ver} is available. Download new version here: https://github.com/profitrollgame/YusarinBot/releases/latest")
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
intents = discord.Intents().all()
client = discord.Client(intents=intents)
@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:
await changeNomicPerms("deny", vc_from, member)
# If user joined vc
else:
if isUserVoice(vc_from):
if isVoiceOfUser(vc_from, member):
await removeUserVoice(vc_from)
else:
await changeNomicPerms("deny", vc_from, member)
if isUserVoice(vc_to):
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
# ==========================================================================================
@client.event
async def on_message(message):
config = loadJson("config.json")
if message.guild is not None:
try:
2022-02-08 23:37:24 +02:00
prefix = guildConfGet(message.guild, "prefix")
2022-02-05 02:31:32 +02:00
if prefix is None:
prefix = config["bot_prefix"]
except Exception as exp:
print(exp)
prefix = config["bot_prefix"]
else:
prefix = config["bot_prefix"]
if message.author == client.user:
return
if message.content in [f"{prefix}reboot", f"{prefix}restart", f"{prefix}shutdown", f"{prefix}die"]:
2022-02-08 23:37:24 +02:00
gotCommand(message)
2022-02-05 02:31:32 +02:00
if message.author.id == config["owner"]:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(description=getMsg("shutdown", message.guild).format(message.author), color=strToColor(config["color_default"])), mention_author=False)
2022-02-05 02:31:32 +02:00
os.system(f"kill -9 {str(pid)}")
else:
return
elif message.content.startswith(f"{prefix}channel"):
2022-02-08 23:37:24 +02:00
gotCommand(message)
2022-02-05 02:31:32 +02:00
fullcmd = message.content.split()
if message.guild is not None:
if message.author.guild_permissions.administrator:
try:
if fullcmd[1] == "reset":
2022-02-08 23:37:24 +02:00
if guildConfGet(message.guild, "channel") is not None:
2022-02-05 02:31:32 +02:00
2022-02-08 23:37:24 +02:00
guildConfReset(message.guild, "channel")
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("reset_channel_title", message.guild), description=getMsg("reset_channel_description", message.guild).format(prefix), color=strToColor(config["color_ok"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("hint_none_channel_title", message.guild), description=getMsg("hint_none_channel_description", message.guild).format(prefix), color=strToColor(config["color_warn"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
selected_channel = discord.utils.get(message.guild.channels, id=int(fullcmd[1]))
2022-02-10 21:26:16 +02:00
if isinstance(selected_channel, discord.VoiceChannel):
2022-02-05 02:31:32 +02:00
2022-02-10 21:26:16 +02:00
guildConfSet(message.guild, "channel", int(fullcmd[1]))
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("set_channel_title", message.guild), description=getMsg("set_channel_description", message.guild).format(selected_channel.name), color=strToColor(config["color_ok"])), mention_author=False)
2022-02-05 02:31:32 +02:00
2022-02-10 21:26:16 +02:00
if guildConfGet(message.guild, "category") is None:
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.channel.send(embed=makeEmbed(title=getMsg("hint_none_category_title", message.guild), description=getMsg("hint_none_category_description", message.guild).format(prefix), color=strToColor(config["color_warn"])))
2022-02-10 21:26:16 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("error_text_channel_title", message.guild), description=getMsg("error_text_channel_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-10 21:26:16 +02:00
2022-02-05 02:31:32 +02:00
except Exception as exp:
2022-02-14 19:07:31 +02:00
if debug:
print(exp)
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("error_channel_title", message.guild), description=getMsg("error_channel_description", message.guild).format(prefix), footer=getMsg("help_notice_id", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("forbidden_title", message.guild), description=getMsg("forbidden_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("dm_title", message.guild), description=getMsg("dm_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
elif message.content.startswith(f"{prefix}category"):
2022-02-08 23:37:24 +02:00
gotCommand(message)
2022-02-05 02:31:32 +02:00
fullcmd = message.content.split()
if message.guild is not None:
if message.author.guild_permissions.administrator:
try:
if fullcmd[1] == "reset":
2022-02-08 23:37:24 +02:00
if guildConfGet(message.guild, "category") is not None:
2022-02-05 02:31:32 +02:00
2022-02-08 23:37:24 +02:00
guildConfReset(message.guild, "category")
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("reset_category_title", message.guild), description=getMsg("reset_category_description", message.guild).format(prefix), color=strToColor(config["color_ok"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("hint_none_category_title", message.guild), description=getMsg("hint_none_category_description", message.guild).format(prefix), color=strToColor(config["color_warn"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
selected_category = discord.utils.get(message.guild.channels, id=int(fullcmd[1]))
2022-02-08 23:37:24 +02:00
guildConfSet(message.guild, "category", int(fullcmd[1]))
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("set_category_title", message.guild), description=getMsg("set_category_description", message.guild).format(selected_category.name), color=strToColor(config["color_ok"])), mention_author=False)
2022-02-05 02:31:32 +02:00
2022-02-08 23:37:24 +02:00
if guildConfGet(message.guild, "channel") is None:
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.channel.send(embed=makeEmbed(title=getMsg("hint_none_channel_title", message.guild), description=getMsg("hint_none_channel_description", message.guild).format(prefix), color=strToColor(config["color_warn"])))
2022-02-05 02:31:32 +02:00
except Exception as exp:
2022-02-14 19:07:31 +02:00
if debug:
print(exp)
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("error_category_title", message.guild), description=getMsg("error_category_description", message.guild).format(prefix), footer=getMsg("help_notice_id_category", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("forbidden_title", message.guild), description=getMsg("forbidden_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("dm_title", message.guild), description=getMsg("dm_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
elif message.content.startswith(f"{prefix}prefix"):
2022-02-08 23:37:24 +02:00
gotCommand(message)
2022-02-05 02:31:32 +02:00
fullcmd = message.content.split()
if message.guild is not None:
if message.author.guild_permissions.administrator:
try:
if fullcmd[1] == "reset":
2022-02-08 23:37:24 +02:00
if guildConfGet(message.guild, "prefix") is not None:
2022-02-05 02:31:32 +02:00
2022-02-08 23:37:24 +02:00
guildConfReset(message.guild, "prefix")
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("reset_prefix_title", message.guild), description=getMsg("reset_prefix_description", message.guild).format(config["bot_prefix"], config["bot_prefix"]), color=strToColor(config["color_ok"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("hint_none_prefix_title", message.guild), description=getMsg("hint_none_prefix_description", message.guild).format(prefix, prefix), color=strToColor(config["color_warn"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-08 23:37:24 +02:00
guildConfSet(message.guild, "prefix", fullcmd[1])
2022-02-05 02:31:32 +02:00
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("set_prefix_title", message.guild), description=getMsg("set_prefix_description", message.guild).format(fullcmd[1]), color=strToColor(config["color_ok"])), mention_author=False)
2022-02-07 03:06:01 +02:00
except:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("error_prefix_title", message.guild), description=getMsg("error_prefix_description", message.guild).format(prefix), color=strToColor(config["color_error"])), mention_author=False)
2022-02-07 03:06:01 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("forbidden_title", message.guild), description=getMsg("forbidden_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-07 03:06:01 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("dm_title", message.guild), description=getMsg("dm_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-07 03:06:01 +02:00
elif message.content.startswith(f"{prefix}locale"):
2022-02-08 23:37:24 +02:00
gotCommand(message)
2022-02-07 03:06:01 +02:00
fullcmd = message.content.split()
if message.guild is not None:
if message.author.guild_permissions.administrator:
try:
if fullcmd[1] == "reset":
2022-02-08 23:37:24 +02:00
if guildConfGet(message.guild, "locale") is not None:
2022-02-07 03:06:01 +02:00
2022-02-08 23:37:24 +02:00
guildConfReset(message.guild, "locale")
appendLog(f"Server's locale has been reset", message.guild)
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("reset_locale_title", message.guild), description=getMsg("reset_locale_description", message.guild).format(getMsg("locale_name", message.guild), prefix), color=strToColor(config["color_ok"])), mention_author=False)
2022-02-07 03:06:01 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("hint_none_locale_title", message.guild), description=getMsg("hint_none_locale_description", message.guild).format(getMsg("locale_name", message.guild), prefix), color=strToColor(config["color_warn"])), mention_author=False)
2022-02-07 03:06:01 +02:00
else:
for locale_file in os.listdir(f"{path}/locale/"):
if locale_file[:-5] == fullcmd[1]:
2022-02-08 23:37:24 +02:00
guildConfSet(message.guild, "locale", fullcmd[1])
appendLog(f"Server's locale is now set to {fullcmd[1]}", message.guild)
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("set_locale_title", message.guild), description=getMsg("set_locale_description", message.guild).format(getMsg("locale_name", message.guild)), color=strToColor(config["color_ok"])), mention_author=False)
2022-02-07 03:06:01 +02:00
return
locales = []
for locale_file in os.listdir(f"{path}/locale/"):
locales.append(f"`{locale_file[:-5]}`")
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("error_locale_title", message.guild), description=getMsg("error_locale_description", message.guild).format(prefix, ", ".join(locales)), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
except:
2022-02-07 03:06:01 +02:00
locales = []
for locale_file in os.listdir(f"{path}/locale/"):
locales.append(f"`{locale_file[:-5]}`")
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("error_locale_title", message.guild), description=getMsg("error_locale_description", message.guild).format(prefix, ", ".join(locales)), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("forbidden_title", message.guild), description=getMsg("forbidden_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
else:
2022-02-14 19:07:31 +02:00
await message.reply(embed=makeEmbed(title=getMsg("dm_title", message.guild), description=getMsg("dm_description", message.guild), color=strToColor(config["color_error"])), mention_author=False)
2022-02-05 02:31:32 +02:00
elif message.content.startswith(f"{prefix}help"):
2022-02-08 23:37:24 +02:00
gotCommand(message)
2022-02-14 19:07:31 +02:00
await message.reply(embed=getHelpMessage(message, version, prefix=prefix), mention_author=False)
2022-02-07 03:06:01 +02:00
#if loadJson("config.json")["auto_clear_trash"]:
# run func
appendLog(f"Trying to log in...")
2022-02-05 02:31:32 +02:00
2022-02-10 21:26:16 +02:00
client.run(loadJson("config.json")["bot_token"])