AutoZoomDiscord/bot.py

164 lines
7.1 KiB
Python

# -*- coding: utf-8 -*-
import discord
from sys import exit
from discord import Embed
from modules.functions import *
from modules.functions_bot import *
from os import sep, listdir
intents = discord.Intents().all()
client = discord.Bot(intents=intents)
def makeEmbed(title="", description="", footer="", color=0xffffff) -> discord.Embed:
embed=Embed(title=title, description=description, color=color)
if footer is not None:
embed.set_footer(text=footer)
return embed
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name=configGet("activity")))
@client.slash_command(name="link",
description=locale("description", "cmd", "link", locale=configGet("locale")),
name_localizations=localeName("link", None),
description_localizations=localeDescription("link", None)
)
async def link(ctx: discord.ApplicationContext,
code:
discord.Option(str,
locale("description", "cmd", "link", "options", "code", locale=configGet("locale")),
required=True,
name_localizations=localeName("link", "code"),
description_localizations=localeDescription("link", "code"),
)
):
logWrite(f'Got command start/link from {ctx.author.id}', logs_folder=configGet("logs"))
if isinstance(ctx.channel, discord.DMChannel) or isinstance(ctx.channel, discord.channel.PartialMessageable):
await ctx.defer()
else:
await ctx.defer(ephemeral=True)
data = configGet("data")
if f"{ctx.author.id}.json" not in listdir(f"{data}{sep}users{sep}"):
logWrite(f'Creating blank data file for {ctx.author.id}', logs_folder=configGet("logs"))
jsonSave( f"{data}{sep}users{sep}{ctx.author.id}.json", {"api_key": None, "linked": False, "context": {"action": None, "data": None}} )
if not userGet(ctx.author.id, "linked"):
if code in jsonLoad(configGet("api_keys"))["autozoom"]:
await ctx.respond(embed=makeEmbed(title=locale("key_correct", "msg"), description=locale("key_correct_text", "msg"), color=0x45d352))
userSet(ctx.author.id, "api_key", code)
userSet(ctx.author.id, "linked", True)
keys_storage = jsonLoad(f"{data}{sep}keys_storage.json")
keys_storage[code] = ctx.author.id
jsonSave(f"{data}{sep}keys_storage.json", keys_storage)
logWrite(f"Added apikey {code} for user {ctx.author.id}", logs_folder=configGet("logs"))
elif code in jsonLoad(configGet("expired_keys")):
logWrite(f"User {ctx.author.id} tried to pair with expired apikey {code}", logs_folder=configGet("logs"))
await ctx.respond(embed=makeEmbed(title=locale("key_expired", "msg"), description=locale("key_expired_text", "msg"), color=0xe06044))
else:
logWrite(f"User {ctx.author.id} tried to pair with invalid apikey {code}", logs_folder=configGet("logs"))
await ctx.respond(embed=makeEmbed(title=locale("key_wrong", "msg"), description=locale("key_wrong_text", "msg"), color=0xe06044))
else:
await ctx.respond(embed=makeEmbed(title=locale("already_linked", "msg"), description=locale("already_linked_text", "msg"), color=0xe06044))
@client.slash_command(name="unlink",
description=locale("description", "cmd", "unlink", locale=configGet("locale")),
name_localizations=localeName("unlink", None),
description_localizations=localeDescription("unlink", None)
)
async def unlink(ctx: discord.ApplicationContext):
logWrite(f'Got command unlink from {ctx.author.id}', logs_folder=configGet("logs"))
if isinstance(ctx.channel, discord.DMChannel) or isinstance(ctx.channel, discord.channel.PartialMessageable):
await ctx.defer()
else:
await ctx.defer(ephemeral=True)
data = configGet("data")
if not userGet(ctx.author.id, "linked"):
await ctx.respond(embed=makeEmbed(title=locale("not_linked", "msg"), description=locale("not_linked_text", "msg"), color=0xe06044))
else:
try:
keys_storage = jsonLoad(f"{data}{sep}keys_storage.json")
del keys_storage[userGet(ctx.author.id, "api_key")]
jsonSave(f"{data}{sep}keys_storage.json", keys_storage)
except:
pass
userClear(ctx.author.id, "api_key")
userSet(ctx.author.id, "linked", False)
await ctx.respond(embed=makeEmbed(title=locale("unlinked", "msg"), description=locale("unlinked_text", "msg"), color=0x45d352))
@client.slash_command(name="meeting",
description=locale("description", "cmd", "meeting", locale=configGet("locale")),
name_localizations=localeName("meeting", None),
description_localizations=localeDescription("meeting", None)
)
async def meeting(ctx: discord.ApplicationContext,
title: discord.Option(str,
locale("description", "cmd", "meeting", "options", "title", locale=configGet("locale")),
required=True,
name_localizations=localeName("meeting", "title"),
description_localizations=localeDescription("meeting", "title")
),
day: discord.Option(str,
locale("description", "cmd", "meeting", "options", "day", locale=configGet("locale")),
required=True,
name_localizations=localeName("meeting", "day"),
description_localizations=localeDescription("meeting", "day")
),
time: discord.Option(str,
locale("description", "cmd", "meeting", "options", "time", locale=configGet("locale")),
required=True,
name_localizations=localeName("meeting", "time"),
description_localizations=localeDescription("meeting", "time")
),
link: discord.Option(str,
locale("description", "cmd", "meeting", "options", "link", locale=configGet("locale")),
required=True,
name_localizations=localeName("meeting", "link"),
description_localizations=localeDescription("meeting", "link")
),
repeat: discord.Option(bool,
locale("description", "cmd", "meeting", "options", "repeat", locale=configGet("locale")),
required=True,
name_localizations=localeName("meeting", "repeat"),
description_localizations=localeDescription("meeting", "repeat")
),
record: discord.Option(bool,
locale("description", "cmd", "meeting", "options", "record", locale=configGet("locale")),
required=True,
name_localizations=localeName("meeting", "record"),
description_localizations=localeDescription("meeting", "record")
)
):
logWrite(f'Got command meeting from {ctx.author.id}', logs_folder=configGet("logs"))
if isinstance(ctx.channel, discord.DMChannel) or isinstance(ctx.channel, discord.channel.PartialMessageable):
await ctx.defer()
else:
await ctx.defer(ephemeral=True)
data = configGet("data")
return
if configGet("token") != "INSERT-TOKEN":
client.run(configGet("token"))
else:
logWrite("Could not start the bot. Please, configure token in config.json", logs_folder=configGet("logs"))
exit()