ari/ari/bot.py

79 lines
2.5 KiB
Python

import logging
from typing import Dict
import discord
from discord import app_commands
from .player import Player
from .constants import CACHE_SIZE, MUSIC_CACHE, Emoji
from .messages import MessageHandler
from .cache import Cache
discord.utils.setup_logging()
logging.getLogger("ari").setLevel(logging.INFO)
logger = logging.getLogger(__name__)
intents = discord.Intents.default()
intents.message_content = True
class Bot(discord.Client):
def __init__(self, intents: discord.Intents) -> None:
super().__init__(intents=intents)
self.tree = app_commands.CommandTree(self)
self.message_handler = MessageHandler(self)
self.music_cache = Cache(self.http, MUSIC_CACHE, max_size=CACHE_SIZE)
self.players: Dict[int, Player] = {}
async def setup_hook(self) -> None:
return
await self.tree.sync()
async def on_message(self, message: discord.Message):
await self.message_handler.handle_message(message)
async def on_raw_reaction_add(self, payload: discord.RawReactionActionEvent):
await self.message_handler.handle_reaction_add(payload)
bot = Bot(intents)
@bot.tree.command(name="skip", description="Skip specified number of songs")
@app_commands.describe(number="Number of songs to skip")
@app_commands.guild_only()
async def skip(interaction: discord.Interaction, number: int = 1):
assert interaction.guild is not None
if number < 1:
await interaction.response.send_message(
f"{Emoji.error} The number of songs to skip must be bigger or equal to 1",
ephemeral=True,
)
return
player = bot.players.get(interaction.guild.id)
if player is None or not player.is_running():
await interaction.response.send_message(
f"{Emoji.error} I am not currently playing anything", ephemeral=True
)
else:
player.skip(number)
await interaction.response.send_message(
f"Skipping {number} song{'s' if number > 1 else ''}..."
)
@bot.tree.command(name="stop", description="Stop playing and disconnect")
@app_commands.guild_only()
async def stop(interaction: discord.Interaction):
assert interaction.guild is not None
player = bot.players.get(interaction.guild.id)
if player is None or not player.is_running():
await interaction.response.send_message(
f"{Emoji.error} I am not currently playing anything", ephemeral=True
)
else:
player.stop()
await interaction.response.send_message(f"Disconnecting...")