Quick wins: Lower default volume to 25% and add file upload support
- Scale volume by 0.25x to prevent earrape (user still sees 0-200%) - Add support for direct audio file URL links (.mp3, .mp4, etc.) - New =playfile command for Discord file uploads - Supports MP3, MP4, WAV, OGG, FLAC, M4A, WEBM, AAC, OPUS formats
This commit is contained in:
@@ -141,6 +141,72 @@ class music(commands.Cog):
|
||||
await queue.play(ctx)
|
||||
|
||||
|
||||
@commands.command(
|
||||
help="Upload and play an audio file (MP3, MP4, WAV, etc.)",
|
||||
aliases=['pf', 'file'])
|
||||
async def playfile(self, ctx: Context):
|
||||
"""Play an uploaded audio file from Discord attachment"""
|
||||
if ctx.guild is None:
|
||||
await ctx.send("❌ This command must be used in a server!")
|
||||
return
|
||||
|
||||
# Check if there's an attachment
|
||||
if not ctx.message.attachments:
|
||||
await ctx.send(
|
||||
"❌ No file attached! Please upload an audio file with your message.\n"
|
||||
"**Supported formats:** MP3, MP4, WAV, OGG, FLAC, M4A, WEBM, AAC, OPUS"
|
||||
)
|
||||
return
|
||||
|
||||
server = ctx.guild.id
|
||||
attachment = ctx.message.attachments[0]
|
||||
|
||||
# Validate file extension
|
||||
audio_extensions = ('.mp3', '.mp4', '.wav', '.ogg', '.flac', '.m4a', '.webm', '.aac', '.opus')
|
||||
if not any(attachment.filename.lower().endswith(ext) for ext in audio_extensions):
|
||||
await ctx.send(
|
||||
f"❌ Invalid file type: `{attachment.filename}`\n"
|
||||
f"**Supported formats:** MP3, MP4, WAV, OGG, FLAC, M4A, WEBM, AAC, OPUS"
|
||||
)
|
||||
return
|
||||
|
||||
await util.join_vc(ctx)
|
||||
await ctx.message.add_reaction('📎')
|
||||
|
||||
msg = await ctx.send(f"Processing file: `{attachment.filename}`...")
|
||||
|
||||
# Discord provides a CDN URL for the attachment
|
||||
file_url = attachment.url
|
||||
|
||||
# Use translate to process the file URL (yt-dlp handles direct URLs)
|
||||
audio = await translate.main(file_url, self.sp)
|
||||
|
||||
await msg.delete()
|
||||
|
||||
if len(audio) == 0:
|
||||
await ctx.message.add_reaction('🚫')
|
||||
await ctx.send("❌ Failed to process the audio file!")
|
||||
return
|
||||
|
||||
# Override title with filename if yt-dlp didn't get a good title
|
||||
if audio[0]['title'] == 'Unknown' or not audio[0]['title']:
|
||||
audio[0]['title'] = attachment.filename
|
||||
|
||||
# Queue the file
|
||||
audio[0]['position'] = await queue.add_song(
|
||||
server,
|
||||
audio[0],
|
||||
ctx.author.display_name)
|
||||
|
||||
await util.queue_message(ctx, audio[0])
|
||||
|
||||
if await queue.is_server_playing(server):
|
||||
return
|
||||
|
||||
await queue.update_server(server, True)
|
||||
await queue.play(ctx)
|
||||
|
||||
|
||||
@commands.command(
|
||||
help="Queue a song to play next (top of queue)",
|
||||
aliases=['pt', 'pn', 'playnext'])
|
||||
@@ -347,8 +413,9 @@ class music(commands.Cog):
|
||||
new_vol = await queue.set_volume(server.id, level)
|
||||
|
||||
# Update the current playing song's volume if something is playing
|
||||
# Scale down by 0.25 to match queue.py playback scaling
|
||||
if ctx.voice_client and ctx.voice_client.source:
|
||||
ctx.voice_client.source.volume = new_vol / 100.0
|
||||
ctx.voice_client.source.volume = new_vol / 100.0 * 0.25
|
||||
|
||||
# Pick an emoji based on volume
|
||||
if new_vol == 0:
|
||||
|
||||
Reference in New Issue
Block a user