104 lines
3.6 KiB
Python
104 lines
3.6 KiB
Python
import nextcord
|
|
from nextcord.ext import commands, application_checks
|
|
|
|
import tiss
|
|
|
|
cache = {}
|
|
|
|
def run(token: str, config: dict):
|
|
bot = commands.Bot()
|
|
|
|
def get_channel(id):
|
|
global cache
|
|
if id not in cache:
|
|
cache[id] = bot.get_channel(id)
|
|
|
|
return cache[id]
|
|
|
|
|
|
@bot.slash_command(description="Manager master channels")
|
|
@application_checks.has_guild_permissions(administrator=True)
|
|
async def master(
|
|
interaction: nextcord.Interaction,
|
|
):
|
|
pass
|
|
|
|
@master.subcommand(description="Sync Master LVA with Channel")
|
|
@application_checks.has_guild_permissions(administrator=True)
|
|
async def sync(
|
|
interaction: nextcord.Interaction,
|
|
link: str = nextcord.SlashOption(description="Enter TISS link")
|
|
):
|
|
lva_code = tiss.TISS.get_code_from_any(link)
|
|
|
|
if lva_code == None:
|
|
await interaction.send("Invalid TISS link", ephemeral=True)
|
|
return
|
|
|
|
try:
|
|
lva = tiss.TISS.get_lva(lva_code)
|
|
except:
|
|
await interaction.send("Could not fetch TISS data", ephemeral=True)
|
|
return
|
|
|
|
if lva is None:
|
|
await interaction.send("Could not fetch TISS data", ephemeral=True)
|
|
return
|
|
|
|
forum = get_channel(config["channels"]["forum"])
|
|
|
|
# Get forum data
|
|
# Get tags
|
|
tags_available = forum.available_tags
|
|
thread_tags = []
|
|
|
|
config_curricula = config["curricula"]
|
|
# This should work? but doesn't somehow
|
|
# for curriculum in lva.curricula:
|
|
# if curriculum in config_curricula:
|
|
# print(f"Curriculum found: {curriculum}")
|
|
# try:
|
|
# print(f"Looking for id {config_curricula[curriculum]}")
|
|
# tags.append(forum.get_tag(id = config_curricula[curriculum]))
|
|
# print("Tag found")
|
|
# except:
|
|
# print(f"Tag not found for curriculum {curriculum}")
|
|
# else:
|
|
# print(f"Curriculum not found: {curriculum}")
|
|
|
|
for curriculum in config_curricula:
|
|
if curriculum in lva.curricula:
|
|
# Find the tag in the available tags
|
|
tag = next(x for x in tags_available if x.id == config_curricula[curriculum])
|
|
if tag is not None:
|
|
thread_tags.append(tag)
|
|
|
|
# Get name
|
|
thread_name = f"{lva.type} {lva.name} ({lva.id_pretty()})"
|
|
|
|
# Get content
|
|
thread_content = tiss.TISS.get_anonymous_tiss_link(lva.id)
|
|
|
|
# Determine if the thread already exists
|
|
for thread in forum.threads:
|
|
# Does the thread name contain the LVA code?
|
|
if lva.id_pretty() in thread.name:
|
|
await thread.edit(name = thread_name, applied_tags = thread_tags)
|
|
|
|
message = await thread.fetch_message(thread.id)
|
|
await message.edit(content = thread_content)
|
|
await message.pin()
|
|
|
|
await interaction.send(f"Existing forum thread has been synced {thread.jump_url}", ephemeral=False)
|
|
return
|
|
|
|
# If it doesn't exist, create a new thread
|
|
thread = await forum.create_thread(name = thread_name, content = thread_content, applied_tags = thread_tags)
|
|
message = await thread.fetch_message(thread.id)
|
|
await message.pin()
|
|
|
|
reply_message = f"Forum channel created! {thread.jump_url}{" No tags were found, are you sure this was a Master LVA?" if len(thread_tags) == 0 else ""}"
|
|
await interaction.send(reply_message, ephemeral=False)
|
|
|
|
bot.run(token)
|