AutoZoomDiscord/bot.py

78 lines
3.7 KiB
Python

import os
import discord
from sys import exit
from discord import Embed
from modules.functions import *
from modules.functions_bot import *
intents = discord.Intents().all()
client = discord.Bot(intents=intents)
def makeEmbed(title="", description="", footer="", color=0xffffff):
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="end-play.xyz/autozoom"))
@client.slash_command(name="link", description="Connect to your AutoZoom")
async def link(ctx: discord.ApplicationContext, code: discord.Option(str, "Code you got in AutoZoom app", required=True)):
logWrite(f'Got command start/link from {ctx.author.id}')
if f"{ctx.author.id}.json" not in os.listdir("data/users/"):
logWrite(f'Creating blank data file for {ctx.author.id}')
jsonSave( f"data/users/{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("data/keys_storage.json")
keys_storage[code] = ctx.author.id
jsonSave("data/keys_storage.json", keys_storage)
logWrite(f"Added apikey {code} for user {ctx.author.id}")
else:
logWrite(f"User {ctx.author.id} tried to pair with invalid apikey {code}")
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="Disconnect from your AutoZoom")
async def unlink(ctx: discord.ApplicationContext):
logWrite(f'Got command ulink from {ctx.author.id}')
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("data/keys_storage.json")
del keys_storage[userGet(ctx.author.id, "api_key")]
jsonSave("data/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="Add new Zoom meeting")
async def meeting(ctx: discord.ApplicationContext, title: discord.Option(str, "Meeting title", required=True), day: discord.Option(str, "Day formatted as dd.mm.yyyy", required=True), time: discord.Option(str, "Time formatted as hh:mm", required=True), link: discord.Option(str, "Direct meeting link", required=True), repeat: discord.Option(bool, "Repeat meeting this weekday", required=True), record: discord.Option(bool, "Record meeting using app", required=True)):
logWrite(f'Got command meeting from {ctx.author.id}')
return
if configGet("token") != "INSERT-TOKEN":
client.run(configGet("token"))
else:
logWrite("Could not start the bot. Please, configure token in config.json")
exit()