From dc8a2330c5ca4ddef307101b6ce47de99ba61522 Mon Sep 17 00:00:00 2001 From: meooow25 Date: Sun, 14 Oct 2018 19:58:26 +0530 Subject: Add cog to play spooky sounds * Add SpookySound cog * Add sound files * Add PyNaCl to Pipfile (required for Discord voice) --- bot/cogs/spookysound.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 bot/cogs/spookysound.py (limited to 'bot/cogs') diff --git a/bot/cogs/spookysound.py b/bot/cogs/spookysound.py new file mode 100644 index 00000000..99c1ae24 --- /dev/null +++ b/bot/cogs/spookysound.py @@ -0,0 +1,39 @@ +import random +from pathlib import Path + +import discord +from discord.ext import commands + +HACKTOBERBOT_VOICE_CHANNEL_ID = 101010 # Replace with actual channel ID + + +class SpookySound: + + def __init__(self, bot): + self.bot = bot + self.sound_files = list(Path("./bot/resources/spookysounds").glob("*.mp3")) + self.channel = None + + async def on_ready(self): + self.channel = self.bot.get_channel(HACKTOBERBOT_VOICE_CHANNEL_ID) + + @commands.cooldown(rate=1, per=120) + @commands.command(brief="Play a spooky sound, restricted to once per 2 mins") + async def spookysound(self, ctx): + """ + Connect to the Hacktoberbot voice channel, play a random spooky sound, then disconnect. Cannot be used more than + once in 2 minutes. + """ + await ctx.send("Initiating spooky sound...") + voice = await self.channel.connect() + file_path = random.choice(self.sound_files) + src = discord.FFmpegPCMAudio(str(file_path.resolve())) + + async def disconnect(): + await voice.disconnect() + + voice.play(src, after=lambda e: self.bot.loop.create_task(disconnect())) + + +def setup(bot): + bot.add_cog(SpookySound(bot)) -- cgit v1.2.3 From d019893bc598528730297bfb22ddc746e1c9734e Mon Sep 17 00:00:00 2001 From: meooow25 Date: Wed, 17 Oct 2018 12:38:32 +0530 Subject: * Update channel ID. * Add class docstring. * Make `disconnect` a helper static method. --- bot/cogs/spookysound.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'bot/cogs') diff --git a/bot/cogs/spookysound.py b/bot/cogs/spookysound.py index 99c1ae24..dd607097 100644 --- a/bot/cogs/spookysound.py +++ b/bot/cogs/spookysound.py @@ -4,10 +4,13 @@ from pathlib import Path import discord from discord.ext import commands -HACKTOBERBOT_VOICE_CHANNEL_ID = 101010 # Replace with actual channel ID +HACKTOBERBOT_VOICE_CHANNEL_ID = 498804789287714816 class SpookySound: + """ + A cog that plays a spooky sound in a voice channel on command. + """ def __init__(self, bot): self.bot = bot @@ -25,14 +28,17 @@ class SpookySound: once in 2 minutes. """ await ctx.send("Initiating spooky sound...") - voice = await self.channel.connect() 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))) - async def disconnect(): - await voice.disconnect() - - voice.play(src, after=lambda e: self.bot.loop.create_task(disconnect())) + @staticmethod + async def disconnect(voice): + """ + Helper method to disconnect a given voice client. + """ + await voice.disconnect() def setup(bot): -- cgit v1.2.3