salzarbeiter/main.py

86 lines
2.6 KiB
Python
Raw Normal View History

#!/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
from discord import app_commands, Embed
from src.roles import AddRoleButton, RemoveRoleButton
from src.ui import RoleSelectView
class Bot(commands.Bot):
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()
with open('config.yaml', 'r') as file:
config = safe_load(file)
# Bot
client = Bot()
# Command Groups
roles = app_commands.Group(name="roles", description="Manage role dialogues")
# Commands
## Group: roles
@commands.guild_only()
@roles.command(name="create")
async def roles_create_button(interaction: discord.Interaction):
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']
])
)
)
# 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'])