salzarbeiter/main.py

85 lines
2.6 KiB
Python
Executable File

#!/bin/env python3
from typing import Literal, Optional
from yaml import safe_load
import os
import discord
from discord.ext import commands
from discord.ext.commands import Greedy, Context
from discord import app_commands
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()
for loc in os.curdir, "/etc/salzarbeiter", os.environ.get("CFG_FILE"):
try:
with open(os.path.join(loc, "config.yaml"), "r") as file:
config = safe_load(file)
except:
pass
# Bot
client = Bot()
# Command Groups
roles = app_commands.Group(name="roles", description="Manage role dialogues")
# Commands
## Group: roles
@commands.guild_only()
@commands.has_permissions(administrator=True)
@roles.command(name="create")
async def roles_create_button(interaction: discord.Interaction):
await interaction.response.send_message(view=RoleSelectView())
# Sync using ,sync [~, *, ^]
# https://about.abstractumbra.dev/discord.py/2023/01/29/sync-command-example.html
# ~ -> Current guild
# * -> Global
# ^ -> Clear global tree, sync to current guild
@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'])