Formatted everything with black

This commit is contained in:
Profitroll 2023-04-19 14:21:39 +02:00
parent 5329b4c727
commit 75c1889477
6 changed files with 502 additions and 147 deletions

View File

@ -1,35 +1,90 @@
from discord import ApplicationContext, Option, SlashCommandGroup, CategoryChannel from discord import ApplicationContext, Option, SlashCommandGroup, CategoryChannel
from discord.ext import commands from discord.ext import commands
from functions import getMsg, guildConfGet, guildConfReset, guildConfSet, loadJson, makeEmbed, strToColor from functions import (
getMsg,
guildConfGet,
guildConfReset,
guildConfSet,
loadJson,
makeEmbed,
strToColor,
)
#=========================================================================================================================
# =========================================================================================================================
class CogCategory(commands.Cog): class CogCategory(commands.Cog):
def __init__(self, client): def __init__(self, client):
self.client = client self.client = client
category = SlashCommandGroup("category", "Commands related to parent channels category") category = SlashCommandGroup(
"category", "Commands related to parent channels category"
)
@category.command(name="set", description="Select the voice channel that will be parent to private ones") @category.command(
async def category_set(self, ctx: ApplicationContext, category: Option(CategoryChannel, "Parent Channel Category")): # type: ignore name="set",
description="Select the voice channel that will be parent to private ones",
)
async def category_set(self, ctx: ApplicationContext, category: Option(CategoryChannel, "Parent Channel Category")): # type: ignore
config = loadJson("config.json") config = loadJson("config.json")
if ctx.guild is not None: if ctx.guild is not None:
guildConfSet(ctx.guild, "category", category.id) 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"]))) 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: 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"]))) 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: else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) await ctx.respond(
embed=makeEmbed(
title=getMsg("dm_title", ctx.guild),
description=getMsg("dm_description", ctx.guild),
color=strToColor(config["color_error"]),
)
)
@category.command(name="reset", description="Reset the currently selected parent channel category") @category.command(
async def category_reset(self, ctx: ApplicationContext): # type: ignore name="reset", description="Reset the currently selected parent channel category"
)
async def category_reset(self, ctx: ApplicationContext): # type: ignore
config = loadJson("config.json") config = loadJson("config.json")
if ctx.guild is not None: if ctx.guild is not None:
if guildConfGet(ctx.guild, "category") is not None: if guildConfGet(ctx.guild, "category") is not None:
guildConfReset(ctx.guild, "category") 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"]))) 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: 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"]))) 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: else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) await ctx.respond(
#========================================================================================================================= embed=makeEmbed(
title=getMsg("dm_title", ctx.guild),
description=getMsg("dm_description", ctx.guild),
color=strToColor(config["color_error"]),
)
)
# =========================================================================================================================

View File

