aboutsummaryrefslogtreecommitdiffstats
path: root/arthur/exts/fun/numbers.py
blob: bb7705e189058c675eef5a0da8e6b75fa7d84a0e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import discord
from discord.ext import commands

from arthur.config import CONFIG


class Numbers(commands.GroupCog):
    """Commands for working with and controlling the numbers forwarder."""

    def __init__(self, bot: commands.Bot):
        self.bot = bot

    async def cog_load(self) -> None:
        """Join devops channel on cog load."""
        devops_vc = self.bot.get_channel(CONFIG.devops_vc_id)
        await self._join_and_play_numbers(devops_vc)  # type: ignore[union-attr]

    async def _join_and_play_numbers(
        self,
        channel: discord.VoiceChannel | discord.StageChannel,
    ) -> None:
        vc = await channel.connect(self_deaf=True, self_mute=False)
        vc.play(discord.FFmpegOpusAudio(CONFIG.numbers_url))

    @commands.group(invoke_without_command=True)
    async def numbers(
        self,
        ctx: commands.Context,
        channel: discord.VoiceChannel | discord.StageChannel | None = None,
    ) -> None:
        """
        Join a voice channel and forward numbers.

        If channel is not provided, the bot will join the voice channel of the user who invoked the command.
        """
        if not channel and isinstance(ctx.author, discord.Member) and (vs := ctx.author.voice):
            channel = vs.channel

        if not channel:
            await ctx.send(":x: Join a voice channel first!")
            return

        try:
            await self._join_and_play_numbers(channel)
        except discord.ClientException:
            # Already connected to a voice channel
            emoji = "🔇"
        else:
            emoji = "🔊"

        await ctx.message.add_reaction(emoji)

    @numbers.command()
    async def stop(self, ctx: commands.Context) -> None:
        """Stop playing URN in the servers voice channel."""
        if ctx.guild and (vc := ctx.guild.voice_client):
            await vc.disconnect(force=True)
            await ctx.message.add_reaction("🔇")
        else:
            await ctx.send(":x: Not in a voice channel!")


async def setup(bot: commands.Bot) -> None:
    """Set up the numbers cog."""
    await bot.add_cog(Numbers(bot))