Skip to content

Commit

Permalink
Fixed soundboard and playsound command.
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyscripts committed Jan 15, 2025
1 parent a51a2f2 commit 129f335
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 54 deletions.
65 changes: 28 additions & 37 deletions commands/fun.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -377,23 +377,23 @@ async def playsound(self, ctx, mp3_url):
"colour": "#ff0000"
})
return

if not voice_state:
await cmdhelper.send_message(ctx, {
"title": "Play Sound",
"description": f"You're not in a voice channel",
"colour": "#ff0000"
})
return

if not str(mp3_url).endswith("mp3"):
await cmdhelper.send_message(ctx, {
"title": "Play Sound",
"description": f"That file is not an MP3",
"colour": "#ff0000"
})
return

sound_res = requests.get(mp3_url)
if sound_res.status_code != 200:
await cmdhelper.send_message(ctx, {
Expand All @@ -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))
bot.add_cog(Fun(bot))
28 changes: 11 additions & 17 deletions utils/soundboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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)
Expand All @@ -50,32 +46,30 @@ 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:
data.pop("source_guild_id")
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"
Expand All @@ -96,4 +90,4 @@ def get_default_sounds(self):
available=True
))

return sounds
return sounds

0 comments on commit 129f335

Please sign in to comment.