@ -1,21 +1,45 @@
from discord import ApplicationContext, SlashCommandGroup, VoiceChannel, Member, VoiceState, Cog, option, utils from discord import (
ApplicationContext,
SlashCommandGroup,
VoiceChannel,
Member,
VoiceState,
Cog,
option,
utils,
)
from discord.ext import commands from discord.ext import commands
from functions import appendLog, changeNomicPerms, createUserVoice, getMsg, guildConfGet, guildConfReset, guildConfSet, isUserVoice, isVoiceOfUser, loadJson, makeEmbed, removeUserVoice, strToColor from functions import (
appendLog,
changeNomicPerms,
createUserVoice,
getMsg,
guildConfGet,
guildConfReset,
guildConfSet,
isUserVoice,
isVoiceOfUser,
loadJson,
makeEmbed,
removeUserVoice,
strToColor,
)
#=========================================================================================================================
# =========================================================================================================================
class CogChannel(commands.Cog): class CogChannel(commands.Cog):
def __init__(self, client): def __init__(self, client):
self.client = client self.client = client
@Cog.listener() @Cog.listener()
async def on_voice_state_update(self, member: Member, before: VoiceState, after: VoiceState): async def on_voice_state_update(
self, member: Member, before: VoiceState, after: VoiceState
):
config = loadJson("config.json") config = loadJson("config.json")
vc_from = before.channel vc_from = before.channel
vc_to = after.channel vc_to = after.channel
# If user left vc # If user left vc
if vc_to is None: if vc_to is None:
if isUserVoice(vc_from): if isUserVoice(vc_from):
@ -25,7 +49,7 @@ class CogChannel(commands.Cog):
else: else:
if loadJson("config.json")["enable_nomic"]: if loadJson("config.json")["enable_nomic"]:
await changeNomicPerms("deny", vc_from, member) await changeNomicPerms("deny", vc_from, member)
# If user joined vc # If user joined vc
else: else:
if isUserVoice(vc_from): if isUserVoice(vc_from):
@ -39,37 +63,92 @@ class CogChannel(commands.Cog):
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, 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:
await removeUserVoice(voice_chan) await removeUserVoice(voice_chan)
else: else:
appendLog(f"Category for guild {vc_to.guild} ({str(vc_to.guild.id)}) is not set", guild=vc_to.guild) appendLog(
f"Category for guild {vc_to.guild} ({str(vc_to.guild.id)}) is not set",
guild=vc_to.guild,
)
channel = SlashCommandGroup("channel", "Commands related to parent voice channel") channel = SlashCommandGroup("channel", "Commands related to parent voice channel")
@channel.command(name="set", description="Select the voice channel that will be parent to private ones") @channel.command(
name="set",
description="Select the voice channel that will be parent to private ones",
)
@option("channel", description="Parent Voice Channel") @option("channel", description="Parent Voice Channel")
async def channel_set(self, ctx: ApplicationContext, channel: VoiceChannel): async def channel_set(self, ctx: ApplicationContext, channel: VoiceChannel):
config = loadJson("config.json") config = loadJson("config.json")
if ctx.guild is not None: if ctx.guild is not None:
guildConfSet(ctx.guild, "channel", channel.id) 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"]))) 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: 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"]))) 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: else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) 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") @channel.command(
async def channel_reset(self, ctx: ApplicationContext): name="reset", description="Reset the currently selected parent voice channel"
)
async def channel_reset(self, ctx: ApplicationContext):
config = loadJson("config.json") config = loadJson("config.json")
if ctx.guild is not None: if ctx.guild is not None:
if guildConfGet(ctx.guild, "channel") is not None: if guildConfGet(ctx.guild, "channel") is not None:
guildConfReset(ctx.guild, "channel") 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"]))) await ctx.respond(
embed=makeEmbed(
title=getMsg("reset_channel_title", ctx.guild),
description=getMsg("reset_channel_description", ctx.guild),
color=strToColor(config["color_ok"]),
)
)
else: else:
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"]))) 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: else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) await ctx.respond(
#========================================================================================================================= embed=makeEmbed(
title=getMsg("dm_title", ctx.guild),
description=getMsg("dm_description", ctx.guild),
color=strToColor(config["color_error"]),
)
)
# =========================================================================================================================

View File

