Skip to content

Commit

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

bot_token = 'MTIzMjE3NTYwNzU2NDIxMDIxNw.GEp7Ge.HPvDBvgPN97jVMKl-jTm9YgM3W4OkkEBP5Efgc'

serversettings = '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 All @@ -20,7 +21,7 @@
'source_address': '0.0.0.0',
'force-ipv4': True,
'preferredcodec': 'mp3',
'cachedir': False
'cachedir': False,

}

Expand Down
Empty file added config/serversettings.json
Empty file.
53 changes: 52 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
queue = []



@bot.event
async def on_ready():
print(f'Logged in as {bot.user}')
print(f'Successfully booted: {bot.user}')
await bot.change_presence(activity=discord.Game(name="!play <song>", ), status=discord.Status.do_not_disturb)




Expand All @@ -46,11 +48,19 @@ async def play(ctx, *, query: str):

video_url = info['url']

ctx.bot.video_info = info
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)
voice_client.play(audio_source)

await ctx.send(f":notes: Now playing: {info['title']} from {info['original_url']}")
# disconnect when the song is over but not when pausing
while voice_client.is_connected() and (voice_client.is_playing() or ctx.voice_client.is_paused()):
await asyncio.sleep(1)
await stop(ctx)


except Exception as e:
print(f"Error playing music: {e}")
Expand All @@ -60,6 +70,7 @@ async def play(ctx, *, query: str):
async def stop(ctx):
if ctx.voice_client and ctx.voice_client.is_connected():
await ctx.voice_client.disconnect()
await ctx.send("Music stopped.")

@bot.command(name='pause', help='Pause the currently playing music')
async def pause(ctx):
Expand All @@ -70,11 +81,51 @@ 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

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

# 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')

@bot.command(name='seek', help='Set the playback position to a specific time in seconds')
async def fastforward(ctx, seconds: int):
# Ensure there is a voice client playing music
if ctx.voice_client and ctx.voice_client.is_playing():
# Stop the current playback
ctx.voice_client.stop()

# Check if there's stored video information
if hasattr(ctx.bot, 'video_info') and hasattr(ctx.bot, 'video_url'):
# Add the offset in seconds to the FFmpeg options to fast forward
seek_time = f"-ss {seconds}"
ffmpeg_opts = {**config.ffmpeg_options, "options": f"{config.ffmpeg_options['options']} {seek_time}"}

audio_source = discord.FFmpegPCMAudio(ctx.bot.video_url, executable=FFMPEG_PATH, **ffmpeg_opts)

ctx.voice_client.play(audio_source)

await ctx.send(f"Started playing at {seconds} seconds.")
else:
await ctx.send("No music data available to seek.")
else:
await ctx.send("No music is currently playing.")

@bot.event
async def on_message(message):
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PyNaCl
discord
yt_dlp
ffmpeg
ffmpeg
json

0 comments on commit 8172c24

Please sign in to comment.