multi-server support with broken volume

This commit is contained in:
Bence T. 2025-05-01 12:22:58 +02:00
parent 014d05629a
commit 60ebd345e7

40
main.py
View file

@ -9,8 +9,8 @@ intents = discord.Intents.default()
intents.message_content = True # Needed to read .play/.stop commands intents.message_content = True # Needed to read .play/.stop commands
bot = commands.Bot(command_prefix="..", intents=intents) bot = commands.Bot(command_prefix="..", intents=intents)
# Global variable to hold the current voice client # Global variable to hold the current voice clients
voice_client = None voice_clients = {}
# Sets the default volume to 20%, to make headphone users survive # Sets the default volume to 20%, to make headphone users survive
# the volume does not change when the bot is already playing, only new audio # the volume does not change when the bot is already playing, only new audio
@ -27,26 +27,26 @@ async def on_ready():
@bot.command() @bot.command()
async def play(ctx, url: str): async def play(ctx, url: str):
global voice_client guild_id = ctx.guild.id
if not ctx.author.voice: if not ctx.author.voice:
await ctx.send("lépjél már be egy kurva csatornába te szerencsétlen fasz") await ctx.send("lépjél már be egy kurva csatornába te szerencsétlen fasz")
return return
# Connect to the user's voice channel if not already connected if guild_id not in voice_clients or not voice_clients[guild_id].is_connected():
if not voice_client or not voice_client.is_connected(): voice_clients[guild_id] = await ctx.author.voice.channel.connect()
voice_client = await ctx.author.voice.channel.connect()
# Stop any currently playing audio vc = voice_clients[guild_id]
if voice_client.is_playing():
voice_client.stop()
if vc.is_playing():
vc.stop()
try: try:
# Load blocked keywords from file # Load blocked keywords from file
with open("blocked_keywords.txt", "r", encoding="utf-8") as f: with open("blocked_keywords.txt", "r", encoding="utf-8") as f:
blocked_keywords = [line.strip().lower() for line in f if line.strip()] blocked_keywords = [line.strip().lower() for line in f if line.strip()]
# YTDLP options
ydl_opts = { ydl_opts = {
"format": "bestaudio/best", "format": "bestaudio/best",
"quiet": True, "quiet": True,
@ -54,6 +54,7 @@ async def play(ctx, url: str):
"default_search": "auto", "default_search": "auto",
} }
# Use yt-dlp to extract audio URL and check for blocked keywords
with yt_dlp.YoutubeDL(ydl_opts) as ydl: with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(url, download=False) info = ydl.extract_info(url, download=False)
title = info["title"].lower() title = info["title"].lower()
@ -63,15 +64,17 @@ async def play(ctx, url: str):
audio_url = info["url"] audio_url = info["url"]
print("Audio URL:", audio_url) print("Audio URL:", audio_url)
# Set up FFmpeg options with the current volume
ffmpeg_options = { ffmpeg_options = {
"before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -protocol_whitelist file,http,https,tcp,tls,crypto", "before_options": "-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5 -protocol_whitelist file,http,https,tcp,tls,crypto",
"options": f"-vn -af volume={current_volume}" "options": f"-vn -af volume={current_volume}"
} }
voice_client.play( # Play the audio
vc.play(
FFmpegPCMAudio(audio_url, **ffmpeg_options), FFmpegPCMAudio(audio_url, **ffmpeg_options),
after=lambda e: print(f"Finished playing: {e}") after=lambda e: print(f"Finished playing: {e}")
) )
await ctx.send(f"most ez a fos szól: {info['title']}") await ctx.send(f"most ez a fos szól: {info['title']}")
except Exception as e: except Exception as e:
@ -79,14 +82,15 @@ async def play(ctx, url: str):
@bot.command() @bot.command()
async def stop(ctx): async def stop(ctx):
global voice_client guild_id = ctx.guild.id
if voice_client and voice_client.is_playing(): if guild_id in voice_clients and voice_clients[guild_id].is_playing():
voice_client.stop() voice_clients[guild_id].stop()
await ctx.send("hát akkor ne hallgassad basszalak szájba") await ctx.send("hát akkor ne hallgassad basszalak szájba")
else: else:
await ctx.send("nem is szól semmi te buzeráns fasz") await ctx.send("nem is szól semmi te buzeráns fasz")
#Does not work in multi-server setup, needs fix
@bot.command() @bot.command()
async def volume(ctx, vol: float): async def volume(ctx, vol: float):
global current_volume global current_volume
@ -98,11 +102,11 @@ async def volume(ctx, vol: float):
@bot.command() @bot.command()
async def leave(ctx): async def leave(ctx):
global voice_client guild_id = ctx.guild.id
if voice_client and voice_client.is_connected(): if guild_id in voice_clients and voice_clients[guild_id].is_connected():
await voice_client.disconnect() await voice_clients[guild_id].disconnect()
voice_client = None del voice_clients[guild_id]
await ctx.send("akkor csá gecik") await ctx.send("akkor csá gecik")
else: else:
await ctx.send("nem is vagyok voice channelben basszam ki a szádat") await ctx.send("nem is vagyok voice channelben basszam ki a szádat")