@ -1,11 +1,20 @@
from os import listdir from os import listdir
from discord import ApplicationContext, Option, SlashCommandGroup from discord import ApplicationContext, Option, SlashCommandGroup
from discord.ext import commands from discord.ext import commands
from functions import appendLog, getMsg, guildConfGet, guildConfReset, guildConfSet, loadJson, makeEmbed, strToColor from functions import (
appendLog,
getMsg,
guildConfGet,
guildConfReset,
guildConfSet,
loadJson,
makeEmbed,
strToColor,
)
#=========================================================================================================================
# =========================================================================================================================
class CogLocale(commands.Cog): class CogLocale(commands.Cog):
def __init__(self, client): def __init__(self, client):
self.client = client self.client = client
@ -17,32 +26,78 @@ class CogLocale(commands.Cog):
valid_locales.append(".".join(entry.split(".")[:-1])) valid_locales.append(".".join(entry.split(".")[:-1]))
@locale.command(name="set", description="Set bot's messages language") @locale.command(name="set", description="Set bot's messages language")
async def locale_set(self, ctx: ApplicationContext, language: Option(str, "One of the languages in list", choices=valid_locales)): # type: ignore async def locale_set(self, 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 listdir(f"locale/"): if language + ".json" in listdir(f"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 = listdir(f"locale/") files_locales = listdir(f"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"]),
)
)
else: else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) await ctx.respond(
embed=makeEmbed(
title=getMsg("dm_title", ctx.guild),
description=getMsg("dm_description", ctx.guild),
color=strToColor(config["color_error"]),
)
)
@locale.command(name="reset", description="Reset the bot's language in this guild") @locale.command(name="reset", description="Reset the bot's language in this guild")
async def locale_reset(self, ctx: ApplicationContext): # type: ignore async def locale_reset(self, ctx: ApplicationContext): # type: ignore
config = loadJson("config.json") config = loadJson("config.json")
if ctx.guild is not None: if ctx.guild is not None:
if guildConfGet(ctx.guild, "locale") is not None: if guildConfGet(ctx.guild, "locale") is not None:
guildConfReset(ctx.guild, "locale") guildConfReset(ctx.guild, "locale")
appendLog(f"Server's locale has been reset", ctx.guild) 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"]))) 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"]),
)
)
else: else:
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"]))) 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: else:
await ctx.respond(embed=makeEmbed(title=getMsg("dm_title", ctx.guild), description=getMsg("dm_description", ctx.guild), color=strToColor(config["color_error"]))) await ctx.respond(
#========================================================================================================================= embed=makeEmbed(
title=getMsg("dm_title", ctx.guild),
description=getMsg("dm_description", ctx.guild),
color=strToColor(config["color_error"]),
)
)
# =========================================================================================================================

View File

@ -4,37 +4,48 @@ from discord import ApplicationContext, slash_command, Cog, Guild
from discord.ext import commands from discord.ext import commands
from functions import appendLog, getMsg, loadJson, makeEmbed, saveJson, strToColor from functions import appendLog, getMsg, loadJson, makeEmbed, saveJson, strToColor
#=========================================================================================================================
class CogUtility(commands.Cog):
# =========================================================================================================================
class CogUtility(commands.Cog):
def __init__(self, client): def __init__(self, client):
self.client = client self.client = client
@Cog.listener() @Cog.listener()
async def on_guild_join(self, guild: Guild): async def on_guild_join(self, guild: Guild):
makedirs(f"guilds/{str(guild.id)}", exist_ok=True) makedirs(f"guilds/{str(guild.id)}", exist_ok=True)
makedirs(f"guilds/{str(guild.id)}/channels", exist_ok=True) makedirs(f"guilds/{str(guild.id)}/channels", exist_ok=True)
saveJson({}, f"guilds/{str(guild.id)}/config.json") saveJson({}, f"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)}")
@Cog.listener() @Cog.listener()
async def on_guild_remove(self, guild: Guild): async def on_guild_remove(self, guild: Guild):
try: try:
rmtree(f"guilds/{str(guild.id)}") rmtree(f"guilds/{str(guild.id)}")
except: except:
pass pass
appendLog(f"Left guild '{guild}' with id {str(guild.id)}") appendLog(f"Left guild '{guild}' with id {str(guild.id)}")
@slash_command(name="shutdown", description="Restart the bot") @slash_command(name="shutdown", description="Restart the bot")
async def shutdown(self, ctx: ApplicationContext): async def shutdown(self, 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"]),
)
)
system(f"kill -9 {str(getpid())}") system(f"kill -9 {str(getpid())}")
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"]),
)
)
# =========================================================================================================================

View File

