From 129f33587e2111b044c6af786f86643a15f0f68b Mon Sep 17 00:00:00 2001 From: Ben Tettmar Date: Wed, 15 Jan 2025 02:46:37 +0000 Subject: [PATCH] Fixed soundboard and playsound command. --- commands/fun.py | 65 +++++++++++++++++++-------------------------- utils/soundboard.py | 28 ++++++++----------- 2 files changed, 39 insertions(+), 54 deletions(-) diff --git a/commands/fun.py b/commands/fun.py index 81f55ff..d094dfe 100755 --- a/commands/fun.py +++ b/commands/fun.py @@ -46,7 +46,7 @@ async def fun(self, ctx, selected_page: int = 1): @commands.command(name="rickroll", description="Never gonna give you up.", usage="") async def rickroll(self, ctx): - lyrics = requests.get("https://gist.githubusercontent.com/bennyscripts/c8f9a62542174cdfb45499fdf8719723/raw/2f6a8245c64c0ea3249814ad8e016ceac45473e0/rickroll.txt").text + lyrics = requests.get("https://gist.githubusercontent.com/bennyscripts/c8f9a62542174cdfb45499fdf8719723/raw/2f6a8245c64c0ea3249814ad8e016ceac45473e0/rickroll.txt").text for line in lyrics.splitlines(): await ctx.send(line) await asyncio.sleep(1) @@ -79,7 +79,7 @@ async def iq(self, ctx, *, user: discord.User): @commands.command(name="howgay", description="Get the gayness of a user.", usage="[user]", aliases=["gay", "gayrating"]) async def howgay(self, ctx, *, user: discord.User): gay_percentage = random.randint(0, 100) - + embed = discord.Embed(title=f"how gay is {user.name}?", description=f"{user.name} is {gay_percentage}% gay!") await cmdhelper.send_message(ctx, embed.to_dict()) @@ -241,7 +241,7 @@ async def dice(self, ctx, sides: int = 6): # # "yellow": {"codeblock": "fix", "prefix": "", "suffix": ""}, # # "green": {"codeblock": "cs", "prefix": "'", "suffix": "'",}, # # "blue": {"codeblock": "md", "prefix": "#", "suffix": ""}} - + # # emojis = ["🟥", "🟧", "🟨", "🟩", "🟦", "🟪"] # emojis = ["🔴", "🟠", "🟡", "🟢", "🔵", "🟣"] # message = await ctx.send(text) @@ -261,7 +261,7 @@ async def dice(self, ctx, sides: int = 6): # message = await ctx.fetch_message(msg_id) # # await message.add_reaction("🫡") - + # for _ in range(5): # for emoji in emojis: # reaction = await message.add_reaction(emoji) @@ -312,7 +312,7 @@ async def meme(self, ctx): embed = discord.Embed(title="Error", description="Too many requests, please try again later.") await cmdhelper.send_message(ctx, embed.to_dict()) return - + meme = random.choice(r.json()["data"]["children"])["data"]["url"] await ctx.send(meme) @@ -344,7 +344,7 @@ async def yomomma(self, ctx): r = requests.get("https://www.yomama-jokes.com/api/v1/jokes/random/") joke = r.json()["joke"] await ctx.send(joke) - + @commands.command(name="8ball", description="Ask the magic 8ball a question.", usage="[question]", aliases=["magic8ball", "ask8ball"]) async def eightball(self, ctx, *, question: str): responses = ["It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely on it", @@ -377,7 +377,7 @@ async def playsound(self, ctx, mp3_url): "colour": "#ff0000" }) return - + if not voice_state: await cmdhelper.send_message(ctx, { "title": "Play Sound", @@ -385,7 +385,7 @@ async def playsound(self, ctx, mp3_url): "colour": "#ff0000" }) return - + if not str(mp3_url).endswith("mp3"): await cmdhelper.send_message(ctx, { "title": "Play Sound", @@ -393,7 +393,7 @@ async def playsound(self, ctx, mp3_url): "colour": "#ff0000" }) return - + sound_res = requests.get(mp3_url) if sound_res.status_code != 200: await cmdhelper.send_message(ctx, { @@ -402,39 +402,30 @@ async def playsound(self, ctx, mp3_url): "colour": "#ff0000" }) return - - try: - with open("data/cache/mysound.mp3", "wb") as sound_file: - sound_file.write(sound_res.content) - - soundeffects = soundboard.Soundboard(cfg.get("token"), ctx.guild.id, voice_state.channel.id) - sound = soundeffects.upload_sound("data/cache/mysound.mp3", "ghost_sound_player", volume=1, emoji_id=None) - - if sound.id: - await cmdhelper.send_message(ctx, { - "title": "Play Sound", - "description": f"Sound file is being played" - }) - - soundeffects.play_sound(sound.id, source_guild_id=ctx.guild.id) - soundeffects.delete_sound(sound.id) - os.remove("data/cache/mysound.mp3") - else: - await cmdhelper.send_message(ctx, { - "title": "Play Sound", - "description": f"Sound could not be played. Possible reasons include:\n- File is too long\n- File is over 512KB", - "colour": "#ff0000" - }) - return - - except Exception as e: + with open("data/cache/mysound.mp3", "wb") as sound_file: + sound_file.write(sound_res.content) + + soundeffects = soundboard.Soundboard(cfg.get("token"), ctx.guild.id, voice_state.channel.id) + sound = soundeffects.upload_sound("data/cache/mysound.mp3", "ghost_sound_player", volume=1, emoji_id=None) + + if sound.id: + await cmdhelper.send_message(ctx, { + "title": "Play Sound", + "description": f"Sound file is being played" + }) + + soundeffects.play_sound(sound.id, source_guild_id=ctx.guild.id) + soundeffects.delete_sound(sound.id) + os.remove("data/cache/mysound.mp3") + + else: await cmdhelper.send_message(ctx, { "title": "Play Sound", "description": f"Sound could not be played. Possible reasons include:\n- File is too long\n- File is over 512KB", "colour": "#ff0000" }) - return + return def setup(bot): - bot.add_cog(Fun(bot)) \ No newline at end of file + bot.add_cog(Fun(bot)) diff --git a/utils/soundboard.py b/utils/soundboard.py index d703cc0..d3c2e7a 100644 --- a/utils/soundboard.py +++ b/utils/soundboard.py @@ -3,15 +3,11 @@ import mimetypes class Sound: - def __init__(self, name, sound_id, volume, emoji_id, emoji_name, override_path, user_id, available): + def __init__(self, name, sound_id, volume, emoji_id): self.name = name self.id = sound_id self.volume = volume self.emoji_id = emoji_id - self.emoji_name = emoji_name - self.override_path = override_path - self.user_id = user_id - self.available = available class Soundboard: def __init__(self, token, guild_id, channel_id): @@ -30,7 +26,7 @@ def encode(sound_file): content_type = mimetypes.guess_type(sound_file)[0] return f"data:{content_type};base64,{encoded}" - + def upload_sound(self, sound_file, name, emoji_id, volume): endpoint = f"https://discord.com/api/v9/guilds/{self.guild_id}/soundboard-sounds" encoded = self.encode(sound_file) @@ -50,24 +46,20 @@ def upload_sound(self, sound_file, name, emoji_id, volume): name=data["name"], sound_id=data["sound_id"], volume=data["volume"], - emoji_id=data["emoji_id"], - emoji_name=data["emoji_name"], - override_path=data["override_path"], - user_id=data["user_id"], - available=data.get("available", False) + emoji_id=data["emoji_id"] ) - + else: return res - + def delete_sound(self, sound_id): endpoint = f"https://discord.com/api/v9/guilds/{self.guild_id}/soundboard-sounds/{sound_id}" res = requests.delete(endpoint, headers=self.headers) return True if res.status_code == 204 else False - + def play_sound(self, sound_id, source_guild_id = True, override_path = None): - endpoint = f"https://discord.com/api/v9/channels/{self.channel_id}/voice-channel-effects" + endpoint = f"https://discord.com/api/v9/channels/{self.channel_id}/send-soundboard-sound" data = {"sound_id": sound_id, "source_guild_id": self.guild_id, "override_path": override_path} if source_guild_id == False or source_guild_id == None: @@ -75,7 +67,9 @@ def play_sound(self, sound_id, source_guild_id = True, override_path = None): if override_path == None or override_path == False: data.pop("override_path") - return requests.post(endpoint, json=data, headers=self.headers) + resp = requests.post(endpoint, json=data, headers=self.headers) + # print(resp.text) + return resp def get_default_sounds(self): endpoint = f"https://discord.com/api/v9/soundboard-default-sounds" @@ -96,4 +90,4 @@ def get_default_sounds(self): available=True )) - return sounds \ No newline at end of file + return sounds