This repository has been archived on 2024-10-14. You can view files and clone it, but cannot push or open issues or pull requests.
AutoZoomDiscord/bot.py

164 lines
7.1 KiB
Python
Raw Normal View History

2022-09-14 14:02:06 +03:00
# -*- coding: utf-8 -*-
2022-09-14 13:48:58 +03:00
import discord
from sys import exit
from discord import Embed
from modules.functions import *
from modules.functions_bot import *
2022-09-15 01:20:38 +03:00
from os import sep, listdir
2022-09-14 13:48:58 +03:00
intents = discord.Intents().all()
client = discord.Bot(intents=intents)
2022-09-15 14:05:44 +03:00
def makeEmbed(title="", description="", footer="", color=0xffffff) -> discord.Embed:
2022-09-14 13:48:58 +03:00
embed=Embed(title=title, description=description, color=color)
if footer is not None:
embed.set_footer(text=footer)
return embed
2022-09-15 14:05:44 +03:00
2022-09-14 13:48:58 +03:00
@client.event
async def on_ready():
print(f"Logged in as {client.user}")
2022-09-15 14:05:44 +03:00
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)
2022-09-14 13:48:58 +03:00
2022-09-15 14:05:44 +03:00
data = configGet("data")
2022-09-14 13:48:58 +03:00
2022-09-15 14:05:44 +03:00
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}} )
2022-09-14 13:48:58 +03:00
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)
2022-09-15 14:05:44 +03:00
keys_storage = jsonLoad(f"{data}{sep}keys_storage.json")
2022-09-14 13:48:58 +03:00
keys_storage[code] = ctx.author.id
2022-09-15 14:05:44 +03:00
jsonSave(f"{data}{sep}keys_storage.json", keys_storage)
logWrite(f"Added apikey {code} for user {ctx.author.id}", logs_folder=configGet("logs"))
2022-09-15 14:50:19 +03:00
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))
2022-09-14 13:48:58 +03:00
else:
2022-09-15 14:05:44 +03:00
logWrite(f"User {ctx.author.id} tried to pair with invalid apikey {code}", logs_folder=configGet("logs"))
2022-09-14 13:48:58 +03:00
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))
2022-09-15 14:05:44 +03:00
@client.slash_command(name="unlink",
description=locale("description", "cmd", "unlink", locale=configGet("locale")),
name_localizations=localeName("unlink", None),
description_localizations=localeDescription("unlink", None)
)
2022-09-14 13:48:58 +03:00
async def unlink(ctx: discord.ApplicationContext):
2022-09-15 14:05:44 +03:00
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")
2022-09-14 13:48:58 +03:00
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:
2022-09-15 14:05:44 +03:00
keys_storage = jsonLoad(f"{data}{sep}keys_storage.json")
2022-09-14 13:48:58 +03:00
del keys_storage[userGet(ctx.author.id, "api_key")]
2022-09-15 14:05:44 +03:00
jsonSave(f"{data}{sep}keys_storage.json", keys_storage)
2022-09-14 13:48:58 +03:00
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))
2022-09-15 14:05:44 +03:00
@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)
2022-09-14 13:48:58 +03:00
2022-09-15 14:05:44 +03:00
data = configGet("data")
2022-09-14 13:48:58 +03:00
return
if configGet("token") != "INSERT-TOKEN":
client.run(configGet("token"))
else:
2022-09-15 14:05:44 +03:00
logWrite("Could not start the bot. Please, configure token in config.json", logs_folder=configGet("logs"))
2022-09-14 13:48:58 +03:00
exit()