diff options
| author | 2020-04-02 14:39:24 +0200 | |
|---|---|---|
| committer | 2020-04-02 14:39:24 +0200 | |
| commit | d77a2bbc50305d05371197f4cfe3354cfca4c627 (patch) | |
| tree | be1eed54972d9843f66114311f93b68b579046ac /bot/exts/halloween/spookysound.py | |
| parent | Merge pull request #382 from ks129/game-fuzzy (diff) | |
| parent | Merge master: adjust `Space` cog location (diff) | |
Merge pull request #329 from python-discord/seasonal-purge
Deseasonify: Make all cogs available year-round, and manage only branding by season.
Diffstat (limited to 'bot/exts/halloween/spookysound.py')
| -rw-r--r-- | bot/exts/halloween/spookysound.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/bot/exts/halloween/spookysound.py b/bot/exts/halloween/spookysound.py new file mode 100644 index 00000000..325447e5 --- /dev/null +++ b/bot/exts/halloween/spookysound.py @@ -0,0 +1,47 @@ +import logging +import random +from pathlib import Path + +import discord +from discord.ext import commands + +from bot.constants import Hacktoberfest + +log = logging.getLogger(__name__) + + +class SpookySound(commands.Cog): + """A cog that plays a spooky sound in a voice channel on command.""" + + def __init__(self, bot: commands.Bot): + self.bot = bot + self.sound_files = list(Path("bot/resources/halloween/spookysounds").glob("*.mp3")) + self.channel = None + + @commands.cooldown(rate=1, per=1) + @commands.command(brief="Play a spooky sound, restricted to once per 2 mins") + async def spookysound(self, ctx: commands.Context) -> None: + """ + Connect to the Hacktoberbot voice channel, play a random spooky sound, then disconnect. + + Cannot be used more than once in 2 minutes. + """ + if not self.channel: + await self.bot.wait_until_ready() + self.channel = self.bot.get_channel(Hacktoberfest.voice_id) + + await ctx.send("Initiating spooky sound...") + file_path = random.choice(self.sound_files) + src = discord.FFmpegPCMAudio(str(file_path.resolve())) + voice = await self.channel.connect() + voice.play(src, after=lambda e: self.bot.loop.create_task(self.disconnect(voice))) + + @staticmethod + async def disconnect(voice: discord.VoiceClient) -> None: + """Helper method to disconnect a given voice client.""" + await voice.disconnect() + + +def setup(bot: commands.Bot) -> None: + """Spooky sound Cog load.""" + bot.add_cog(SpookySound(bot)) |