@ -3,7 +3,19 @@ from datetime import datetime
from os import listdir, makedirs, remove, stat, path from os import listdir, makedirs, remove, stat, path
from shutil import copyfileobj from shutil import copyfileobj
from typing import Any, Literal, Union from typing import Any, Literal, Union
from discord import utils, Embed, TextChannel, VoiceChannel, PermissionOverwrite, Guild, CategoryChannel, Member, Client, ApplicationContext, Message from discord import (
utils,
Embed,
TextChannel,
VoiceChannel,
PermissionOverwrite,
Guild,
CategoryChannel,
Member,
Client,
ApplicationContext,
Message,
)
try: try:
from ujson import loads, dumps from ujson import loads, dumps
@ -20,38 +32,42 @@ log_size = 512
debug = False 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 = loads(json_file.read()) output = loads(json_file.read())
json_file.close() json_file.close()
debug = output["debug"] debug = output["debug"]
except: except:
debug = debug debug = debug
# Check latest log size # Check latest log size
def checkSize(): def checkSize():
try: try:
if not path.isdir("logs"): if not path.isdir("logs"):
makedirs("logs", exist_ok=True) makedirs("logs", exist_ok=True)
log = stat("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("logs/latest.log", "rb") as f_in: with open("logs/latest.log", "rb") as f_in:
with gzipopen(f'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:
copyfileobj(f_in, f_out) copyfileobj(f_in, f_out)
print(f'Copied 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("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: str, guild: Union[Guild, None] = None, announce=True) -> None: def appendLog(message: str, guild: Union[Guild, None] = None, announce=True) -> None:
global debug global debug
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}'
else: else:
@ -59,25 +75,27 @@ def appendLog(message: str, guild: Union[Guild, None] = None, announce=True) ->
message_formatted = f'[{datetime.now().strftime("%d.%m.%Y")}] [{datetime.now().strftime("%H:%M:%S")}] [{guild} | {str(guild.id)}] {message}' message_formatted = f'[{datetime.now().strftime("%d.%m.%Y")}] [{datetime.now().strftime("%H:%M:%S")}] [{guild} | {str(guild.id)}] {message}'
else: else:
message_formatted = f'[{datetime.now().strftime("%d.%m.%Y")}] [{datetime.now().strftime("%H:%M:%S")}] [{guild}] {message}' message_formatted = f'[{datetime.now().strftime("%d.%m.%Y")}] [{datetime.now().strftime("%H:%M:%S")}] [{guild}] {message}'
if announce: if announce:
print(message_formatted, flush=True) print(message_formatted, flush=True)
checkSize() checkSize()
log = open('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: Any, filename: str) -> None: 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:
f.write(dumps(value, indent=4, ensure_ascii=False)) f.write(dumps(value, indent=4, ensure_ascii=False))
f.close() f.close()
def loadJson(filename: str) -> Any: 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 = loads(json_file.read()) output = loads(json_file.read())
json_file.close() json_file.close()
except Exception as exp: except Exception as exp:
@ -86,19 +104,26 @@ def loadJson(filename: str) -> Any:
output = {} output = {}
return output return output
def colorToStr(): def colorToStr():
pass pass
def strToColor(string: str) -> int: def strToColor(string: str) -> int:
return int(hex(int(string.replace("#", ""), 16)), 0) return int(hex(int(string.replace("#", ""), 16)), 0)
def gotCommand(message: Message) -> None: 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: Guild, variable: str) -> Any: def guildConfGet(guild: Guild, variable: str) -> Any:
global debug global debug
try: try:
@ -106,9 +131,12 @@ def guildConfGet(guild: Guild, variable: str) -> Any:
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: Guild, variable: str, value: Any) -> None: def guildConfSet(guild: Guild, variable: str, value: Any) -> None:
config = loadJson(f"guilds/{str(guild.id)}/config.json") config = loadJson(f"guilds/{str(guild.id)}/config.json")
config[variable] = value config[variable] = value
@ -120,6 +148,7 @@ def guildConfSet(guild: Guild, variable: str, value: Any) -> None:
saveJson(config, f"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: Guild, variable: str) -> None: def guildConfReset(guild: Guild, variable: str) -> None:
try: try:
config = loadJson(f"guilds/{str(guild.id)}/config.json") config = loadJson(f"guilds/{str(guild.id)}/config.json")
@ -134,6 +163,7 @@ def guildConfReset(guild: Guild, variable: str) -> None:
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: Guild) -> str: def guildLocaleGet(guild: Guild) -> str:
config = loadJson(f"config.json") config = loadJson(f"config.json")
try: try:
@ -145,21 +175,28 @@ def guildLocaleGet(guild: Guild) -> str:
else: else:
return locale return locale
def getMsg(string: str, guild: Union[Guild, None] = None) -> str: def getMsg(string: str, guild: Union[Guild, None] = None) -> str:
try: try:
locale = loadJson(f'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) -> Embed:
embed=Embed(title=title, description=description, color=color) def makeEmbed(title="", description="", footer="", color=0xFFFFFF) -> Embed:
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: int, guild: Guild, type: Literal["Any", "Text", "Voice"] = "Any") -> bool:
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
@ -180,6 +217,7 @@ def channelExists(number: int, guild: Guild, type: Literal["Any", "Text", "Voice
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: int, guild: Guild): def channelGetName(number: int, guild: Guild):
global debug global debug
try: try:
@ -190,6 +228,7 @@ def channelGetName(number: int, guild: Guild):
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: VoiceChannel) -> bool: def isUserVoice(vc: VoiceChannel) -> bool:
try: try:
channels_list = listdir(f"guilds/{str(vc.guild.id)}/channels/") channels_list = listdir(f"guilds/{str(vc.guild.id)}/channels/")
@ -199,68 +238,116 @@ def isUserVoice(vc: VoiceChannel) -> bool:
return False return False
except: except:
return False return False
async def removeUserVoice(vc: VoiceChannel) -> None: async def removeUserVoice(vc: VoiceChannel) -> None:
global debug global debug
channels_list = listdir(f"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"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 = 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 = utils.get(vc.guild.channels, id=vc_conf["nomic"]) nomic_channel = utils.get(vc.guild.channels, id=vc_conf["nomic"])
remove(vc_file) remove(vc_file)
await needed_channel.delete() await needed_channel.delete()
if debug: if debug:
appendLog(f"Removed voice channel '{needed_channel}' ({str(needed_channel.id)}) of user with id {str(vc_conf['ownerid'])}", guild=vc.guild) appendLog(
f"Removed voice channel '{needed_channel}' ({str(needed_channel.id)}) of user with id {str(vc_conf['ownerid'])}",
guild=vc.guild,
)
else: else:
appendLog(f"Removed voice channel '{needed_channel}' of user with id {str(vc_conf['ownerid'])}", guild=vc.guild) appendLog(
f"Removed voice channel '{needed_channel}' of user with id {str(vc_conf['ownerid'])}",
guild=vc.guild,
)
if loadJson("config.json")["enable_nomic"]: if loadJson("config.json")["enable_nomic"]:
await nomic_channel.delete() await nomic_channel.delete()
if debug: if debug:
appendLog(f"Removed nomic channel {nomic_channel} ({str(nomic_channel.id)}) of channel with id {str(needed_channel.id)}", guild=vc.guild) appendLog(
f"Removed nomic channel {nomic_channel} ({str(nomic_channel.id)}) of channel with id {str(needed_channel.id)}",
guild=vc.guild,
)
else: else:
appendLog(f"Removed nomic channel '{nomic_channel}' of channel with id {str(needed_channel.id)}", guild=vc.guild) appendLog(
f"Removed nomic channel '{nomic_channel}' of channel with id {str(needed_channel.id)}",
guild=vc.guild,
)
else: else:
return return
async def createUserVoice(vc: VoiceChannel, category: CategoryChannel, member: Member) -> VoiceChannel:
async def createUserVoice(
vc: VoiceChannel, category: CategoryChannel, member: Member
) -> VoiceChannel:
global debug global debug
chan = {} chan = {}
overwrites_channel = { overwrites_channel = {
vc.guild.default_role: PermissionOverwrite(view_channel=True), vc.guild.default_role: PermissionOverwrite(view_channel=True),
vc.guild.me: PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True), vc.guild.me: PermissionOverwrite(
member: PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True) 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: PermissionOverwrite(view_channel=False, read_messages=False), vc.guild.default_role: PermissionOverwrite(
vc.guild.me: PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True), view_channel=False, read_messages=False
member: PermissionOverwrite(read_messages=True, view_channel=True, manage_channels=True) ),
vc.guild.me: 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 path.isdir(f"guilds/{str(created_channel.guild.id)}/channels"): if not path.isdir(f"guilds/{str(created_channel.guild.id)}/channels"):
makedirs(f"guilds/{str(created_channel.guild.id)}/channels", exist_ok=True) makedirs(f"guilds/{str(created_channel.guild.id)}/channels", exist_ok=True)
vc_file = f"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"]:
nomic_channel = await vc.guild.create_text_channel(getMsg("name_nomic", vc.guild).format(created_channel.id), category=category, overwrites=overwrites_nomic, topic=getMsg("description_nomic", vc.guild).format(str(created_channel.id))) nomic_channel = await vc.guild.create_text_channel(
getMsg("name_nomic", vc.guild).format(created_channel.id),
category=category,
overwrites=overwrites_nomic,
topic=getMsg("description_nomic", vc.guild).format(str(created_channel.id)),
)
if debug: if debug:
appendLog(f"Created nomic channel '{nomic_channel}' ({str(nomic_channel.id)}) for channel '{created_channel}' ({str(created_channel.id)})", guild=vc.guild) appendLog(
f"Created nomic channel '{nomic_channel}' ({str(nomic_channel.id)}) for channel '{created_channel}' ({str(created_channel.id)})",
guild=vc.guild,
)
else: else:
appendLog(f"Created nomic channel '{nomic_channel}' for channel '{created_channel}'", guild=vc.guild) appendLog(
f"Created nomic channel '{nomic_channel}' for channel '{created_channel}'",
guild=vc.guild,
)
chan["nomic"] = nomic_channel.id chan["nomic"] = nomic_channel.id
saveJson(chan, vc_file) saveJson(chan, vc_file)
return created_channel return created_channel
def isVoiceOfUser(vc: VoiceChannel, member: Member) -> bool: def isVoiceOfUser(vc: VoiceChannel, member: Member) -> bool:
vc_file = f"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)
@ -269,7 +356,10 @@ def isVoiceOfUser(vc: VoiceChannel, member: Member) -> bool:
else: else:
return False return False
async def changeNomicPerms(mode: Literal["deny", "allow"], vc: VoiceChannel, member: Member) -> None:
async def changeNomicPerms(
mode: Literal["deny", "allow"], vc: VoiceChannel, member: Member
) -> None:
vc_file = f"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)
if loadJson("config.json")["enable_nomic"]: if loadJson("config.json")["enable_nomic"]:
@ -279,6 +369,7 @@ async def changeNomicPerms(mode: Literal["deny", "allow"], vc: VoiceChannel, mem
else: else:
await nomic_channel.set_permissions(member, view_channel=True) await nomic_channel.set_permissions(member, view_channel=True)
async def clearTrash(client: Client): async def clearTrash(client: Client):
if not path.isdir(f"guilds/"): if not path.isdir(f"guilds/"):
makedirs(f"guilds", exist_ok=True) makedirs(f"guilds", exist_ok=True)
@ -290,8 +381,12 @@ async def clearTrash(client: Client):
for channel in channels_list: for channel in channels_list:
channel_id = channel[:-5] channel_id = channel[:-5]
try: try:
selected_channel = utils.get(guild_object.voice_channels, id=int(channel_id)) selected_channel = utils.get(
channel_owner = loadJson(f"guilds/{guild}/channels/{channel}")["ownerid"] guild_object.voice_channels, id=int(channel_id)
)
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:
@ -301,52 +396,82 @@ async def clearTrash(client: Client):
except: except:
remove(f"guilds/{guild}/channels/{channel_id}.json") remove(f"guilds/{guild}/channels/{channel_id}.json")
#async def autoClearTrash(client):
# execute clearTrash every 120 seconds # async def autoClearTrash(client):
# execute clearTrash every 120 seconds
def getHelpMessage(ctx: ApplicationContext, version: float) -> Embed: def getHelpMessage(ctx: ApplicationContext, version: float) -> Embed:
# channelExists(number, guild, type="Voice")
#channelExists(number, guild, type="Voice")
config = loadJson("config.json") config = loadJson("config.json")
if ctx.guild is not None: if ctx.guild is not None:
if channelExists(guildConfGet(ctx.guild, 'channel'), ctx.guild, type="Voice"): if channelExists(guildConfGet(ctx.guild, "channel"), ctx.guild, type="Voice"):
desc_channel = getMsg("help_channel_set", guild=ctx.guild).format(channelGetName(guildConfGet(ctx.guild, 'channel'), ctx.guild)) desc_channel = getMsg("help_channel_set", guild=ctx.guild).format(
channelGetName(guildConfGet(ctx.guild, "channel"), ctx.guild)
)
else: else:
desc_channel = getMsg("help_channel_none", guild=ctx.guild) desc_channel = getMsg("help_channel_none", guild=ctx.guild)
if channelExists(guildConfGet(ctx.guild, 'category'), ctx.guild, type="Any"): if channelExists(guildConfGet(ctx.guild, "category"), ctx.guild, type="Any"):
desc_category = getMsg("help_category_set", guild=ctx.guild).format(channelGetName(guildConfGet(ctx.guild, 'category'), ctx.guild)) desc_category = getMsg("help_category_set", guild=ctx.guild).format(
channelGetName(guildConfGet(ctx.guild, "category"), ctx.guild)
)
else: else:
desc_category = getMsg("help_category_none", guild=ctx.guild) desc_category = getMsg("help_category_none", guild=ctx.guild)
desc_locale = getMsg("help_locale", guild=ctx.guild).format(getMsg("locale_name", ctx.guild)) desc_locale = getMsg("help_locale", guild=ctx.guild).format(
getMsg("locale_name", ctx.guild)
)
description = "\n".join([desc_locale, desc_channel, desc_category]) description = "\n".join([desc_locale, desc_channel, desc_category])
embed=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=Embed(title=getMsg("help_title_dm", ctx.guild), color=strToColor(config["color_default"])) embed = Embed(
title=getMsg("help_title_dm", ctx.guild),
embed.set_author(name=f'{config["bot_name"]} v{str(version)}', url=config["bot_site"], icon_url=config["bot_icon"]) 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"],
)
if ctx.author.id == config["owner"]: if ctx.author.id == config["owner"]:
embed.add_field(name=f"/shutdown", value=getMsg("help_cmd_shutdown", ctx.guild), inline=False) embed.add_field(
name=f"/shutdown",
embed.add_field(name=f"/channel set", value=getMsg("help_cmd_channel", ctx.guild), inline=False) value=getMsg("help_cmd_shutdown", ctx.guild),
embed.add_field(name=f"/category set", value=getMsg("help_cmd_category", ctx.guild), inline=False) inline=False,
embed.add_field(name=f"/locale set", value=getMsg("help_cmd_locale", ctx.guild), inline=False) )
embed.add_field(
name=f"/channel set", value=getMsg("help_cmd_channel", ctx.guild), inline=False
)
embed.add_field(
name=f"/category set",
value=getMsg("help_cmd_category", ctx.guild),
inline=False,
)
embed.add_field(
name=f"/locale set", value=getMsg("help_cmd_locale", ctx.guild), inline=False
)
if ctx.guild is None: if ctx.guild is None:
embed.set_footer(text=getMsg("help_server", ctx.guild)) embed.set_footer(text=getMsg("help_server", ctx.guild))
else: else:
embed.set_footer(text=getMsg("help_notice_id", ctx.guild)) embed.set_footer(text=getMsg("help_notice_id", ctx.guild))
return embed return embed
async def guildConfigured(guild: Guild) -> str:
async def guildConfigured(guild: Guild) -> str:
output = {} output = {}
config = loadJson("config.json") config = loadJson("config.json")
@ -354,10 +479,16 @@ async def guildConfigured(guild: Guild) -> str:
if guildConfGet(guild, kind) is not None: if guildConfGet(guild, kind) is not None:
try: try:
guild_object = 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)
else: else:
output[kind] = getMsg("unconfigured_"+kind, guild) output[kind] = getMsg("unconfigured_" + kind, guild)
return getMsg("server_config", guild).format(getMsg("info_locale", guild).format(getMsg("locale_name", guild)), output["channel"], output["category"]) return getMsg("server_config", guild).format(
getMsg("info_locale", guild).format(getMsg("locale_name", guild)),
output["channel"],
output["category"],
)

View File

@ -1,8 +1,17 @@
try: try:
from requests import get from requests import get
from discord import ApplicationContext, Intents, Bot, ActivityType, Activity, VoiceState from discord import (
ApplicationContext,
Intents,
Bot,
ActivityType,
Activity,
VoiceState,
)
except Exception as exp: except Exception as exp:
print(f"Dependencies 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"
)
exit() exit()
from os import getpid from os import getpid
@ -12,18 +21,29 @@ from functions import *
pid = getpid() pid = getpid()
version = 1.8 version = 1.8
if loadJson("config.json")["owner"] == "SET-OWNER-ID" or loadJson("config.json")["bot_token"] == "SET-BOT-TOKEN": if (
print(f"Bot is not correctly configured.\nMake sure you've set up owner id and bot token in 'config.json'\nLearn more here: https://git.end-play.xyz/profitroll/YusarinBot") 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 'config.json'\nLearn more here: https://git.end-play.xyz/profitroll/YusarinBot"
)
exit() exit()
if loadJson("config.json")["check_for_updates"]: if loadJson("config.json")["check_for_updates"]:
try: try:
serv_ver = loads(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)}")
except Exception as exp: except Exception as exp:
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 = Bot(intents=intents) client = Bot(intents=intents)
@ -33,25 +53,29 @@ from cogs.cogChannel import CogChannel
from cogs.cogLocale import CogLocale from cogs.cogLocale import CogLocale
from cogs.cogUtility import CogUtility from cogs.cogUtility import CogUtility
@client.slash_command(name="help", description="Get information about this server") @client.slash_command(name="help", description="Get information about this server")
async def help(ctx: ApplicationContext): async def help(ctx: ApplicationContext):
await ctx.respond(embed=getHelpMessage(ctx, version)) await ctx.respond(embed=getHelpMessage(ctx, version))
@client.event @client.event
async def on_ready(): async def on_ready():
appendLog(f"Logged in as {client.user}") appendLog(f"Logged in as {client.user}")
config = loadJson("config.json") config = loadJson("config.json")
await client.change_presence(activity=Activity(type=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)
client.add_cog(CogCategory(client)) client.add_cog(CogCategory(client))
client.add_cog(CogChannel(client)) client.add_cog(CogChannel(client))
client.add_cog(CogLocale(client)) client.add_cog(CogLocale(client))
client.add_cog(CogUtility(client)) client.add_cog(CogUtility(client))
appendLog(f"Trying to log in...") appendLog(f"Trying to log in...")
client.run(loadJson("config.json")["bot_token"]) client.run(loadJson("config.json")["bot_token"])