2024-01-22 22:02:13 +01:00
|
|
|
#!/bin/env python3
|
|
|
|
from typing import Literal, Optional
|
|
|
|
from emoji import emojize
|
|
|
|
from yaml import safe_load
|
|
|
|
|
|
|
|
import discord
|
|
|
|
from discord.ext import commands
|
|
|
|
from discord.ext.commands import Greedy, Context
|
2024-01-23 17:42:10 +01:00
|
|
|
from discord import app_commands, Embed
|
2024-01-22 22:02:13 +01:00
|
|
|
|
|
|
|
from src.roles import AddRoleButton, RemoveRoleButton
|
2024-01-23 12:09:51 +01:00
|
|
|
from src.ui import RoleSelectView
|
2024-01-22 22:02:13 +01:00
|
|
|
|
|
|
|
class Bot(commands.Bot):
|
2024-01-23 12:09:51 +01:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__(command_prefix=",", intents=discord.Intents.default())
|
|
|
|
self.intents.message_content = True
|
|
|
|
|
|
|
|
async def setup_hook(self) -> None:
|
|
|
|
self.add_view(RoleSelectView())
|
|
|
|
return await super().setup_hook()
|
|
|
|
|
2024-01-22 22:02:13 +01:00
|
|
|
with open('config.yaml', 'r') as file:
|
|
|
|
config = safe_load(file)
|
|
|
|
|
|
|
|
# Bot
|
2024-01-23 12:09:51 +01:00
|
|
|
client = Bot()
|
2024-01-22 22:02:13 +01:00
|
|
|
|
|
|
|
# Command Groups
|
|
|
|
roles = app_commands.Group(name="roles", description="Manage role dialogues")
|
|
|
|
|
|
|
|
# Commands
|
|
|
|
## Group: roles
|
|
|
|
@roles.command(name="create")
|
|
|
|
async def roles_create_button(interaction: discord.Interaction):
|
2024-01-23 17:42:10 +01:00
|
|
|
await interaction.response.send_message(
|
|
|
|
view=RoleSelectView(),
|
|
|
|
embed=Embed(
|
|
|
|
title="Available Roles:",
|
|
|
|
description="\n".join([
|
|
|
|
f"{interaction.client.get_emoji(role['emoji'])} {interaction.guild.get_role(role['id']).mention}"
|
|
|
|
for role in interaction.client.config['roles']
|
|
|
|
])
|
|
|
|
)
|
|
|
|
)
|
2024-01-22 22:02:13 +01:00
|
|
|
|
|
|
|
# Sync using ,sync ~
|
|
|
|
@client.command()
|
|
|
|
@commands.guild_only()
|
|
|
|
@commands.is_owner()
|
|
|
|
async def sync(
|
|
|
|
ctx: Context, guilds: Greedy[discord.Object], spec: Optional[Literal["~", "*", "^"]] = None) -> None:
|
|
|
|
if not guilds:
|
|
|
|
if spec == "~":
|
|
|
|
synced = await ctx.bot.tree.sync(guild=ctx.guild)
|
|
|
|
elif spec == "*":
|
|
|
|
ctx.bot.tree.copy_global_to(guild=ctx.guild)
|
|
|
|
synced = await ctx.bot.tree.sync(guild=ctx.guild)
|
|
|
|
elif spec == "^":
|
|
|
|
ctx.bot.tree.clear_commands(guild=ctx.guild)
|
|
|
|
await ctx.bot.tree.sync(guild=ctx.guild)
|
|
|
|
synced = []
|
|
|
|
else:
|
|
|
|
synced = await ctx.bot.tree.sync()
|
|
|
|
|
|
|
|
await ctx.send(
|
|
|
|
f"Synced {len(synced)} commands {'globally' if spec is None else 'to the current guild.'}"
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
|
|
|
ret = 0
|
|
|
|
for guild in guilds:
|
|
|
|
try:
|
|
|
|
await ctx.bot.tree.sync(guild=guild)
|
|
|
|
except discord.HTTPException:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
ret += 1
|
|
|
|
|
|
|
|
await ctx.send(f"Synced the tree to {ret}/{len(guilds)}.")
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
client.tree.add_command(roles)
|
|
|
|
client.run(client.config['auth']['token'])
|