Skip to content

Commit

Permalink
Added !volume command
Browse files Browse the repository at this point in the history
  • Loading branch information
lamzaone committed Apr 23, 2024
1 parent 8172c24 commit 8feac73
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 deletions.
Binary file modified config/__pycache__/config.cpython-310.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions config/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import discord
import os

bot_token = 'MTIzMjE3NTYwNzU2NDIxMDIxNw.GEp7Ge.HPvDBvgPN97jVMKl-jTm9YgM3W4OkkEBP5Efgc'
serversettings = 'serversettings.json'
serversettings = os.path.join(os.getcwd(), 'config/serversettings.json')
ffmpeg_options = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
# 'options': '-vn -filter:a "volume=0.5"',
'options': '-vn',
}

Expand Down
5 changes: 5 additions & 0 deletions config/serversettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"1100516119644741681": {
"volume": 1.0
}
}
46 changes: 31 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from config import config # Make sure this import points to your bot's configuration
import asyncio
import json

# FFmpeg path - ensure you have FFmpeg installed and update the path if needed
FFMPEG_PATH = os.path.join(os.getcwd(), 'ffmpeg/bin/ffmpeg.exe')
Expand All @@ -21,12 +22,30 @@
async def on_ready():
print(f'Successfully booted: {bot.user}')
await bot.change_presence(activity=discord.Game(name="!play <song>", ), status=discord.Status.do_not_disturb)

for guild in bot.guilds:
if str(guild.id) not in config.serversettings:
# write the guild id to the settings file
with open(config.serversettings, 'w') as f:
json.dump({guild.id: {
'volume': 0.5
}}, f, indent=4)

with open(config.serversettings, 'r') as f:
settings = json.load(f)
settings = settings[str(guild.id)]





@bot.command(name='play', help='Play music from YouTube using a search term or URL')
async def play(ctx, *, query: str):
# retrieve settings for the guild from the settings file
with open(config.serversettings, 'r') as f:
settings = json.load(f)
settings = settings[str(ctx.guild.id)]
print(settings)
# Connect to the voice channel
if not ctx.author.voice:
await ctx.send("You must be in a voice channel to play music.")
Expand All @@ -52,7 +71,7 @@ async def play(ctx, *, query: str):
ctx.bot.video_url = video_url
# Play the audio using FFmpeg
audio_source = discord.FFmpegPCMAudio(video_url, executable=FFMPEG_PATH, **config.ffmpeg_options)
audio_source = discord.PCMVolumeTransformer(audio_source, volume=0.5)
audio_source = discord.PCMVolumeTransformer(audio_source, settings['volume'])
voice_client.play(audio_source)

await ctx.send(f":notes: Now playing: {info['title']} from {info['original_url']}")
Expand Down Expand Up @@ -81,25 +100,22 @@ async def pause(ctx):
ctx.voice_client.resume()
await ctx.send("Music resumed.")

@bot.command(name='volume', help='Adjust the volume level.')
async def volume(ctx, volume: float):
# Ensure the input volume is a percentage (e.g., 50 for 50%)
volume = volume / 100 # Convert to a float from a percentage

@bot.command(name='volume', help='Set the volume of the music')
async def volume(ctx, volume: int):
#change the settings for the guild in the settings file
with open(config.serversettings, 'r') as f:
settings = json.load(f)
settings = settings[str(ctx.guild.id)]
if ctx.voice_client and ctx.voice_client.is_playing():
# Re-create the audio source with the new volume
if bot.original_source is None:
bot.original_source = ctx.voice_client.source # Original audio source
new_source = discord.PCMVolumeTransformer(bot.original_source, volume=volume) # Create a new volume transformer
settings['volume'] = volume / 100
with open(config.serversettings, 'w') as f:
json.dump({ctx.guild.id: settings}, f, indent=4)
ctx.voice_client.source.volume = volume / 100
await ctx.send(f"Volume set to {volume}%")

# Stop current playback
ctx.voice_client.source = new_source


await ctx.send(f"Volume set to {volume * 100}%.")
else:
await ctx.send("No music is currently playing.")

@bot.command(name='ping', help='Check the bot\'s latency')
async def ping(ctx):
await ctx.send(f'Pong! Latency: {round(bot.latency * 1000)}ms, websocket latency: {round(bot.ws.latency * 1000)}ms')
Expand Down

0 comments on commit 8feac73

Please sign in to comment.