Tasty refactor
This commit is contained in:
parent
78239c7e5d
commit
a3a0aaa72b
196
functions.py
196
functions.py
@ -1,14 +1,12 @@
|
|||||||
import gzip
|
from json import loads, dumps
|
||||||
import os
|
from gzip import open as gzipopen
|
||||||
import sys
|
|
||||||
import json
|
|
||||||
import shutil
|
|
||||||
import discord # type: ignore
|
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from pathlib import Path
|
from os import listdir, makedirs, remove, stat, path
|
||||||
|
from shutil import copyfileobj
|
||||||
|
from typing import Any, Literal, Union
|
||||||
|
from discord import utils, Embed, TextChannel, VoiceChannel, PermissionOverwrite, Guild, CategoryChannel, Member, Client, ApplicationContext, Message
|
||||||
|
|
||||||
path = Path(__file__).resolve().parent
|
# path = Path(__file__).resolve().parent
|
||||||
|
|
||||||
log_size = 512
|
log_size = 512
|
||||||
|
|
||||||
@ -19,7 +17,7 @@ debug = False
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
with open("config.json", 'r', encoding="utf-8") as json_file:
|
with open("config.json", 'r', encoding="utf-8") as json_file:
|
||||||
output = json.load(json_file)
|
output = loads(json_file.read())
|
||||||
json_file.close()
|
json_file.close()
|
||||||
debug = output["debug"]
|
debug = output["debug"]
|
||||||
except:
|
except:
|
||||||
@ -27,30 +25,28 @@ except:
|
|||||||
|
|
||||||
# Check latest log size
|
# Check latest log size
|
||||||
def checkSize():
|
def checkSize():
|
||||||
global path
|
|
||||||
path = str(path)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if not os.path.isdir(f"{path}/logs"):
|
if not path.isdir("logs"):
|
||||||
os.mkdir(f"{path}/logs")
|
makedirs("logs", exist_ok=True)
|
||||||
|
|
||||||
log = os.stat(path + '/logs/latest.log')
|
log = stat("logs/latest.log")
|
||||||
global log_size
|
global log_size
|
||||||
|
|
||||||
if (log.st_size / 1024) > log_size:
|
if (log.st_size / 1024) > log_size:
|
||||||
with open(path + '/logs/latest.log', 'rb') as f_in:
|
with open("logs/latest.log", "rb") as f_in:
|
||||||
with gzip.open(f'{path}/logs/{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.log.gz', 'wb') as f_out:
|
with gzipopen(f'logs/{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.log.gz', 'wb') as f_out:
|
||||||
shutil.copyfileobj(f_in, f_out)
|
copyfileobj(f_in, f_out)
|
||||||
print(f'Copied {path}/logs/{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.log.gz')
|
print(f'Copied logs/{datetime.now().strftime("%d.%m.%Y_%H:%M:%S")}.log.gz')
|
||||||
open(path + '/logs/latest.log', 'w').close()
|
open("logs/latest.log", "w").close()
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
print('Not found')
|
print('Not found')
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Append string to log
|
# Append string to log
|
||||||
def appendLog(message, guild=None, announce=True):
|
def appendLog(message: str, guild: Union[Guild, None] = None, announce=True) -> None:
|
||||||
global debug
|
global debug
|
||||||
global path
|
|
||||||
|
|
||||||
if guild == None:
|
if guild == None:
|
||||||
message_formatted = f'[{datetime.now().strftime("%d.%m.%Y")}] [{datetime.now().strftime("%H:%M:%S")}] {message}'
|
message_formatted = f'[{datetime.now().strftime("%d.%m.%Y")}] [{datetime.now().strftime("%H:%M:%S")}] {message}'
|
||||||
@ -65,20 +61,20 @@ def appendLog(message, guild=None, announce=True):
|
|||||||
|
|
||||||
checkSize()
|
checkSize()
|
||||||
|
|
||||||
log = open(path + '/logs/latest.log', 'a') # type: ignore
|
log = open('logs/latest.log', 'a') # type: ignore
|
||||||
log.write(f'{message_formatted}\n')
|
log.write(f'{message_formatted}\n')
|
||||||
log.close()
|
log.close()
|
||||||
|
|
||||||
def saveJson(value, filename):
|
def saveJson(value: Any, filename: str) -> None:
|
||||||
with open(filename, 'w', encoding="utf-8") as f:
|
with open(filename, 'w', encoding="utf-8") as f:
|
||||||
json.dump(value, f, indent=4, ensure_ascii=False)
|
f.write(dumps(value, indent=4, ensure_ascii=False))
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def loadJson(filename):
|
def loadJson(filename: str) -> Any:
|
||||||
global debug
|
global debug
|
||||||
try:
|
try:
|
||||||
with open(filename, 'r', encoding="utf-8") as json_file:
|
with open(filename, 'r', encoding="utf-8") as json_file:
|
||||||
output = json.load(json_file)
|
output = loads(json_file.read())
|
||||||
json_file.close()
|
json_file.close()
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
if debug:
|
if debug:
|
||||||
@ -89,57 +85,53 @@ def loadJson(filename):
|
|||||||
def colorToStr():
|
def colorToStr():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def strToColor(string):
|
def strToColor(string: str) -> int:
|
||||||
return int(hex(int(string.replace("#", ""), 16)), 0)
|
return int(hex(int(string.replace("#", ""), 16)), 0)
|
||||||
|
|
||||||
def gotCommand(message):
|
def gotCommand(message: Message) -> None:
|
||||||
global debug
|
global debug
|
||||||
if debug:
|
if debug:
|
||||||
appendLog(f"Command '{message.content}' from {message.author} ({str(message.author.id)})", message.guild)
|
appendLog(f"Command '{message.content}' from {message.author} ({str(message.author.id)})", message.guild)
|
||||||
else:
|
else:
|
||||||
appendLog(f"Command '{message.content}' from {message.author}", message.guild)
|
appendLog(f"Command '{message.content}' from {message.author}", message.guild)
|
||||||
|
|
||||||
def guildConfGet(guild, variable):
|
def guildConfGet(guild: Guild, variable: str) -> Any:
|
||||||
global path
|
|
||||||
global debug
|
global debug
|
||||||
try:
|
try:
|
||||||
config = loadJson(f"{path}/guilds/{str(guild.id)}/config.json")
|
config = loadJson(f"guilds/{str(guild.id)}/config.json")
|
||||||
return config[variable]
|
return config[variable]
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
if debug:
|
if debug:
|
||||||
appendLog(f"Could not get guild config key '{variable}' due to {exp}", guild)
|
appendLog(f"Could not get guild config key '{variable}' due to {exp}", guild)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def guildConfSet(guild, variable, value):
|
def guildConfSet(guild: Guild, variable: str, value: Any) -> None:
|
||||||
global path
|
config = loadJson(f"guilds/{str(guild.id)}/config.json")
|
||||||
config = loadJson(f"{path}/guilds/{str(guild.id)}/config.json")
|
|
||||||
config[variable] = value
|
config[variable] = value
|
||||||
try:
|
try:
|
||||||
saveJson(config, f"{path}/guilds/{str(guild.id)}/config.json")
|
saveJson(config, f"guilds/{str(guild.id)}/config.json")
|
||||||
except:
|
except:
|
||||||
os.mkdir(f"{path}/guilds/{str(guild.id)}")
|
makedirs(f"guilds/{str(guild.id)}", exist_ok=True)
|
||||||
os.mkdir(f"{path}/guilds/{str(guild.id)}/channels")
|
makedirs(f"guilds/{str(guild.id)}/channels", exist_ok=True)
|
||||||
saveJson(config, f"{path}/guilds/{str(guild.id)}/config.json")
|
saveJson(config, f"guilds/{str(guild.id)}/config.json")
|
||||||
appendLog(f"Guild config key '{variable}' is now set to '{value}'", guild)
|
appendLog(f"Guild config key '{variable}' is now set to '{value}'", guild)
|
||||||
|
|
||||||
def guildConfReset(guild, variable):
|
def guildConfReset(guild: Guild, variable: str) -> None:
|
||||||
global path
|
|
||||||
try:
|
try:
|
||||||
config = loadJson(f"{path}/guilds/{str(guild.id)}/config.json")
|
config = loadJson(f"guilds/{str(guild.id)}/config.json")
|
||||||
del config[variable]
|
del config[variable]
|
||||||
try:
|
try:
|
||||||
saveJson(config, f"{path}/guilds/{str(guild.id)}/config.json")
|
saveJson(config, f"guilds/{str(guild.id)}/config.json")
|
||||||
except:
|
except:
|
||||||
os.mkdir(f"{path}/guilds/{str(guild.id)}")
|
makedirs(f"guilds/{str(guild.id)}", exist_ok=True)
|
||||||
os.mkdir(f"{path}/guilds/{str(guild.id)}/channels")
|
makedirs(f"guilds/{str(guild.id)}/channels", exist_ok=True)
|
||||||
saveJson(config, f"{path}/guilds/{str(guild.id)}/config.json")
|
saveJson(config, f"guilds/{str(guild.id)}/config.json")
|
||||||
appendLog(f"Guild config key '{variable}' has been reset", guild)
|
appendLog(f"Guild config key '{variable}' has been reset", guild)
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
appendLog(f"Could not reset guild config key '{variable}' due to {exp}", guild)
|
appendLog(f"Could not reset guild config key '{variable}' due to {exp}", guild)
|
||||||
|
|
||||||
def guildLocaleGet(guild):
|
def guildLocaleGet(guild: Guild) -> str:
|
||||||
global path
|
config = loadJson(f"config.json")
|
||||||
config = loadJson(f"{path}/config.json")
|
|
||||||
try:
|
try:
|
||||||
locale = guildConfGet(guild, "locale")
|
locale = guildConfGet(guild, "locale")
|
||||||
except:
|
except:
|
||||||
@ -149,57 +141,54 @@ def guildLocaleGet(guild):
|
|||||||
else:
|
else:
|
||||||
return locale
|
return locale
|
||||||
|
|
||||||
def getMsg(string, guild=None):
|
def getMsg(string: str, guild: Union[Guild, None] = None) -> str:
|
||||||
global path
|
|
||||||
config = loadJson("config.json")
|
|
||||||
try:
|
try:
|
||||||
locale = loadJson(f'{path}/locale/{guildLocaleGet(guild)}.json')
|
locale = loadJson(f'locale/{guildLocaleGet(guild)}.json')
|
||||||
return locale["messages"][string]
|
return locale["messages"][string]
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
appendLog(f"Could not get locale string named {string} due to exception {exp}", guild)
|
appendLog(f"Could not get locale string named {string} due to exception {exp}", guild)
|
||||||
return string
|
return string
|
||||||
|
|
||||||
def makeEmbed(title="", description="", footer="", color=0xffffff):
|
def makeEmbed(title="", description="", footer="", color=0xffffff) -> Embed:
|
||||||
embed=discord.Embed(title=title, description=description, color=color)
|
embed=Embed(title=title, description=description, color=color)
|
||||||
if footer is not None:
|
if footer is not None:
|
||||||
embed.set_footer(text=footer)
|
embed.set_footer(text=footer)
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
def channelExists(number, guild, type="Any"):
|
def channelExists(number: int, guild: Guild, type: Literal["Any", "Text", "Voice"] = "Any") -> bool:
|
||||||
global debug
|
global debug
|
||||||
if number == None:
|
if number == None:
|
||||||
return False
|
return False
|
||||||
try:
|
try:
|
||||||
if type == "Voice":
|
if type == "Voice":
|
||||||
selected_channel = discord.utils.get(guild.channels, id=number)
|
selected_channel = utils.get(guild.channels, id=number)
|
||||||
if isinstance(selected_channel, discord.VoiceChannel):
|
if isinstance(selected_channel, VoiceChannel):
|
||||||
return True
|
return True
|
||||||
elif type == "Text":
|
elif type == "Text":
|
||||||
selected_channel = discord.utils.get(guild.channels, id=number)
|
selected_channel = utils.get(guild.channels, id=number)
|
||||||
if isinstance(selected_channel, discord.TextChannel):
|
if isinstance(selected_channel, TextChannel):
|
||||||
return True
|
return True
|
||||||
elif type == "Any":
|
elif type == "Any":
|
||||||
selected_channel = discord.utils.get(guild.channels, id=number)
|
selected_channel = utils.get(guild.channels, id=number)
|
||||||
return True
|
return True
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
if debug:
|
if debug:
|
||||||
appendLog(f"Channel ID {str(number)} is not a channel due to {exp}")
|
appendLog(f"Channel ID {str(number)} is not a channel due to {exp}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def channelGetName(number, guild):
|
def channelGetName(number: int, guild: Guild):
|
||||||
global debug
|
global debug
|
||||||
try:
|
try:
|
||||||
selected_channel = discord.utils.get(guild.channels, id=number)
|
selected_channel = utils.get(guild.channels, id=number)
|
||||||
return selected_channel.name
|
return selected_channel.name
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
if debug:
|
if debug:
|
||||||
appendLog(f"Channel ID {str(number)} is not a channel due to {exp}")
|
appendLog(f"Channel ID {str(number)} is not a channel due to {exp}")
|
||||||
return "Channel doesn't exist"
|
return "Channel doesn't exist"
|
||||||
|
|
||||||
def isUserVoice(vc):
|
def isUserVoice(vc: VoiceChannel) -> bool:
|
||||||
global path
|
|
||||||
try:
|
try:
|
||||||
channels_list = os.listdir(f"{path}/guilds/{str(vc.guild.id)}/channels/")
|
channels_list = listdir(f"guilds/{str(vc.guild.id)}/channels/")
|
||||||
if f"{str(vc.id)}.json" in channels_list:
|
if f"{str(vc.id)}.json" in channels_list:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
@ -207,19 +196,18 @@ def isUserVoice(vc):
|
|||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def removeUserVoice(vc):
|
async def removeUserVoice(vc: VoiceChannel) -> None:
|
||||||
global path
|
|
||||||
global debug
|
global debug
|
||||||
channels_list = os.listdir(f"{path}/guilds/{str(vc.guild.id)}/channels/")
|
channels_list = listdir(f"guilds/{str(vc.guild.id)}/channels/")
|
||||||
if f"{vc.id}.json" in channels_list:
|
if f"{vc.id}.json" in channels_list:
|
||||||
vc_file = f"{path}/guilds/{str(vc.guild.id)}/channels/{str(vc.id)}.json"
|
vc_file = f"guilds/{str(vc.guild.id)}/channels/{str(vc.id)}.json"
|
||||||
vc_conf = loadJson(vc_file)
|
vc_conf = loadJson(vc_file)
|
||||||
|
|
||||||
needed_channel = discord.utils.get(vc.guild.channels, id=vc.id)
|
needed_channel = utils.get(vc.guild.channels, id=vc.id)
|
||||||
if loadJson("config.json")["enable_nomic"]:
|
if loadJson("config.json")["enable_nomic"]:
|
||||||
nomic_channel = discord.utils.get(vc.guild.channels, id=vc_conf["nomic"])
|
nomic_channel = utils.get(vc.guild.channels, id=vc_conf["nomic"])
|
||||||
|
|
||||||
os.remove(vc_file)
|
remove(vc_file)
|
||||||
|
|
||||||
await needed_channel.delete()
|
await needed_channel.delete()
|
||||||
if debug:
|
if debug:
|
||||||
@ -236,28 +224,27 @@ async def removeUserVoice(vc):
|
|||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
async def createUserVoice(vc, category, member):
|
async def createUserVoice(vc: VoiceChannel, category: CategoryChannel, member: Member) -> VoiceChannel:
|
||||||
global path
|
|
||||||
global debug
|
global debug
|
||||||
chan = {}
|
chan = {}
|
||||||
overwrites_channel = {
|
overwrites_channel = {
|
||||||
vc.guild.default_role: discord.PermissionOverwrite(view_channel=True),
|
vc.guild.default_role: PermissionOverwrite(view_channel=True),
|
||||||
vc.guild.me: discord.PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True),
|
vc.guild.me: PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True),
|
||||||
member: discord.PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True)
|
member: PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True)
|
||||||
}
|
}
|
||||||
overwrites_nomic = {
|
overwrites_nomic = {
|
||||||
vc.guild.default_role: discord.PermissionOverwrite(view_channel=False, read_messages=False),
|
vc.guild.default_role: PermissionOverwrite(view_channel=False, read_messages=False),
|
||||||
vc.guild.me: discord.PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True),
|
vc.guild.me: PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True),
|
||||||
member: discord.PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True)
|
member: PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True)
|
||||||
}
|
}
|
||||||
created_channel = await vc.guild.create_voice_channel(getMsg("name_voice", vc.guild).format(member.name), category=category, overwrites=overwrites_channel)
|
created_channel = await vc.guild.create_voice_channel(getMsg("name_voice", vc.guild).format(member.name), category=category, overwrites=overwrites_channel)
|
||||||
if debug:
|
if debug:
|
||||||
appendLog(f"Created voice channel '{created_channel}' ({str(created_channel.id)}) for user {member} ({str(member.id)})", guild=vc.guild)
|
appendLog(f"Created voice channel '{created_channel}' ({str(created_channel.id)}) for user {member} ({str(member.id)})", guild=vc.guild)
|
||||||
else:
|
else:
|
||||||
appendLog(f"Created voice channel '{created_channel}' for user {member}", guild=vc.guild)
|
appendLog(f"Created voice channel '{created_channel}' for user {member}", guild=vc.guild)
|
||||||
if not os.path.isdir(f"{path}/guilds/{str(created_channel.guild.id)}/channels"):
|
if not path.isdir(f"guilds/{str(created_channel.guild.id)}/channels"):
|
||||||
os.mkdir(f"{path}/guilds/{str(created_channel.guild.id)}/channels")
|
makedirs(f"guilds/{str(created_channel.guild.id)}/channels", exist_ok=True)
|
||||||
vc_file = f"{path}/guilds/{str(created_channel.guild.id)}/channels/{str(created_channel.id)}.json"
|
vc_file = f"guilds/{str(created_channel.guild.id)}/channels/{str(created_channel.id)}.json"
|
||||||
chan["ownerid"] = member.id
|
chan["ownerid"] = member.id
|
||||||
saveJson(chan, vc_file)
|
saveJson(chan, vc_file)
|
||||||
if loadJson("config.json")["enable_nomic"]:
|
if loadJson("config.json")["enable_nomic"]:
|
||||||
@ -270,40 +257,37 @@ async def createUserVoice(vc, category, member):
|
|||||||
saveJson(chan, vc_file)
|
saveJson(chan, vc_file)
|
||||||
return created_channel
|
return created_channel
|
||||||
|
|
||||||
def isVoiceOfUser(vc, member):
|
def isVoiceOfUser(vc: VoiceChannel, member: Member) -> bool:
|
||||||
global path
|
vc_file = f"guilds/{str(vc.guild.id)}/channels/{str(vc.id)}.json"
|
||||||
vc_file = f"{path}/guilds/{str(vc.guild.id)}/channels/{str(vc.id)}.json"
|
|
||||||
vc_conf = loadJson(vc_file)
|
vc_conf = loadJson(vc_file)
|
||||||
if vc_conf["ownerid"] == member.id:
|
if vc_conf["ownerid"] == member.id:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
async def changeNomicPerms(mode, vc, member):
|
async def changeNomicPerms(mode: Literal["deny", "allow"], vc: VoiceChannel, member: Member) -> None:
|
||||||
global path
|
vc_file = f"guilds/{str(vc.guild.id)}/channels/{str(vc.id)}.json"
|
||||||
vc_file = f"{path}/guilds/{str(vc.guild.id)}/channels/{str(vc.id)}.json"
|
|
||||||
vc_conf = loadJson(vc_file)
|
vc_conf = loadJson(vc_file)
|
||||||
if loadJson("config.json")["enable_nomic"]:
|
if loadJson("config.json")["enable_nomic"]:
|
||||||
nomic_channel = discord.utils.get(vc.guild.channels, id=vc_conf["nomic"])
|
nomic_channel = utils.get(vc.guild.channels, id=vc_conf["nomic"])
|
||||||
if mode == "deny":
|
if mode == "deny":
|
||||||
await nomic_channel.set_permissions(member, view_channel=False)
|
await nomic_channel.set_permissions(member, view_channel=False)
|
||||||
else:
|
else:
|
||||||
await nomic_channel.set_permissions(member, view_channel=True)
|
await nomic_channel.set_permissions(member, view_channel=True)
|
||||||
|
|
||||||
async def clearTrash(client):
|
async def clearTrash(client: Client):
|
||||||
global path
|
if not path.isdir(f"guilds/"):
|
||||||
if not os.path.isdir(f"{path}/guilds/"):
|
makedirs(f"guilds", exist_ok=True)
|
||||||
os.mkdir(f"{path}/guilds")
|
guilds_list = listdir(f"guilds/")
|
||||||
guilds_list = os.listdir(f"{path}/guilds/")
|
|
||||||
for guild in guilds_list:
|
for guild in guilds_list:
|
||||||
guild_object = client.get_guild(int(guild))
|
guild_object = client.get_guild(int(guild))
|
||||||
if os.path.isdir(f"{path}/guilds/{guild}/channels"):
|
if path.isdir(f"guilds/{guild}/channels"):
|
||||||
channels_list = os.listdir(f"{path}/guilds/{guild}/channels/")
|
channels_list = listdir(f"guilds/{guild}/channels/")
|
||||||
for channel in channels_list:
|
for channel in channels_list:
|
||||||
channel_id = channel[:-5]
|
channel_id = channel[:-5]
|
||||||
try:
|
try:
|
||||||
selected_channel = discord.utils.get(guild_object.voice_channels, id=int(channel_id))
|
selected_channel = utils.get(guild_object.voice_channels, id=int(channel_id))
|
||||||
channel_owner = loadJson(f"{path}/guilds/{guild}/channels/{channel}")["ownerid"]
|
channel_owner = loadJson(f"guilds/{guild}/channels/{channel}")["ownerid"]
|
||||||
remove_channel = True
|
remove_channel = True
|
||||||
for member in selected_channel.members:
|
for member in selected_channel.members:
|
||||||
if member.id == channel_owner:
|
if member.id == channel_owner:
|
||||||
@ -311,12 +295,12 @@ async def clearTrash(client):
|
|||||||
if remove_channel:
|
if remove_channel:
|
||||||
await removeUserVoice(selected_channel)
|
await removeUserVoice(selected_channel)
|
||||||
except:
|
except:
|
||||||
os.remove(f"{path}/guilds/{guild}/channels/{channel_id}.json")
|
remove(f"guilds/{guild}/channels/{channel_id}.json")
|
||||||
|
|
||||||
#async def autoClearTrash(client):
|
#async def autoClearTrash(client):
|
||||||
# execute clearTrash every 120 seconds
|
# execute clearTrash every 120 seconds
|
||||||
|
|
||||||
def getHelpMessage(ctx, version):
|
def getHelpMessage(ctx: ApplicationContext, version: float) -> Embed:
|
||||||
|
|
||||||
#channelExists(number, guild, type="Voice")
|
#channelExists(number, guild, type="Voice")
|
||||||
|
|
||||||
@ -337,9 +321,9 @@ def getHelpMessage(ctx, version):
|
|||||||
|
|
||||||
description = "\n".join([desc_locale, desc_channel, desc_category])
|
description = "\n".join([desc_locale, desc_channel, desc_category])
|
||||||
|
|
||||||
embed=discord.Embed(title=getMsg("help_title", ctx.guild), description=description, color=strToColor(config["color_default"]))
|
embed=Embed(title=getMsg("help_title", ctx.guild), description=description, color=strToColor(config["color_default"]))
|
||||||
else:
|
else:
|
||||||
embed=discord.Embed(title=getMsg("help_title_dm", ctx.guild), color=strToColor(config["color_default"]))
|
embed=Embed(title=getMsg("help_title_dm", ctx.guild), color=strToColor(config["color_default"]))
|
||||||
|
|
||||||
embed.set_author(name=f'{config["bot_name"]} v{str(version)}', url=config["bot_site"], icon_url=config["bot_icon"])
|
embed.set_author(name=f'{config["bot_name"]} v{str(version)}', url=config["bot_site"], icon_url=config["bot_icon"])
|
||||||
|
|
||||||
@ -357,7 +341,7 @@ def getHelpMessage(ctx, version):
|
|||||||
|
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
async def guildConfigured(guild):
|
async def guildConfigured(guild: Guild) -> str:
|
||||||
|
|
||||||
output = {}
|
output = {}
|
||||||
config = loadJson("config.json")
|
config = loadJson("config.json")
|
||||||
@ -365,7 +349,7 @@ async def guildConfigured(guild):
|
|||||||
for kind in ["channel", "category"]:
|
for kind in ["channel", "category"]:
|
||||||
if guildConfGet(guild, kind) is not None:
|
if guildConfGet(guild, kind) is not None:
|
||||||
try:
|
try:
|
||||||
guild_object = discord.utils.get(guild.categories, id=guildConfGet(guild, kind))
|
guild_object = utils.get(guild.categories, id=guildConfGet(guild, kind))
|
||||||
output[kind] = getMsg("configured_"+kind, guild).format(guild_object.name)
|
output[kind] = getMsg("configured_"+kind, guild).format(guild_object.name)
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
output[kind] = getMsg("unconfigured_"+kind, guild)
|
output[kind] = getMsg("unconfigured_"+kind, guild)
|
||||||
|
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:
|
try:
|
||||||
import discord # type: ignore
|
from requests import get
|
||||||
from discord import ApplicationContext, Option, Intents # type: ignore
|
from discord import ApplicationContext, Option, Intents, Bot, ActivityType, Activity
|
||||||
except Exception as exp:
|
except Exception as exp:
|
||||||
print(f"Module py-cord is not installed. Make sure to run 'pip install -r requirements.txt' before first start")
|
print(f"Dependencies not installed. Make sure to run 'pip install -r requirements.txt' before first start")
|
||||||
sys.exit()
|
exit()
|
||||||
|
|
||||||
|
from os import getpid, system
|
||||||
|
from shutil import rmtree
|
||||||
from functions import *
|
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":
|
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")
|
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"]:
|
if loadJson("config.json")["check_for_updates"]:
|
||||||
try:
|
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:
|
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"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)}")
|
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)}")
|
appendLog(f"Could not get YusarinBot cloud version due to {exp}. Currently using {str(version)}")
|
||||||
|
|
||||||
intents = Intents().all()
|
intents = Intents().all()
|
||||||
client = discord.Bot(intents=intents)
|
client = Bot(intents=intents)
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
@ -39,7 +35,7 @@ async def on_ready():
|
|||||||
|
|
||||||
config = loadJson("config.json")
|
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)
|
await clearTrash(client)
|
||||||
|
|
||||||
@ -48,8 +44,8 @@ async def on_guild_join(guild):
|
|||||||
|
|
||||||
global path
|
global path
|
||||||
|
|
||||||
os.mkdir(f"{path}/guilds/{str(guild.id)}")
|
makedirs(f"{path}/guilds/{str(guild.id)}", exist_ok=True)
|
||||||
os.mkdir(f"{path}/guilds/{str(guild.id)}/channels")
|
makedirs(f"{path}/guilds/{str(guild.id)}/channels", exist_ok=True)
|
||||||
saveJson({}, f"{path}/guilds/{str(guild.id)}/config.json")
|
saveJson({}, f"{path}/guilds/{str(guild.id)}/config.json")
|
||||||
|
|
||||||
appendLog(f"Joined guild '{guild}' with id {str(guild.id)}")
|
appendLog(f"Joined guild '{guild}' with id {str(guild.id)}")
|
||||||
@ -60,7 +56,7 @@ async def on_guild_remove(guild):
|
|||||||
global path
|
global path
|
||||||
|
|
||||||
try:
|
try:
|
||||||
shutil.rmtree(f"{path}/guilds/{str(guild.id)}")
|
rmtree(f"{path}/guilds/{str(guild.id)}")
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -99,7 +95,7 @@ async def on_voice_state_update(member, before, after):
|
|||||||
await changeNomicPerms("allow", vc_to, member)
|
await changeNomicPerms("allow", vc_to, member)
|
||||||
if vc_to.id == guildConfGet(vc_to.guild, "channel"):
|
if vc_to.id == guildConfGet(vc_to.guild, "channel"):
|
||||||
if guildConfGet(vc_to.guild, "category") is not None:
|
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:
|
try:
|
||||||
await member.move_to(voice_chan)
|
await member.move_to(voice_chan)
|
||||||
except:
|
except:
|
||||||
@ -116,7 +112,7 @@ async def shutdown(ctx: ApplicationContext):
|
|||||||
config = loadJson("config.json")
|
config = loadJson("config.json")
|
||||||
if ctx.author.id == config["owner"]:
|
if ctx.author.id == config["owner"]:
|
||||||
await ctx.respond(embed=makeEmbed(description=getMsg("shutdown", ctx.guild).format(ctx.author), color=strToColor(config["color_default"])))
|
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:
|
else:
|
||||||
await ctx.respond(embed=makeEmbed(title=getMsg("admin_title", ctx.guild), description=getMsg("admin_description", ctx.guild), color=strToColor(config["color_error"])))
|
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")
|
locale = client.create_group("locale", "Commands related to bot's locale")
|
||||||
|
|
||||||
valid_locales = []
|
valid_locales = []
|
||||||
files_locales = os.listdir(f"{path}/locale/")
|
files_locales = listdir(f"{path}/locale/")
|
||||||
for entry in files_locales:
|
for entry in files_locales:
|
||||||
valid_locales.append(".".join(entry.split(".")[:-1]))
|
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
|
async def locale_set(ctx: ApplicationContext, language: Option(str, "One of the languages in list", choices=valid_locales)): # type: ignore
|
||||||
config = loadJson("config.json")
|
config = loadJson("config.json")
|
||||||
if ctx.guild is not None:
|
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)
|
guildConfSet(ctx.guild, "locale", language)
|
||||||
appendLog(f"Server's locale is now set to {language}", ctx.guild)
|
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"])))
|
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:
|
else:
|
||||||
valid_locales = []
|
valid_locales = []
|
||||||
files_locales = os.listdir(f"{path}/locale/")
|
files_locales = listdir(f"{path}/locale/")
|
||||||
for entry in files_locales:
|
for entry in files_locales:
|
||||||
valid_locales.append(entry.split(".")[:-1])
|
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"])))
|
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"])))
|
||||||
|
Loading…
Reference in New Issue
Block a user