please make a ai chatbot that when anybody sends a message to this channel…
- User
please make a ai chatbot that when anybody sends a message to this channel 1288185653494026292 it gets parsed into a ai with a certaint personality I can specify in a json. please make it then respond with text and with speech, depending on what mode is selected. make a speech only mode, make a text only mode and make the normal mode that does both. and for speech, make it turn its response into a speech using Coqui TTS with gpu enabled. and then playing it. make sure the ai gets the username that send the message. and also make it have chat history. use openhermes model for the ollama. make sure to output the entire code. here is what I have so far: import discord from discord import app_commands from discord.ext import commands import os from dotenv import load_dotenv import asyncio from pydub import AudioSegment import requests import json import logging from typing import Optional, List from enum import Enum from discord.ui import View, Button from math import ceil
Configure logging with timestamps
logging.basicConfig( level=logging.INFO, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S' )
Load environment variables from .env
load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN')
Define intents
intents = discord.Intents.default() intents.message_content = True intents.guilds = True intents.voice_states = True
Playback modes
class PlaybackMode(Enum): QUEUE = 'queue' SYNC = 'sync' PARALLEL = 'parallel'
Initialize the bot
class SoundboardBot(commands.Bot): def init(self): super().init( command_prefix="!", # Prefix is not used for slash commands but required intents=intents, ) self.queue = {} # Dictionary to hold queues for each guild self.sounds = {} # Dictionary to hold sound metadata self.disconnect_tasks = {} # Dictionary to hold disconnect tasks for each guild self.guild_durations = {} # Dictionary to hold disconnect durations per guild self.guild_modes = {} # Dictionary to hold playback modes per guild ('queue', 'sync', 'parallel') self.parallel_audio_sources = {} # Dictionary to hold ParallelAudioSource per guild
async def setup_hook(self): # Sync the command tree with Discord await self.tree.sync() # Load sounds from JSON self.load_sounds() def load_sounds(self): sounds_file = os.path.join("data", "sounds.json") if os.path.exists(sounds_file): with open(sounds_file, "r") as f: self.sounds = json.load(f) logging.info("Loaded sounds from sounds.json") else: self.sounds = {} os.makedirs(os.path.dirname(sounds_file), exist_ok=True) with open(sounds_file, "w") as f: json.dump(self.sounds, f, indent=4) logging.info("Created new sounds.json") def save_sounds(self): sounds_file = os.path.join("data", "sounds.json") with open(sounds_file, "w") as f: json.dump(self.sounds, f, indent=4) logging.info("Saved sounds to sounds.json") async def play_sound_via_button(self, interaction: discord.Interaction, sound_name: str): """ Helper function to play sounds when triggered via soundboard buttons. Respects the current playback mode ('queue', 'sync', 'parallel'). """ # Check if the user is in a voice channel if not interaction.user.voice or not interaction.user.voice.channel: await interaction.response.send_message("❌ You need to be in a voice channel to use this button.", ephemeral=True) return voice_channel = interaction.user.voice.channel guild_id = interaction.guild.id # Locate the sound file if sound_name not in self.sounds: await interaction.response.send_message(f"❌ Sound `{sound_name}` not found.", ephemeral=True) return sound_file = self.sounds[sound_name]["file_path"] sound_duration = self.sounds[sound_name]["duration"] # Get the current mode mode = self.guild_modes.get(guild_id, 'queue') # Initialize the queue if not present if guild_id not in self.queue: self.queue[guild_id] = asyncio.Queue() # Connect to the voice channel if not already connected voice_client = discord.utils.get(self.voice_clients, guild__id=guild_id) if not voice_client: try: voice_client = await voice_channel.connect() logging.info(f"Connected to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to connect to voice channel: {e}") await interaction.response.send_message(f"❌ Failed to connect to voice channel: {e}", ephemeral=True) return else: if voice_client.channel != voice_channel: try: await voice_client.move_to(voice_channel) logging.info(f"Moved to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to move to voice channel: {e}") await interaction.response.send_message(f"❌ Failed to move to voice channel: {e}", ephemeral=True) return if mode == 'queue': # Enqueue the sound with default speed=1.0 await self.queue[guild_id].put((sound_file, 1, sound_duration, 1.0)) logging.info(f"Enqueued sound '{sound_name}' in guild {guild_id} (Mode: Queue).") # Cancel any existing disconnect task if guild_id in self.disconnect_tasks: self.disconnect_tasks[guild_id].cancel() del self.disconnect_tasks[guild_id] # If not already playing, start playing if not voice_client.is_playing(): await play_next(guild_id) await interaction.response.send_message(f"✅ Sound `{sound_name}` enqueued.", ephemeral=True) elif mode == 'sync': # Sync mode: Play immediately, interrupting current playback if voice_client.is_playing(): voice_client.stop() logging.info(f"Stopped current playback in guild {guild_id} for sync mode.") # Clear the queue if guild_id in self.queue: try: while not self.queue[guild_id].empty(): self.queue[guild_id].get_nowait() except: pass # Ignore if queue is already empty # Enqueue the sound with default speed=1.0 await self.queue[guild_id].put((sound_file, 1, sound_duration, 1.0)) logging.info(f"Queued sound '{sound_name}' in guild {guild_id} (Mode: Sync).") # Start playing immediately await play_next(guild_id) # Cancel any existing disconnect task if guild_id in self.disconnect_tasks: self.disconnect_tasks[guild_id].cancel() del self.disconnect_tasks[guild_id] await interaction.response.send_message(f"✅ Sound `{sound_name}` played immediately (Sync Mode).", ephemeral=True) elif mode == 'parallel': # Parallel mode: Play sounds simultaneously # Prepare the audio source with default speed=1.0 filters = generate_atempo_filter(1.0) before_options = f"-ss 0 -t {sound_duration}" options = f"-vn -filter:a {filters}" source = discord.FFmpegPCMAudio(sound_file, before_options=before_options, options=options) # Initialize ParallelAudioSource if not exists if guild_id not in self.parallel_audio_sources: self.parallel_audio_sources[guild_id] = ParallelAudioSource() # Add the source to ParallelAudioSource self.parallel_audio_sources[guild_id].add_source(source) # If not already playing, start playing if not voice_client.is_playing(): def after_playing(error): if error: logging.error(f"Error in parallel playback: {error}") asyncio.run_coroutine_threadsafe(handle_parallel_finish(guild_id), self.loop) voice_client.play(self.parallel_audio_sources[guild_id], after=after_playing) logging.info(f"Started parallel playback in guild {guild_id}.") await interaction.response.send_message(f"✅ Sound `{sound_name}` added to parallel playback.", ephemeral=True)bot = SoundboardBot()
Directory to store sound files
SOUND_DIR = "sounds"
Ensure the sounds and data directories exist
for directory in [SOUND_DIR, "data"]: if not os.path.exists(directory): os.makedirs(directory) logging.info(f"Created directory: {directory}")
Allowed audio formats
ALLOWED_EXTENSIONS = ('.mp3', '.wav', '.ogg', '.flac')
Maximum file size (in bytes) for uploads via
/addcommandMAX_FILE_SIZE = 8 * 1024 * 1024 # 8MB
Helper function to extract duration of audio file in seconds
def get_audio_duration(file_path): try: audio = AudioSegment.from_file(file_path) duration = len(audio) / 1000 # pydub works in milliseconds return round(duration, 2) except Exception as e: logging.error(f"Error getting duration of {file_path}: {e}") return None
Helper function to download a file from a URL
async def download_file(url, dest): loop = asyncio.get_event_loop() try: response = requests.get(url, stream=True, timeout=120) response.raise_for_status() # Download in chunks to handle large files efficiently with open(dest, "wb") as f: for chunk in response.iter_content(chunk_size=8192): if chunk: f.write(chunk) return True, "" except requests.exceptions.RequestException as e: return False, str(e)
Helper function to handle disconnect after duration
async def monitor_queue(guild_id: int): duration = bot.guild_durations.get(guild_id, 60) # Default to 60 seconds logging.info(f"Monitoring guild {guild_id} for disconnect in {duration} seconds.") await asyncio.sleep(duration) voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id) if voice_client and voice_client.is_connected(): await voice_client.disconnect() logging.info(f"Disconnected from guild {guild_id} after {duration} seconds of inactivity.")
Helper function to generate atempo filters based on desired speed
def generate_atempo_filter(speed: float) -> str: if speed <= 0: raise ValueError("Speed must be a positive number.")
filters = [] while speed > 2.0 or speed < 0.5: if speed > 2.0: filters.append("atempo=2.0") speed /= 2.0 elif speed < 0.5: filters.append("atempo=0.5") speed /= 0.5 filters.append(f"atempo={speed}") return ",".join(filters)Function to play the next sound in the queue
async def play_next(guild_id: int): if guild_id not in bot.queue: return
if bot.queue[guild_id].empty(): # Start a disconnect timer if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] disconnect_task = asyncio.create_task(monitor_queue(guild_id)) bot.disconnect_tasks[guild_id] = disconnect_task return sound_file, repeat, duration_play, speed = await bot.queue[guild_id].get() voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id) if not voice_client or not voice_client.is_connected(): logging.warning(f"No active voice client in guild {guild_id}.") return if voice_client.is_playing(): # Re-enqueue the sound if it's still playing await bot.queue[guild_id].put((sound_file, repeat, duration_play, speed)) return # Play the sound with specified duration and speed try: # Prepare FFmpeg options to limit playback duration and set speed filters = generate_atempo_filter(speed) before_options = f"-ss 0 -t {duration_play}" options = f"-vn -filter:a {filters}" source = discord.FFmpegPCMAudio(sound_file, before_options=before_options, options=options) def after_playing(error): if error: logging.error(f"Error playing sound: {error}") asyncio.run_coroutine_threadsafe(play_next(guild_id), bot.loop) voice_client.play(source, after=after_playing) sound_name = os.path.splitext(os.path.basename(sound_file))[0] logging.info(f"Playing sound '{sound_name}' in guild {guild_id}. Speed: {speed}x") except Exception as e: logging.error(f"Error playing sound {sound_file} in guild {guild_id}: {e}") # Proceed to next sound await play_next(guild_id)Handle finishing of parallel playback
async def handle_parallel_finish(guild_id: int): if guild_id in bot.parallel_audio_sources: if bot.parallel_audio_sources[guild_id].finished: # Start a disconnect timer if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] disconnect_task = asyncio.create_task(monitor_queue(guild_id)) bot.disconnect_tasks[guild_id] = disconnect_task del bot.parallel_audio_sources[guild_id] logging.info(f"Parallel playback finished in guild {guild_id}.")
Class for mixing audio sources for parallel playback
class ParallelAudioSource(discord.AudioSource): def init(self): self.sources: List[discord.AudioSource] = [] self.finished = False
def add_source(self, source: discord.AudioSource): self.sources.append(source) def is_opus(self): return False def read(self): if not self.sources: self.finished = True return b'' frames = [] for source in self.sources[:]: frame = source.read() if frame: frames.append(frame) else: # EOF for this source self.sources.remove(source) if not frames: self.finished = True return b'' # Assume all frames are the same size frame_size = len(frames[0]) mixed_frame = bytearray(frame_size) # Mixing frames for i in range(0, frame_size, 2): sample_sum = 0 for frame in frames: # Get the sample from the frame sample = int.from_bytes(frame[i:i+2], 'little', signed=True) sample_sum += sample # Prevent clipping sample_sum = max(min(sample_sum, 32767), -32768) # Write back to mixed_frame mixed_frame[i:i+2] = int(sample_sum).to_bytes(2, 'little', signed=True) return bytes(mixed_frame)🔹 New Feature: Soundboard Command with Pagination
Utilizing discord.ui for interactive components
class SoundboardView(View): def init(self, bot: SoundboardBot, guild_id: int, current_page: int = 1, sounds_per_page: int = 20): super().init(timeout=None) self.bot = bot self.guild_id = guild_id self.current_page = current_page self.sounds_per_page = sounds_per_page self.sounds_sorted = sorted(bot.sounds.keys(), key=lambda x: x.lower()) self.total_sounds = len(self.sounds_sorted) self.total_pages = ceil(self.total_sounds / self.sounds_per_page) if self.sounds_per_page else 1
self.update_buttons() def update_buttons(self): # Clear existing buttons to update the view self.clear_items() start_index = (self.current_page - 1) * self.sounds_per_page end_index = start_index + self.sounds_per_page sounds_on_page = self.sounds_sorted[start_index:end_index] # Add sound buttons for sound_name in sounds_on_page: self.add_item(SoundButton(sound_name, self.bot, self.guild_id)) # Add navigation buttons if necessary if self.total_pages > 1: if self.current_page > 1: self.add_item(PreviousPageButton()) if self.current_page < self.total_pages: self.add_item(NextPageButton())class PreviousPageButton(Button): def init(self): super().init(style=discord.ButtonStyle.secondary, label="⬅️ Previous", custom_id="previous_page")
async def callback(self, interaction: discord.Interaction): view: SoundboardView = self.view if view.current_page > 1: view.current_page -= 1 view.update_buttons() # Retrieve the existing embed if interaction.message.embeds: embed = interaction.message.embeds[0] else: embed = discord.Embed( title="🎵 Soundboard 🎵", description="Click the buttons below to play sounds.", color=discord.Color.blue() ) # Update the footer embed.set_footer(text=f"Page {view.current_page} of {view.total_pages}") await interaction.response.edit_message(embed=embed, view=view) else: await interaction.response.defer()class NextPageButton(Button): def init(self): super().init(style=discord.ButtonStyle.secondary, label="Next ➡️", custom_id="next_page")
async def callback(self, interaction: discord.Interaction): view: SoundboardView = self.view if view.current_page < view.total_pages: view.current_page += 1 view.update_buttons() # Retrieve the existing embed if interaction.message.embeds: embed = interaction.message.embeds[0] else: embed = discord.Embed( title="🎵 Soundboard 🎵", description="Click the buttons below to play sounds.", color=discord.Color.blue() ) # Update the footer embed.set_footer(text=f"Page {view.current_page} of {view.total_pages}") await interaction.response.edit_message(embed=embed, view=view) else: await interaction.response.defer()class SoundButton(Button): def init(self, sound_name: str, bot: SoundboardBot, guild_id: int): super().init( style=discord.ButtonStyle.primary, label=sound_name, custom_id=f'soundboard_sound_{sound_name}' ) self.sound_name = sound_name self.bot = bot self.guild_id = guild_id
async def callback(self, interaction: discord.Interaction): await self.bot.play_sound_via_button(interaction, self.sound_name)🔹 Slash Command: Soundboard
@bot.tree.command(name="soundboard", description="Display the soundboard with clickable buttons for sounds.") async def soundboard(interaction: discord.Interaction): await interaction.response.defer()
if not bot.sounds: embed = discord.Embed( title="❌ No Sounds Available ❌", description="Add some sounds using the `/add` or `/add_from_url` commands.", color=discord.Color.red() ) await interaction.followup.send(embed=embed) return # Create the view with sound buttons and pagination view = SoundboardView(bot, interaction.guild.id) embed = discord.Embed( title="🎵 Soundboard 🎵", description="Click the buttons below to play sounds.", color=discord.Color.blue() ) # Add page information embed.set_footer(text=f"Page {view.current_page} of {view.total_pages}") await interaction.followup.send(embed=embed, view=view)🔹 Slash Command: Set Playback Mode
@bot.tree.command(name="set_mode", description="Set the playback mode for the bot.") @app_commands.describe( mode='Playback mode to set: "queue", "sync", or "parallel"' ) async def set_mode(interaction: discord.Interaction, mode: PlaybackMode): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id bot.guild_modes[guild_id] = mode.value logging.info(f"Guild {guild_id} mode changed to {mode.value.capitalize()}.") await interaction.followup.send(f"✅ Playback mode set to '{mode.value}'.", ephemeral=True)
🔹 Slash Command: Add a sound via file upload
@bot.tree.command(name="add", description="Add a new sound to the soundboard.") @app_commands.describe( name="Name for the sound", file="Audio file to upload (MP3, WAV, OGG, FLAC)" ) async def add_sound(interaction: discord.Interaction, name: str, file: discord.Attachment): await interaction.response.defer(ephemeral=True)
# Validate file extension if not any(file.filename.lower().endswith(ext) for ext in ALLOWED_EXTENSIONS): await interaction.followup.send( f"❌ Unsupported file format. Please upload a file with one of the following extensions: {', '.join(ALLOWED_EXTENSIONS)}.", ephemeral=True ) return # Validate file size if file.size > MAX_FILE_SIZE: await interaction.followup.send( f"❌ File size exceeds the 8MB limit.", ephemeral=True ) return # Sanitize the sound name to prevent filesystem issues safe_name = "".join([c if c.isalnum() or c in (' ', '_') else '_' for c in name]).strip() if not safe_name: await interaction.followup.send( "❌ Invalid sound name. Please use alphanumeric characters, spaces, or underscores.", ephemeral=True ) return file_extension = os.path.splitext(file.filename)[1] file_path = os.path.join(SOUND_DIR, f"{safe_name}{file_extension}") # Check for name conflicts if any(safe_name.lower() == existing.lower() for existing in bot.sounds.keys()): await interaction.followup.send( f"❌ A sound with the name `{safe_name}` already exists.", ephemeral=True ) return try: # Save the uploaded file await file.save(file_path) # Extract duration duration = get_audio_duration(file_path) if duration is None: os.remove(file_path) await interaction.followup.send( "❌ Failed to extract duration from the audio file. Ensure it's a valid audio file.", ephemeral=True ) return # Add sound metadata bot.sounds[safe_name] = { "file_path": file_path, "duration": duration } bot.save_sounds() # Confirm addition with an embed embed = discord.Embed( title="✅ Sound Added ✅", description=f"Sound `{safe_name}` has been added successfully.", color=discord.Color.green() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="File", value=f"[{file.filename}]({file.url})", inline=False) embed.add_field(name="Duration", value=f"{duration} seconds", inline=False) await interaction.followup.send(embed=embed, ephemeral=True) except Exception as e: logging.error(f"Failed to add sound: {e}") await interaction.followup.send(f"❌ Failed to add sound: {e}", ephemeral=True)🔹 Slash Command: Add a sound from a URL (no file upload limit)
@bot.tree.command(name="add_from_url", description="Add a new sound to the soundboard by downloading from a URL.") @app_commands.describe( name="Name for the sound", url="Direct URL to the audio file (MP3, WAV, OGG, FLAC)" ) async def add_from_url(interaction: discord.Interaction, name: str, url: str): await interaction.response.defer(ephemeral=True)
# Validate file extension if not any(url.lower().endswith(ext) for ext in ALLOWED_EXTENSIONS): await interaction.followup.send( f"❌ Unsupported file format. Please provide a URL ending with one of the following extensions: {', '.join(ALLOWED_EXTENSIONS)}.", ephemeral=True ) return # Sanitize the sound name safe_name = "".join([c if c.isalnum() or c in (' ', '_') else '_' for c in name]).strip() if not safe_name: await interaction.followup.send( "❌ Invalid sound name. Please use alphanumeric characters, spaces, or underscores.", ephemeral=True ) return file_extension = os.path.splitext(url)[1] file_path = os.path.join(SOUND_DIR, f"{safe_name}{file_extension}") # Check for name conflicts if any(safe_name.lower() == existing.lower() for existing in bot.sounds.keys()): await interaction.followup.send( f"❌ A sound with the name `{safe_name}` already exists.", ephemeral=True ) return try: # Download the file success, error = await download_file(url, file_path) if not success: await interaction.followup.send( f"❌ Failed to download the file: {error}", ephemeral=True ) return # Extract duration duration = get_audio_duration(file_path) if duration is None: os.remove(file_path) await interaction.followup.send( "❌ Failed to extract duration from the audio file. Ensure it's a valid audio file.", ephemeral=True ) return # Add sound metadata bot.sounds[safe_name] = { "file_path": file_path, "duration": duration } bot.save_sounds() # Confirm addition with an embed embed = discord.Embed( title="✅ Sound Added from URL ✅", description=f"Sound `{safe_name}` has been added successfully from the provided URL.", color=discord.Color.green() ) embed.add_field(name="URL", value=f"[Download Here]({url})", inline=False) embed.add_field(name="Duration", value=f"{duration} seconds", inline=False) await interaction.followup.send(embed=embed, ephemeral=True) except Exception as e: logging.error(f"Failed to add sound from URL: {e}") # Clean up if download failed if os.path.exists(file_path): os.remove(file_path) await interaction.followup.send(f"❌ Failed to add sound from URL: {e}", ephemeral=True)🔹 Slash Command: Play a sound with optional repeat and speed
@bot.tree.command(name="play", description="Play a sound in your current voice channel.") @app_commands.describe( name="Name of the sound to play", repeat="Number of times to play the sound (default: 1)", play_duration="Time in seconds to play the sound (default: full duration)", speed="Playback speed (e.g., 1.0 is normal, 2.0 is 2x speed) (optional)" ) async def play_sound(interaction: discord.Interaction, name: str, repeat: int = 1, play_duration: Optional[float] = None, speed: Optional[float] = 1.0): await interaction.response.defer()
# Validate repeat parameter if repeat < 1: await interaction.followup.send("❌ Repeat count must be at least 1.", ephemeral=True) return # Validate speed parameter if speed <= 0: await interaction.followup.send("❌ Speed must be a positive number.", ephemeral=True) return # Check if the user is in a voice channel if not interaction.user.voice or not interaction.user.voice.channel: await interaction.followup.send("❌ You need to be in a voice channel to use this command.", ephemeral=True) return voice_channel = interaction.user.voice.channel guild_id = interaction.guild.id # Locate the sound file if name not in bot.sounds: await interaction.followup.send(f"❌ Sound `{name}` not found.", ephemeral=True) return sound_file = bot.sounds[name]["file_path"] sound_duration = bot.sounds[name]["duration"] # Determine play duration if play_duration is None: play_duration = sound_duration elif play_duration > sound_duration: play_duration = sound_duration elif play_duration <= 0: await interaction.followup.send("❌ Play duration must be a positive number.", ephemeral=True) return play_duration = round(play_duration, 2) # Ensure it's rounded to 2 decimal places # Get the current mode mode = bot.guild_modes.get(guild_id, 'queue') # Initialize the queue if not present if guild_id not in bot.queue: bot.queue[guild_id] = asyncio.Queue() # Connect to the voice channel if not already connected voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id) if not voice_client: try: voice_client = await voice_channel.connect() logging.info(f"Connected to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to connect to voice channel: {e}") await interaction.followup.send(f"❌ Failed to connect to voice channel: {e}", ephemeral=True) return else: if voice_client.channel != voice_channel: try: await voice_client.move_to(voice_channel) logging.info(f"Moved to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to move to voice channel: {e}") await interaction.followup.send(f"❌ Failed to move to voice channel: {e}", ephemeral=True) return if mode == 'queue': # Enqueue the sound with specified speed for _ in range(repeat): await bot.queue[guild_id].put((sound_file, 1, play_duration, speed)) # Each enqueue has repeat=1 logging.info(f"Enqueued sound '{name}' {repeat} time(s) in guild {guild_id} (Mode: Queue).") # Cancel any existing disconnect task if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] # If the voice client is not playing, start playing the next sound if not voice_client.is_playing(): await play_next(guild_id) # Send confirmation with embed queue_length = bot.queue[guild_id].qsize() embed = discord.Embed( title="🎶 Sound Enqueued 🎶", description=f"Sound `{name}` has been added to the queue.", color=discord.Color.blue() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="Repeat Count", value=f"{repeat}", inline=False) embed.add_field(name="Play Duration", value=f"{play_duration} seconds", inline=False) embed.add_field(name="Speed", value=f"{speed}x", inline=False) embed.add_field(name="Position in Queue", value=f"{queue_length}", inline=False) embed.set_footer(text=f"Total in Queue: {queue_length}") await interaction.followup.send(embed=embed) elif mode == 'sync': # Sync mode: Play immediately, interrupting current playback if voice_client.is_playing(): voice_client.stop() logging.info(f"Stopped current playback in guild {guild_id} for sync mode.") # Clear the queue if guild_id in bot.queue: try: while not bot.queue[guild_id].empty(): bot.queue[guild_id].get_nowait() except: pass # Ignore if queue is already empty # Enqueue the sound with specified speed for _ in range(repeat): await bot.queue[guild_id].put((sound_file, 1, play_duration, speed)) # Each enqueue has repeat=1 logging.info(f"Enqueued sound '{name}' {repeat} time(s) in guild {guild_id} (Mode: Sync).") # Start playing immediately await play_next(guild_id) # Cancel any existing disconnect task if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] # Send confirmation with embed embed = discord.Embed( title="🎶 Sound Played 🎶", description=f"Sound `{name}` has been played immediately (Sync Mode).", color=discord.Color.orange() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="Repeat Count", value=f"{repeat}", inline=False) embed.add_field(name="Play Duration", value=f"{play_duration} seconds", inline=False) embed.add_field(name="Speed", value=f"{speed}x", inline=False) await interaction.followup.send(embed=embed) elif mode == 'parallel': # Parallel mode: Play sounds simultaneously # Prepare the audio sources with specified speed for _ in range(repeat): filters = generate_atempo_filter(speed) before_options = f"-ss 0 -t {play_duration}" options = f"-vn -filter:a {filters}" source = discord.FFmpegPCMAudio(sound_file, before_options=before_options, options=options) # Initialize ParallelAudioSource if not exists if guild_id not in bot.parallel_audio_sources: bot.parallel_audio_sources[guild_id] = ParallelAudioSource() # Add the source to ParallelAudioSource bot.parallel_audio_sources[guild_id].add_source(source) logging.info(f"Added sound '{name}' at speed {speed}x to parallel playback in guild {guild_id}.") # If not already playing, start playing if not voice_client.is_playing(): def after_playing(error): if error: logging.error(f"Error in parallel playback: {error}") asyncio.run_coroutine_threadsafe(handle_parallel_finish(guild_id), bot.loop) voice_client.play(bot.parallel_audio_sources[guild_id], after=after_playing) logging.info(f"Started parallel playback in guild {guild_id}.") # Send confirmation with embed embed = discord.Embed( title="🎶 Sound Added to Parallel Playback 🎶", description=f"Sound `{name}` has been added to parallel playback.", color=discord.Color.green() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="Repeat Count", value=f"{repeat}", inline=False) embed.add_field(name="Play Duration", value=f"{play_duration} seconds", inline=False) embed.add_field(name="Speed", value=f"{speed}x", inline=False) await interaction.followup.send(embed=embed)🔹 Slash Command: Play All Sounds
@bot.tree.command(name="play_all", description="Play all sounds at once in your current voice channel.") @app_commands.describe( repeat="Number of times to play each sound (default: 1)", play_duration="Time in seconds to play each sound (default: full duration)", speed="Playback speed for all sounds (e.g., 1.0 is normal, 2.0 is 2x speed) (optional)" ) async def play_all_sounds(interaction: discord.Interaction, repeat: int = 1, play_duration: Optional[float] = None, speed: Optional[float] = 1.0): await interaction.response.defer()
# Validate repeat parameter if repeat < 1: await interaction.followup.send("❌ Repeat count must be at least 1.", ephemeral=True) return # Validate speed parameter if speed <= 0: await interaction.followup.send("❌ Speed must be a positive number.", ephemeral=True) return # Check if there are sounds available if not bot.sounds: await interaction.followup.send("❌ No sounds available to play.", ephemeral=True) return # Check if the user is in a voice channel if not interaction.user.voice or not interaction.user.voice.channel: await interaction.followup.send("❌ You need to be in a voice channel to use this command.", ephemeral=True) return voice_channel = interaction.user.voice.channel guild_id = interaction.guild.id # Initialize the queue if not present if guild_id not in bot.queue: bot.queue[guild_id] = asyncio.Queue() # Connect to the voice channel if not already connected voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id) if not voice_client: try: voice_client = await voice_channel.connect() logging.info(f"Connected to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to connect to voice channel: {e}") await interaction.followup.send(f"❌ Failed to connect to voice channel: {e}", ephemeral=True) return else: if voice_client.channel != voice_channel: try: await voice_client.move_to(voice_channel) logging.info(f"Moved to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to move to voice channel: {e}") await interaction.followup.send(f"❌ Failed to move to voice channel: {e}", ephemeral=True) return # Determine play duration # Assuming all sounds play the same duration first_sound = next(iter(bot.sounds.values()), None) if first_sound: default_play_duration = first_sound["duration"] if play_duration is None: play_duration = default_play_duration elif play_duration > default_play_duration: play_duration = default_play_duration elif play_duration <= 0: await interaction.followup.send("❌ Play duration must be a positive number.", ephemeral=True) return play_duration = round(play_duration, 2) # Enqueue all sounds for sound_name, meta in bot.sounds.items(): sound_file = meta["file_path"] sound_duration = meta["duration"] # Determine individual play duration individual_play_duration = play_duration if play_duration else sound_duration # Adjust if play_duration exceeds actual duration if individual_play_duration > sound_duration: individual_play_duration = sound_duration if individual_play_duration <= 0: individual_play_duration = sound_duration for _ in range(repeat): await bot.queue[guild_id].put((sound_file, 1, individual_play_duration, speed)) logging.info(f"Enqueued sound '{sound_name}' {repeat} time(s) for Play All in guild {guild_id}.") # Update playback mode to parallel temporarily original_mode = bot.guild_modes.get(guild_id, 'queue') bot.guild_modes[guild_id] = 'parallel' # Cancel any existing disconnect task if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] # If not already playing, start playing if not voice_client.is_playing(): await play_next(guild_id) # Restore original playback mode after enqueueing bot.guild_modes[guild_id] = original_mode # Send confirmation with embed queue_length = bot.queue[guild_id].qsize() embed = discord.Embed( title="🎶 All Sounds Enqueued 🎶", description=f"All sounds have been enqueued for playback.", color=discord.Color.blue() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="Repeat Count", value=f"{repeat}", inline=False) embed.add_field(name="Play Duration", value=f"{play_duration} seconds" if play_duration else "Full duration", inline=False) embed.add_field(name="Speed", value=f"{speed}x", inline=False) embed.add_field(name="Total Sounds Enqueued", value=f"{len(bot.sounds) * repeat}", inline=False) embed.set_footer(text=f"Total in Queue: {queue_length}") await interaction.followup.send(embed=embed)🔹 Slash Command: Set Disconnect Duration After Queue Empty (Admin Only)
@bot.tree.command(name="set_disconnect_duration", description="Set how long the bot stays connected after the queue is empty. (Admin only)") @app_commands.describe( duration="Time in seconds to stay connected after queue is empty (minimum: 10)" ) @app_commands.default_permissions(administrator=True) async def set_disconnect_duration(interaction: discord.Interaction, duration: int): await interaction.response.defer(ephemeral=True)
# Validate duration parameter if duration < 10: await interaction.followup.send("❌ Duration must be at least 10 seconds.", ephemeral=True) return guild_id = interaction.guild.id bot.guild_durations[guild_id] = duration # If there's an existing disconnect task, cancel it if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] # Confirm setting with an embed embed = discord.Embed( title="⏰ Disconnect Duration Set ⏰", description=f"The bot will stay connected for {duration} seconds after the queue is empty.", color=discord.Color.blue() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) await interaction.followup.send(embed=embed, ephemeral=True)🔹 Slash Command: Skip the Currently Playing Sound
@bot.tree.command(name="skip", description="Skip the currently playing sound.") async def skip_sound(interaction: discord.Interaction): await interaction.response.defer() guild_id = interaction.guild.id voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id)
if voice_client and voice_client.is_playing(): voice_client.stop() logging.info(f"Skipped sound in guild {guild_id}.") await interaction.followup.send("✅ Skipped the current sound.") else: await interaction.followup.send("❌ No sound is currently playing.", ephemeral=True)🔹 Slash Command: Stop Playback and Clear Queue
@bot.tree.command(name="stop", description="Stop playback and clear the queue.") async def stop_sound(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id)
if guild_id in bot.queue: while not bot.queue[guild_id].empty(): bot.queue[guild_id].get_nowait() if guild_id in bot.parallel_audio_sources: del bot.parallel_audio_sources[guild_id] if voice_client and voice_client.is_connected(): voice_client.stop() await voice_client.disconnect() logging.info(f"Stopped playback and disconnected from guild {guild_id}.") await interaction.followup.send("✅ Stopped playback and cleared the queue.") else: await interaction.followup.send("❌ No active voice connection found.", ephemeral=True)🔹 Slash Command: Clear the Entire Queue
@bot.tree.command(name="clear", description="Clear the entire sound queue.") async def clear_queue(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id
if guild_id in bot.queue and not bot.queue[guild_id].empty(): while not bot.queue[guild_id].empty(): bot.queue[guild_id].get_nowait() # Cancel any disconnect tasks as the queue is now empty if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] await interaction.followup.send("✅ The queue has been cleared.") logging.info(f"Cleared the queue in guild {guild_id}.") else: await interaction.followup.send("❌ The queue is already empty.", ephemeral=True)🔹 Slash Command: List All Sounds with Durations
@bot.tree.command(name="list", description="List all available sounds.") async def list_sounds(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True)
if not bot.sounds: embed = discord.Embed( title="❌ No Sounds Available ❌", description="Add some sounds using the `/add` or `/add_from_url` commands.", color=discord.Color.red() ) await interaction.followup.send(embed=embed) return embed = discord.Embed( title="🎶 Available Sounds 🎶", description="Here are the sounds you can play:", color=discord.Color.green() ) # Discord embeds have a limit of 25 fields; handle accordingly sounds_sorted = sorted(bot.sounds.items(), key=lambda item: item[0].lower()) for name, meta in sounds_sorted: embed.add_field( name=name, value=f"Duration: {meta['duration']} seconds", inline=False ) if len(embed.fields) == 25: break # Prevent exceeding Discord's embed field limit await interaction.followup.send(embed=embed)🔹 Slash Command: Display the Current Sound Queue
@bot.tree.command(name="queue", description="Display the current sound queue.") async def display_queue(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id
if guild_id not in bot.queue or bot.queue[guild_id].empty(): embed = discord.Embed( title="🔄 Sound Queue 🔄", description="The queue is currently empty.", color=discord.Color.orange() ) await interaction.followup.send(embed=embed) return # Peek into the queue without removing items temp_list = list(bot.queue[guild_id].queue) embed = discord.Embed( title="🔄 Sound Queue 🔄", description="Here's the current queue:", color=discord.Color.purple() ) for idx, (sound, repeat, duration_play, speed) in enumerate(temp_list, start=1): sound_name = os.path.splitext(os.path.basename(sound))[0] embed.add_field( name=f"{idx}. {sound_name}", value=f"Repeat: {repeat} time(s)\nPlay Duration: {duration_play} seconds\nSpeed: {speed}x", inline=False ) if len(embed.fields) == 25: break # Prevent exceeding Discord's embed field limit await interaction.followup.send(embed=embed)🔹 Slash Command: Delete a Sound (Admin Only)
@bot.tree.command(name="delete", description="Delete a sound from the soundboard. (Admin only)") @app_commands.describe(name="Name of the sound to delete") @app_commands.default_permissions(administrator=True) async def delete_sound(interaction: discord.Interaction, name: str): await interaction.response.defer(ephemeral=True)
# Locate the sound file if name not in bot.sounds: await interaction.followup.send(f"❌ Sound `{name}` not found.", ephemeral=True) return sound_file = bot.sounds[name]["file_path"] try: os.remove(sound_file) del bot.sounds[name] bot.save_sounds() # Also remove the sound from all guild queues if present for guild_id, queue in bot.queue.items(): temp_queue = asyncio.Queue() while not queue.empty(): item = await queue.get() if item[0] != sound_file: await temp_queue.put(item) bot.queue[guild_id] = temp_queue # Confirm deletion with an embed embed = discord.Embed( title="🗑️ Sound Deleted 🗑️", description=f"Sound `{name}` has been deleted successfully.", color=discord.Color.dark_red() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) await interaction.followup.send(embed=embed, ephemeral=True) except Exception as e: logging.error(f"Failed to delete sound: {e}") await interaction.followup.send(f"❌ Failed to delete sound: {e}", ephemeral=True)🔹 Error handling for delete command permissions
@delete_sound.error async def delete_sound_error(interaction: discord.Interaction, error): if isinstance(error, app_commands.errors.MissingPermissions): await interaction.response.send_message("🔒 You don't have permission to delete sounds.", ephemeral=True) else: await interaction.response.send_message("⚠️ An error occurred while trying to delete the sound.", ephemeral=True)
🔹 Event: Bot Ready
@bot.event async def on_ready(): print(f'Bot connected as {bot.user}') # Removed Unicode emojis to prevent encoding errors logging.info(f'✅ Bot connected as {bot.user} ✅')
🔹 Run the bot
bot.run(TOKEN)
- Assistant
Certainly! Below is the complete code for your Discord bot that integrates AI chatbot functionality with customizable personalities, text and speech responses using Coqui TTS, and integrates with Ollama's
openhermesmodel. The bot listens to a specific channel (with ID1288185653494026292), maintains chat history, and responds based on the selected mode (text, speech, or both).Prerequisites
-
Dependencies: Make sure to install the necessary Python libraries. You can install them using
pip:pip install discord.py[voice] python-dotenv pydub requests coqui-tts asyncio -
FFmpeg: Ensure that FFmpeg is installed on your system and added to the system's PATH. You can download it from FFmpeg's official website.
-
Coqui TTS Models: Download and set up the desired Coqui TTS model with GPU support as per Coqui TTS documentation.
-
Ollama API: Ensure that Ollama's
openhermesmodel is accessible via API. You might need API keys or specific configurations based on Ollama's setup. -
Environment Variables: Create a
.envfile in your project directory with the following content:DISCORD_TOKEN=your_discord_bot_token OLLAMA_API_URL=your_ollama_api_endpoint OLLAMA_API_KEY=your_ollama_api_key COQUI_TTS_MODEL_PATH=path_to_your_coqui_tts_modelReplace the placeholders with your actual credentials and paths.
-
Personality Configuration: Create a
personality.jsonfile in the same directory as your bot script to define the AI's personality. Example:{ "personality": "You are a friendly and helpful assistant.", "additional_context": "Feel free to ask me anything!" }
Complete Bot Code
import discord from discord import app_commands from discord.ext import commands import os from dotenv import load_dotenv import asyncio from pydub import AudioSegment import requests import json import logging from typing import Optional, List, Dict from enum import Enum from discord.ui import View, Button from math import ceil import tempfile import aiohttp import subprocess # ======================= # Configuration & Setup # ======================= # Configure logging with timestamps logging.basicConfig( level=logging.INFO, format='%(asctime)s:%(levelname)s:%(name)s: %(message)s', datefmt='%Y-%m-%d %H:%M:%S' ) # Load environment variables from .env load_dotenv() TOKEN = os.getenv('DISCORD_TOKEN') OLLAMA_API_URL = os.getenv('OLLAMA_API_URL') # e.g., "http://localhost:11434/v1/models/openhermes/generate" OLLAMA_API_KEY = os.getenv('OLLAMA_API_KEY') # If required COQUI_TTS_MODEL_PATH = os.getenv('COQUI_TTS_MODEL_PATH') # Path to your Coqui TTS model # Define intents intents = discord.Intents.default() intents.message_content = True intents.guilds = True intents.voice_states = True # Playback modes class PlaybackMode(Enum): QUEUE = 'queue' SYNC = 'sync' PARALLEL = 'parallel' # Chat modes class ChatMode(Enum): TEXT = 'text' SPEECH = 'speech' BOTH = 'both' # Initialize the bot class SoundboardBot(commands.Bot): def __init__(self): super().__init__( command_prefix="!", # Prefix is not used for slash commands but required intents=intents, ) self.queue = {} # Dictionary to hold queues for each guild self.sounds = {} # Dictionary to hold sound metadata self.disconnect_tasks = {} # Dictionary to hold disconnect tasks for each guild self.guild_durations = {} # Dictionary to hold disconnect durations per guild self.guild_playback_modes = {} # Dictionary to hold playback modes per guild ('queue', 'sync', 'parallel') self.parallel_audio_sources = {} # Dictionary to hold ParallelAudioSource per guild self.chat_histories = {} # Dictionary to hold chat history per guild self.chat_modes = {} # Dictionary to hold chat modes per guild ('text', 'speech', 'both') self.personality = "" # AI personality loaded from JSON self.tts_model = None # Placeholder for Coqui TTS model async def setup_hook(self): # Sync the command tree with Discord await self.tree.sync() # Load sounds from JSON self.load_sounds() # Load personality configuration self.load_personality() # Initialize Coqui TTS await self.initialize_tts() def load_sounds(self): sounds_file = os.path.join("data", "sounds.json") if os.path.exists(sounds_file): with open(sounds_file, "r") as f: self.sounds = json.load(f) logging.info("Loaded sounds from sounds.json") else: self.sounds = {} os.makedirs(os.path.dirname(sounds_file), exist_ok=True) with open(sounds_file, "w") as f: json.dump(self.sounds, f, indent=4) logging.info("Created new sounds.json") def save_sounds(self): sounds_file = os.path.join("data", "sounds.json") with open(sounds_file, "w") as f: json.dump(self.sounds, f, indent=4) logging.info("Saved sounds to sounds.json") def load_personality(self): personality_file = os.path.join("data", "personality.json") if os.path.exists(personality_file): with open(personality_file, "r") as f: data = json.load(f) self.personality = data.get("personality", "") logging.info("Loaded personality from personality.json") else: # Default personality if not set self.personality = "You are a friendly and helpful assistant." os.makedirs(os.path.dirname(personality_file), exist_ok=True) with open(personality_file, "w") as f: json.dump({"personality": self.personality}, f, indent=4) logging.info("Created default personality.json") async def initialize_tts(self): if not COQUI_TTS_MODEL_PATH: logging.error("Coqui TTS model path not set in environment variables.") return try: # Initialize Coqui TTS model (Assuming you're using TTS library) from TTS.api import TTS self.tts_model = TTS(COQUI_TTS_MODEL_PATH, gpu=True) logging.info("Initialized Coqui TTS model with GPU support.") except Exception as e: logging.error(f"Failed to initialize Coqui TTS model: {e}") # =========================== # Event Handlers & Listeners # =========================== async def on_ready(self): print(f'Bot connected as {self.user}') logging.info(f'✅ Bot connected as {self.user} ✅') async def on_message(self, message: discord.Message): # Prevent the bot from responding to itself if message.author == self.user: return # Check if the message is in the specified channel if message.channel.id == 1288185653494026292: await self.handle_ai_chat(message) # Process commands if any await self.process_commands(message) async def handle_ai_chat(self, message: discord.Message): guild_id = message.guild.id # Initialize chat history if not present if guild_id not in self.chat_histories: self.chat_histories[guild_id] = [] if guild_id not in self.chat_modes: self.chat_modes[guild_id] = ChatMode.BOTH # Default mode username = message.author.display_name user_message = message.content # Append user message to chat history self.chat_histories[guild_id].append({"role": "user", "content": f"{username}: {user_message}"}) # Generate AI response ai_response = await self.get_ai_response(guild_id) if not ai_response: ai_response = "I'm sorry, I couldn't generate a response at this time." # Append AI response to chat history self.chat_histories[guild_id].append({"role": "assistant", "content": ai_response}) # Respond based on the current mode mode = self.chat_modes.get(guild_id, ChatMode.BOTH) if mode in [ChatMode.TEXT, ChatMode.BOTH]: await message.channel.send(f"{message.author.mention} {ai_response}") if mode in [ChatMode.SPEECH, ChatMode.BOTH]: # Generate speech audio_file = await self.generate_speech(ai_response) if audio_file: # Play audio in the user's current voice channel if message.author.voice and message.author.voice.channel: voice_channel = message.author.voice.channel voice_client = discord.utils.get(self.voice_clients, guild=message.guild) if not voice_client: try: voice_client = await voice_channel.connect() except Exception as e: logging.error(f"Failed to connect to voice channel: {e}") return elif voice_client.channel != voice_channel: try: await voice_client.move_to(voice_channel) except Exception as e: logging.error(f"Failed to move to voice channel: {e}") return # Play the audio def after_playing(error): if error: logging.error(f"Error playing AI speech: {error}") # Optionally disconnect after playing # asyncio.run_coroutine_threadsafe(voice_client.disconnect(), self.loop) source = discord.FFmpegPCMAudio(audio_file) voice_client.play(source, after=after_playing) else: logging.info("User is not in a voice channel. Cannot play speech.") else: logging.error("Failed to generate speech audio.") # =========================== # AI Interaction Functions # =========================== async def get_ai_response(self, guild_id: int) -> str: """ Sends the chat history to the AI model and retrieves a response. """ if not self.personality: logging.warning("Personality is not set. Using default.") self.personality = "You are a friendly and helpful assistant." # Prepare the payload for Ollama's API payload = { "prompt": f"{self.personality}\n{self.format_chat_history(guild_id)}\nAssistant:", "stream": False, "stop": ["\n"] } headers = { "Content-Type": "application/json" } if OLLAMA_API_KEY: headers["Authorization"] = f"Bearer {OLLAMA_API_KEY}" try: async with aiohttp.ClientSession() as session: async with session.post(OLLAMA_API_URL, json=payload, headers=headers) as resp: if resp.status == 200: data = await resp.json() ai_response = data.get("response", "") logging.info(f"AI Response: {ai_response}") return ai_response.strip() else: logging.error(f"Failed to get AI response. Status Code: {resp.status}") return "" except Exception as e: logging.error(f"Exception while getting AI response: {e}") return "" def format_chat_history(self, guild_id: int) -> str: """ Formats the chat history for the AI model. """ history = self.chat_histories.get(guild_id, []) formatted = "" for entry in history: if entry["role"] == "user": formatted += f"{entry['content']}\n" elif entry["role"] == "assistant": formatted += f"Assistant: {entry['content']}\n" return formatted async def generate_speech(self, text: str) -> Optional[str]: """ Generates speech from text using Coqui TTS and returns the path to the audio file. """ if not self.tts_model: logging.error("TTS model is not initialized.") return None try: # Use a temporary file to save the audio with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tf: audio_path = tf.name # Generate speech self.tts_model.tts_to_file(text=text, file_path=audio_path) logging.info(f"Generated speech audio at {audio_path}") return audio_path except Exception as e: logging.error(f"Failed to generate speech: {e}") return None # =========================== # Soundboard Functionality # =========================== async def play_sound_via_button(self, interaction: discord.Interaction, sound_name: str): """ Helper function to play sounds when triggered via soundboard buttons. Respects the current playback mode ('queue', 'sync', 'parallel'). """ # Check if the user is in a voice channel if not interaction.user.voice or not interaction.user.voice.channel: await interaction.response.send_message("❌ You need to be in a voice channel to use this button.", ephemeral=True) return voice_channel = interaction.user.voice.channel guild_id = interaction.guild.id # Locate the sound file if sound_name not in self.sounds: await interaction.response.send_message(f"❌ Sound `{sound_name}` not found.", ephemeral=True) return sound_file = self.sounds[sound_name]["file_path"] sound_duration = self.sounds[sound_name]["duration"] # Get the current playback mode mode = self.guild_playback_modes.get(guild_id, PlaybackMode.QUEUE.value) # Initialize the queue if not present if guild_id not in self.queue: self.queue[guild_id] = asyncio.Queue() # Connect to the voice channel if not already connected voice_client = discord.utils.get(self.voice_clients, guild__id=guild_id) if not voice_client: try: voice_client = await voice_channel.connect() logging.info(f"Connected to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to connect to voice channel: {e}") await interaction.response.send_message(f"❌ Failed to connect to voice channel: {e}", ephemeral=True) return else: if voice_client.channel != voice_channel: try: await voice_client.move_to(voice_channel) logging.info(f"Moved to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to move to voice channel: {e}") await interaction.response.send_message(f"❌ Failed to move to voice channel: {e}", ephemeral=True) return if mode == PlaybackMode.QUEUE.value: # Enqueue the sound with default speed=1.0 await self.queue[guild_id].put((sound_file, 1, sound_duration, 1.0)) logging.info(f"Enqueued sound '{sound_name}' in guild {guild_id} (Mode: Queue).") # Cancel any existing disconnect task if guild_id in self.disconnect_tasks: self.disconnect_tasks[guild_id].cancel() del self.disconnect_tasks[guild_id] # If not already playing, start playing if not voice_client.is_playing(): asyncio.create_task(self.play_next(guild_id)) await interaction.response.send_message(f"✅ Sound `{sound_name}` enqueued.", ephemeral=True) elif mode == PlaybackMode.SYNC.value: # Sync mode: Play immediately, interrupting current playback if voice_client.is_playing(): voice_client.stop() logging.info(f"Stopped current playback in guild {guild_id} for sync mode.") # Clear the queue if guild_id in self.queue: try: while not self.queue[guild_id].empty(): self.queue[guild_id].get_nowait() except: pass # Ignore if queue is already empty # Enqueue the sound with default speed=1.0 await self.queue[guild_id].put((sound_file, 1, sound_duration, 1.0)) logging.info(f"Queued sound '{sound_name}' in guild {guild_id} (Mode: Sync).") # Start playing immediately asyncio.create_task(self.play_next(guild_id)) # Cancel any existing disconnect task if guild_id in self.disconnect_tasks: self.disconnect_tasks[guild_id].cancel() del self.disconnect_tasks[guild_id] await interaction.response.send_message(f"✅ Sound `{sound_name}` played immediately (Sync Mode).", ephemeral=True) elif mode == PlaybackMode.PARALLEL.value: # Parallel mode: Play sounds simultaneously # Prepare the audio source with default speed=1.0 filters = self.generate_atempo_filter(1.0) before_options = f"-ss 0 -t {sound_duration}" options = f"-vn -filter:a {filters}" source = discord.FFmpegPCMAudio(sound_file, before_options=before_options, options=options) # Initialize ParallelAudioSource if not exists if guild_id not in self.parallel_audio_sources: self.parallel_audio_sources[guild_id] = ParallelAudioSource() # Add the source to ParallelAudioSource self.parallel_audio_sources[guild_id].add_source(source) # If not already playing, start playing if not voice_client.is_playing(): def after_playing(error): if error: logging.error(f"Error in parallel playback: {error}") asyncio.run_coroutine_threadsafe(self.handle_parallel_finish(guild_id), self.loop) voice_client.play(self.parallel_audio_sources[guild_id], after=after_playing) logging.info(f"Started parallel playback in guild {guild_id}.") await interaction.response.send_message(f"✅ Sound `{sound_name}` added to parallel playback.", ephemeral=True) # =========================== # Playback Management # =========================== async def play_next(self, guild_id: int): if guild_id not in self.queue: return if self.queue[guild_id].empty(): # Start a disconnect timer if guild_id in self.disconnect_tasks: self.disconnect_tasks[guild_id].cancel() del self.disconnect_tasks[guild_id] disconnect_task = asyncio.create_task(self.monitor_queue(guild_id)) self.disconnect_tasks[guild_id] = disconnect_task return sound_file, repeat, duration_play, speed = await self.queue[guild_id].get() voice_client = discord.utils.get(self.voice_clients, guild__id=guild_id) if not voice_client or not voice_client.is_connected(): logging.warning(f"No active voice client in guild {guild_id}.") return if voice_client.is_playing(): # Re-enqueue the sound if it's still playing await self.queue[guild_id].put((sound_file, repeat, duration_play, speed)) return # Play the sound with specified duration and speed try: # Prepare FFmpeg options to limit playback duration and set speed filters = self.generate_atempo_filter(speed) before_options = f"-ss 0 -t {duration_play}" options = f"-vn -filter:a {filters}" source = discord.FFmpegPCMAudio(sound_file, before_options=before_options, options=options) def after_playing(error): if error: logging.error(f"Error playing sound: {error}") asyncio.run_coroutine_threadsafe(self.play_next(guild_id), self.loop) voice_client.play(source, after=after_playing) sound_name = os.path.splitext(os.path.basename(sound_file))[0] logging.info(f"Playing sound '{sound_name}' in guild {guild_id}. Speed: {speed}x") except Exception as e: logging.error(f"Error playing sound {sound_file} in guild {guild_id}: {e}") # Proceed to next sound await self.play_next(guild_id) # Handle finishing of parallel playback async def handle_parallel_finish(self, guild_id: int): if guild_id in self.parallel_audio_sources: if self.parallel_audio_sources[guild_id].finished: # Start a disconnect timer if guild_id in self.disconnect_tasks: self.disconnect_tasks[guild_id].cancel() del self.disconnect_tasks[guild_id] disconnect_task = asyncio.create_task(self.monitor_queue(guild_id)) self.disconnect_tasks[guild_id] = disconnect_task del self.parallel_audio_sources[guild_id] logging.info(f"Parallel playback finished in guild {guild_id}.") # Helper function to handle disconnect after duration async def monitor_queue(self, guild_id: int): duration = self.guild_durations.get(guild_id, 60) # Default to 60 seconds logging.info(f"Monitoring guild {guild_id} for disconnect in {duration} seconds.") await asyncio.sleep(duration) voice_client = discord.utils.get(self.voice_clients, guild__id=guild_id) if voice_client and voice_client.is_connected(): await voice_client.disconnect() logging.info(f"Disconnected from guild {guild_id} after {duration} seconds of inactivity.") # Helper function to generate atempo filters based on desired speed def generate_atempo_filter(self, speed: float) -> str: if speed <= 0: raise ValueError("Speed must be a positive number.") filters = [] while speed > 2.0 or speed < 0.5: if speed > 2.0: filters.append("atempo=2.0") speed /= 2.0 elif speed < 0.5: filters.append("atempo=0.5") speed /= 0.5 filters.append(f"atempo={speed}") return ",".join(filters) # =========================== # Soundboard Commands # =========================== # Directory to store sound files SOUND_DIR = "sounds" # Ensure the sounds and data directories exist for directory in [SOUND_DIR, "data"]: if not os.path.exists(directory): os.makedirs(directory) logging.info(f"Created directory: {directory}") # Allowed audio formats ALLOWED_EXTENSIONS = ('.mp3', '.wav', '.ogg', '.flac') # Maximum file size (in bytes) for uploads via `/add` command MAX_FILE_SIZE = 8 * 1024 * 1024 # 8MB # Helper function to extract duration of audio file in seconds def get_audio_duration(file_path): try: audio = AudioSegment.from_file(file_path) duration = len(audio) / 1000 # pydub works in milliseconds return round(duration, 2) except Exception as e: logging.error(f"Error getting duration of {file_path}: {e}") return None # Helper function to download a file from a URL async def download_file(url, dest): loop = asyncio.get_event_loop() try: response = requests.get(url, stream=True, timeout=120) response.raise_for_status() # Download in chunks to handle large files efficiently with open(dest, "wb") as f: for chunk in response.iter_content(chunk_size=8192): if chunk: f.write(chunk) return True, "" except requests.exceptions.RequestException as e: return False, str(e) # =========================== # Soundboard Command with Pagination # Utilizing discord.ui for interactive components # =========================== class SoundboardView(View): def __init__(self, bot: SoundboardBot, guild_id: int, current_page: int = 1, sounds_per_page: int = 20): super().__init__(timeout=None) self.bot = bot self.guild_id = guild_id self.current_page = current_page self.sounds_per_page = sounds_per_page self.sounds_sorted = sorted(bot.sounds.keys(), key=lambda x: x.lower()) self.total_sounds = len(self.sounds_sorted) self.total_pages = ceil(self.total_sounds / self.sounds_per_page) if self.sounds_per_page else 1 self.update_buttons() def update_buttons(self): # Clear existing buttons to update the view self.clear_items() start_index = (self.current_page - 1) * self.sounds_per_page end_index = start_index + self.sounds_per_page sounds_on_page = self.sounds_sorted[start_index:end_index] # Add sound buttons for sound_name in sounds_on_page: self.add_item(SoundButton(sound_name, self.bot, self.guild_id)) # Add navigation buttons if necessary if self.total_pages > 1: if self.current_page > 1: self.add_item(PreviousPageButton()) if self.current_page < self.total_pages: self.add_item(NextPageButton()) class PreviousPageButton(Button): def __init__(self): super().__init__(style=discord.ButtonStyle.secondary, label="⬅️ Previous", custom_id="previous_page") async def callback(self, interaction: discord.Interaction): view: SoundboardView = self.view if view.current_page > 1: view.current_page -= 1 view.update_buttons() # Retrieve the existing embed if interaction.message.embeds: embed = interaction.message.embeds[0] else: embed = discord.Embed( title="🎵 Soundboard 🎵", description="Click the buttons below to play sounds.", color=discord.Color.blue() ) # Update the footer embed.set_footer(text=f"Page {view.current_page} of {view.total_pages}") await interaction.response.edit_message(embed=embed, view=view) else: await interaction.response.defer() class NextPageButton(Button): def __init__(self): super().__init__(style=discord.ButtonStyle.secondary, label="Next ➡️", custom_id="next_page") async def callback(self, interaction: discord.Interaction): view: SoundboardView = self.view if view.current_page < view.total_pages: view.current_page += 1 view.update_buttons() # Retrieve the existing embed if interaction.message.embeds: embed = interaction.message.embeds[0] else: embed = discord.Embed( title="🎵 Soundboard 🎵", description="Click the buttons below to play sounds.", color=discord.Color.blue() ) # Update the footer embed.set_footer(text=f"Page {view.current_page} of {view.total_pages}") await interaction.response.edit_message(embed=embed, view=view) else: await interaction.response.defer() class SoundButton(Button): def __init__(self, sound_name: str, bot: SoundboardBot, guild_id: int): super().__init__( style=discord.ButtonStyle.primary, label=sound_name, custom_id=f'soundboard_sound_{sound_name}' ) self.sound_name = sound_name self.bot = bot self.guild_id = guild_id async def callback(self, interaction: discord.Interaction): await self.bot.play_sound_via_button(interaction, self.sound_name) # 🟡 Registering the Soundboard Command @bot.tree.command(name="soundboard", description="Display the soundboard with clickable buttons for sounds.") async def soundboard(interaction: discord.Interaction): await interaction.response.defer() if not bot.sounds: embed = discord.Embed( title="❌ No Sounds Available ❌", description="Add some sounds using the `/add` or `/add_from_url` commands.", color=discord.Color.red() ) await interaction.followup.send(embed=embed) return # Create the view with sound buttons and pagination view = SoundboardView(bot, interaction.guild.id) embed = discord.Embed( title="🎵 Soundboard 🎵", description="Click the buttons below to play sounds.", color=discord.Color.blue() ) # Add page information embed.set_footer(text=f"Page {view.current_page} of {view.total_pages}") await interaction.followup.send(embed=embed, view=view) # 🔹 Slash Command: Set Playback Mode @bot.tree.command(name="set_mode", description="Set the playback mode for the bot.") @app_commands.describe( mode='Playback mode to set: "queue", "sync", or "parallel"' ) async def set_mode(interaction: discord.Interaction, mode: PlaybackMode): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id bot.guild_playback_modes[guild_id] = mode.value logging.info(f"Guild {guild_id} playback mode changed to {mode.value.capitalize()}.") await interaction.followup.send(f"✅ Playback mode set to '{mode.value}'.", ephemeral=True) # 🔹 Slash Command: Add a sound via file upload @bot.tree.command(name="add", description="Add a new sound to the soundboard.") @app_commands.describe( name="Name for the sound", file="Audio file to upload (MP3, WAV, OGG, FLAC)" ) async def add_sound(interaction: discord.Interaction, name: str, file: discord.Attachment): await interaction.response.defer(ephemeral=True) # Validate file extension if not any(file.filename.lower().endswith(ext) for ext in ALLOWED_EXTENSIONS): await interaction.followup.send( f"❌ Unsupported file format. Please upload a file with one of the following extensions: {', '.join(ALLOWED_EXTENSIONS)}.", ephemeral=True ) return # Validate file size if file.size > MAX_FILE_SIZE: await interaction.followup.send( f"❌ File size exceeds the 8MB limit.", ephemeral=True ) return # Sanitize the sound name to prevent filesystem issues safe_name = "".join([c if c.isalnum() or c in (' ', '_') else '_' for c in name]).strip() if not safe_name: await interaction.followup.send( "❌ Invalid sound name. Please use alphanumeric characters, spaces, or underscores.", ephemeral=True ) return file_extension = os.path.splitext(file.filename)[1] file_path = os.path.join(SOUND_DIR, f"{safe_name}{file_extension}") # Check for name conflicts if any(safe_name.lower() == existing.lower() for existing in bot.sounds.keys()): await interaction.followup.send( f"❌ A sound with the name `{safe_name}` already exists.", ephemeral=True ) return try: # Save the uploaded file await file.save(file_path) # Extract duration duration = get_audio_duration(file_path) if duration is None: os.remove(file_path) await interaction.followup.send( "❌ Failed to extract duration from the audio file. Ensure it's a valid audio file.", ephemeral=True ) return # Add sound metadata bot.sounds[safe_name] = { "file_path": file_path, "duration": duration } bot.save_sounds() # Confirm addition with an embed embed = discord.Embed( title="✅ Sound Added ✅", description=f"Sound `{safe_name}` has been added successfully.", color=discord.Color.green() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="File", value=f"[{file.filename}]({file.url})", inline=False) embed.add_field(name="Duration", value=f"{duration} seconds", inline=False) await interaction.followup.send(embed=embed, ephemeral=True) except Exception as e: logging.error(f"Failed to add sound: {e}") await interaction.followup.send(f"❌ Failed to add sound: {e}", ephemeral=True) # 🔹 Slash Command: Add a sound from a URL (no file upload limit) @bot.tree.command(name="add_from_url", description="Add a new sound to the soundboard by downloading from a URL.") @app_commands.describe( name="Name for the sound", url="Direct URL to the audio file (MP3, WAV, OGG, FLAC)" ) async def add_from_url(interaction: discord.Interaction, name: str, url: str): await interaction.response.defer(ephemeral=True) # Validate file extension if not any(url.lower().endswith(ext) for ext in ALLOWED_EXTENSIONS): await interaction.followup.send( f"❌ Unsupported file format. Please provide a URL ending with one of the following extensions: {', '.join(ALLOWED_EXTENSIONS)}.", ephemeral=True ) return # Sanitize the sound name safe_name = "".join([c if c.isalnum() or c in (' ', '_') else '_' for c in name]).strip() if not safe_name: await interaction.followup.send( "❌ Invalid sound name. Please use alphanumeric characters, spaces, or underscores.", ephemeral=True ) return file_extension = os.path.splitext(url)[1] file_path = os.path.join(SOUND_DIR, f"{safe_name}{file_extension}") # Check for name conflicts if any(safe_name.lower() == existing.lower() for existing in bot.sounds.keys()): await interaction.followup.send( f"❌ A sound with the name `{safe_name}` already exists.", ephemeral=True ) return try: # Download the file success, error = await download_file(url, file_path) if not success: await interaction.followup.send( f"❌ Failed to download the file: {error}", ephemeral=True ) return # Extract duration duration = get_audio_duration(file_path) if duration is None: os.remove(file_path) await interaction.followup.send( "❌ Failed to extract duration from the audio file. Ensure it's a valid audio file.", ephemeral=True ) return # Add sound metadata bot.sounds[safe_name] = { "file_path": file_path, "duration": duration } bot.save_sounds() # Confirm addition with an embed embed = discord.Embed( title="✅ Sound Added from URL ✅", description=f"Sound `{safe_name}` has been added successfully from the provided URL.", color=discord.Color.green() ) embed.add_field(name="URL", value=f"[Download Here]({url})", inline=False) embed.add_field(name="Duration", value=f"{duration} seconds", inline=False) await interaction.followup.send(embed=embed, ephemeral=True) except Exception as e: logging.error(f"Failed to add sound from URL: {e}") # Clean up if download failed if os.path.exists(file_path): os.remove(file_path) await interaction.followup.send(f"❌ Failed to add sound from URL: {e}", ephemeral=True) # 🔹 Slash Command: Play a sound with optional repeat and speed @bot.tree.command(name="play", description="Play a sound in your current voice channel.") @app_commands.describe( name="Name of the sound to play", repeat="Number of times to play the sound (default: 1)", play_duration="Time in seconds to play the sound (default: full duration)", speed="Playback speed (e.g., 1.0 is normal, 2.0 is 2x speed) (optional)" ) async def play_sound(interaction: discord.Interaction, name: str, repeat: int = 1, play_duration: Optional[float] = None, speed: Optional[float] = 1.0): await interaction.response.defer() guild_id = interaction.guild.id # Validate repeat parameter if repeat < 1: await interaction.followup.send("❌ Repeat count must be at least 1.", ephemeral=True) return # Validate speed parameter if speed <= 0: await interaction.followup.send("❌ Speed must be a positive number.", ephemeral=True) return # Check if the user is in a voice channel if not interaction.user.voice or not interaction.user.voice.channel: await interaction.followup.send("❌ You need to be in a voice channel to use this command.", ephemeral=True) return voice_channel = interaction.user.voice.channel # Locate the sound file if name not in bot.sounds: await interaction.followup.send(f"❌ Sound `{name}` not found.", ephemeral=True) return sound_file = bot.sounds[name]["file_path"] sound_duration = bot.sounds[name]["duration"] # Determine play duration if play_duration is None: play_duration = sound_duration elif play_duration > sound_duration: play_duration = sound_duration elif play_duration <= 0: await interaction.followup.send("❌ Play duration must be a positive number.", ephemeral=True) return play_duration = round(play_duration, 2) # Ensure it's rounded to 2 decimal places # Get the current playback mode mode = bot.guild_playback_modes.get(guild_id, PlaybackMode.QUEUE.value) # Initialize the queue if not present if guild_id not in bot.queue: bot.queue[guild_id] = asyncio.Queue() # Connect to the voice channel if not already connected voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id) if not voice_client: try: voice_client = await voice_channel.connect() logging.info(f"Connected to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to connect to voice channel: {e}") await interaction.followup.send(f"❌ Failed to connect to voice channel: {e}", ephemeral=True) return else: if voice_client.channel != voice_channel: try: await voice_client.move_to(voice_channel) logging.info(f"Moved to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to move to voice channel: {e}") await interaction.followup.send(f"❌ Failed to move to voice channel: {e}", ephemeral=True) return if mode == PlaybackMode.QUEUE.value: # Enqueue the sound with specified speed for _ in range(repeat): await bot.queue[guild_id].put((sound_file, 1, play_duration, speed)) # Each enqueue has repeat=1 logging.info(f"Enqueued sound '{name}' {repeat} time(s) in guild {guild_id} (Mode: Queue).") # Cancel any existing disconnect task if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] # If the voice client is not playing, start playing the next sound if not voice_client.is_playing(): asyncio.create_task(bot.play_next(guild_id)) # Send confirmation with embed queue_length = bot.queue[guild_id].qsize() embed = discord.Embed( title="🎶 Sound Enqueued 🎶", description=f"Sound `{name}` has been added to the queue.", color=discord.Color.blue() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="Repeat Count", value=f"{repeat}", inline=False) embed.add_field(name="Play Duration", value=f"{play_duration} seconds", inline=False) embed.add_field(name="Speed", value=f"{speed}x", inline=False) embed.add_field(name="Position in Queue", value=f"{queue_length}", inline=False) embed.set_footer(text=f"Total in Queue: {queue_length}") await interaction.followup.send(embed=embed) elif mode == PlaybackMode.SYNC.value: # Sync mode: Play immediately, interrupting current playback if voice_client.is_playing(): voice_client.stop() logging.info(f"Stopped current playback in guild {guild_id} for sync mode.") # Clear the queue if guild_id in bot.queue: try: while not bot.queue[guild_id].empty(): bot.queue[guild_id].get_nowait() except: pass # Ignore if queue is already empty # Enqueue the sound with specified speed for _ in range(repeat): await bot.queue[guild_id].put((sound_file, 1, play_duration, speed)) # Each enqueue has repeat=1 logging.info(f"Enqueued sound '{name}' {repeat} time(s) in guild {guild_id} (Mode: Sync).") # Start playing immediately asyncio.create_task(bot.play_next(guild_id)) # Cancel any existing disconnect task if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] # Send confirmation with embed embed = discord.Embed( title="🎶 Sound Played 🎶", description=f"Sound `{name}` has been played immediately (Sync Mode).", color=discord.Color.orange() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="Repeat Count", value=f"{repeat}", inline=False) embed.add_field(name="Play Duration", value=f"{play_duration} seconds", inline=False) embed.add_field(name="Speed", value=f"{speed}x", inline=False) await interaction.followup.send(embed=embed) elif mode == PlaybackMode.PARALLEL.value: # Parallel mode: Play sounds simultaneously # Prepare the audio sources with specified speed for _ in range(repeat): filters = self.generate_atempo_filter(speed) before_options = f"-ss 0 -t {play_duration}" options = f"-vn -filter:a {filters}" source = discord.FFmpegPCMAudio(sound_file, before_options=before_options, options=options) # Initialize ParallelAudioSource if not exists if guild_id not in bot.parallel_audio_sources: bot.parallel_audio_sources[guild_id] = ParallelAudioSource() # Add the source to ParallelAudioSource bot.parallel_audio_sources[guild_id].add_source(source) logging.info(f"Added sound '{name}' at speed {speed}x to parallel playback in guild {guild_id}.") # If not already playing, start playing if not voice_client.is_playing(): def after_playing(error): if error: logging.error(f"Error in parallel playback: {error}") asyncio.run_coroutine_threadsafe(bot.handle_parallel_finish(guild_id), bot.loop) voice_client.play(bot.parallel_audio_sources[guild_id], after=after_playing) logging.info(f"Started parallel playback in guild {guild_id}.") # Send confirmation with embed embed = discord.Embed( title="🎶 Sound Added to Parallel Playback 🎶", description=f"Sound `{name}` has been added to parallel playback.", color=discord.Color.green() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="Repeat Count", value=f"{repeat}", inline=False) embed.add_field(name="Play Duration", value=f"{play_duration} seconds", inline=False) embed.add_field(name="Speed", value=f"{speed}x", inline=False) await interaction.followup.send(embed=embed) # 🔹 Slash Command: Play All Sounds @bot.tree.command(name="play_all", description="Play all sounds at once in your current voice channel.") @app_commands.describe( repeat="Number of times to play each sound (default: 1)", play_duration="Time in seconds to play each sound (default: full duration)", speed="Playback speed for all sounds (e.g., 1.0 is normal, 2.0 is 2x speed) (optional)" ) async def play_all_sounds(interaction: discord.Interaction, repeat: int = 1, play_duration: Optional[float] = None, speed: Optional[float] = 1.0): await interaction.response.defer() guild_id = interaction.guild.id # Validate repeat parameter if repeat < 1: await interaction.followup.send("❌ Repeat count must be at least 1.", ephemeral=True) return # Validate speed parameter if speed <= 0: await interaction.followup.send("❌ Speed must be a positive number.", ephemeral=True) return # Check if there are sounds available if not bot.sounds: await interaction.followup.send("❌ No sounds available to play.", ephemeral=True) return # Check if the user is in a voice channel if not interaction.user.voice or not interaction.user.voice.channel: await interaction.followup.send("❌ You need to be in a voice channel to use this command.", ephemeral=True) return voice_channel = interaction.user.voice.channel # Initialize the queue if not present if guild_id not in bot.queue: bot.queue[guild_id] = asyncio.Queue() # Connect to the voice channel if not already connected voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id) if not voice_client: try: voice_client = await voice_channel.connect() logging.info(f"Connected to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to connect to voice channel: {e}") await interaction.followup.send(f"❌ Failed to connect to voice channel: {e}", ephemeral=True) return else: if voice_client.channel != voice_channel: try: await voice_client.move_to(voice_channel) logging.info(f"Moved to voice channel {voice_channel} in guild {guild_id}.") except Exception as e: logging.error(f"Failed to move to voice channel: {e}") await interaction.followup.send(f"❌ Failed to move to voice channel: {e}", ephemeral=True) return # Determine play duration # Assuming all sounds play the same duration first_sound = next(iter(bot.sounds.values()), None) if first_sound: default_play_duration = first_sound["duration"] if play_duration is None: play_duration = default_play_duration elif play_duration > default_play_duration: play_duration = default_play_duration elif play_duration <= 0: await interaction.followup.send("❌ Play duration must be a positive number.", ephemeral=True) return play_duration = round(play_duration, 2) else: await interaction.followup.send("❌ No sounds available to play.", ephemeral=True) return # Enqueue all sounds for sound_name, meta in bot.sounds.items(): sound_file = meta["file_path"] sound_duration = meta["duration"] # Determine individual play duration individual_play_duration = play_duration if play_duration else sound_duration # Adjust if play_duration exceeds actual duration if individual_play_duration > sound_duration: individual_play_duration = sound_duration if individual_play_duration <= 0: individual_play_duration = sound_duration for _ in range(repeat): await bot.queue[guild_id].put((sound_file, 1, individual_play_duration, speed)) logging.info(f"Enqueued sound '{sound_name}' {repeat} time(s) for Play All in guild {guild_id}.") # Update playback mode to parallel temporarily original_mode = bot.guild_playback_modes.get(guild_id, PlaybackMode.QUEUE.value) bot.guild_playback_modes[guild_id] = PlaybackMode.PARALLEL.value # Cancel any existing disconnect task if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] # If not already playing, start playing if not voice_client.is_playing(): asyncio.create_task(bot.play_next(guild_id)) # Restore original playback mode after enqueueing bot.guild_playback_modes[guild_id] = original_mode # Send confirmation with embed queue_length = bot.queue[guild_id].qsize() embed = discord.Embed( title="🎶 All Sounds Enqueued 🎶", description=f"All sounds have been enqueued for playback.", color=discord.Color.blue() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) embed.add_field(name="Repeat Count", value=f"{repeat}", inline=False) embed.add_field(name="Play Duration", value=f"{play_duration} seconds" if play_duration else "Full duration", inline=False) embed.add_field(name="Speed", value=f"{speed}x", inline=False) embed.add_field(name="Total Sounds Enqueued", value=f"{len(bot.sounds) * repeat}", inline=False) embed.set_footer(text=f"Total in Queue: {queue_length}") await interaction.followup.send(embed=embed) # 🔹 Slash Command: Set Disconnect Duration After Queue Empty (Admin Only) @bot.tree.command(name="set_disconnect_duration", description="Set how long the bot stays connected after the queue is empty. (Admin only)") @app_commands.describe( duration="Time in seconds to stay connected after queue is empty (minimum: 10)" ) @app_commands.default_permissions(administrator=True) async def set_disconnect_duration(interaction: discord.Interaction, duration: int): await interaction.response.defer(ephemeral=True) # Validate duration parameter if duration < 10: await interaction.followup.send("❌ Duration must be at least 10 seconds.", ephemeral=True) return guild_id = interaction.guild.id bot.guild_durations[guild_id] = duration # If there's an existing disconnect task, cancel it if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] # Confirm setting with an embed embed = discord.Embed( title="⏰ Disconnect Duration Set ⏰", description=f"The bot will stay connected for {duration} seconds after the queue is empty.", color=discord.Color.blue() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) await interaction.followup.send(embed=embed, ephemeral=True) # 🔹 Slash Command: Skip the Currently Playing Sound @bot.tree.command(name="skip", description="Skip the currently playing sound.") async def skip_sound(interaction: discord.Interaction): await interaction.response.defer() guild_id = interaction.guild.id voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id) if voice_client and voice_client.is_playing(): voice_client.stop() logging.info(f"Skipped sound in guild {guild_id}.") await interaction.followup.send("✅ Skipped the current sound.") else: await interaction.followup.send("❌ No sound is currently playing.", ephemeral=True) # 🔹 Slash Command: Stop Playback and Clear Queue @bot.tree.command(name="stop", description="Stop playback and clear the queue.") async def stop_sound(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id voice_client = discord.utils.get(bot.voice_clients, guild__id=guild_id) if guild_id in bot.queue: while not bot.queue[guild_id].empty(): bot.queue[guild_id].get_nowait() if guild_id in bot.parallel_audio_sources: del bot.parallel_audio_sources[guild_id] if voice_client and voice_client.is_connected(): voice_client.stop() await voice_client.disconnect() logging.info(f"Stopped playback and disconnected from guild {guild_id}.") await interaction.followup.send("✅ Stopped playback and cleared the queue.") else: await interaction.followup.send("❌ No active voice connection found.", ephemeral=True) # 🔹 Slash Command: Clear the Entire Queue @bot.tree.command(name="clear", description="Clear the entire sound queue.") async def clear_queue(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id if guild_id in bot.queue and not bot.queue[guild_id].empty(): while not bot.queue[guild_id].empty(): bot.queue[guild_id].get_nowait() # Cancel any disconnect tasks as the queue is now empty if guild_id in bot.disconnect_tasks: bot.disconnect_tasks[guild_id].cancel() del bot.disconnect_tasks[guild_id] await interaction.followup.send("✅ The queue has been cleared.") logging.info(f"Cleared the queue in guild {guild_id}.") else: await interaction.followup.send("❌ The queue is already empty.", ephemeral=True) # 🔹 Slash Command: List All Sounds with Durations @bot.tree.command(name="list", description="List all available sounds.") async def list_sounds(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) if not bot.sounds: embed = discord.Embed( title="❌ No Sounds Available ❌", description="Add some sounds using the `/add` or `/add_from_url` commands.", color=discord.Color.red() ) await interaction.followup.send(embed=embed) return embed = discord.Embed( title="🎶 Available Sounds 🎶", description="Here are the sounds you can play:", color=discord.Color.green() ) # Discord embeds have a limit of 25 fields; handle accordingly sounds_sorted = sorted(bot.sounds.items(), key=lambda item: item[0].lower()) for name, meta in sounds_sorted: embed.add_field( name=name, value=f"Duration: {meta['duration']} seconds", inline=False ) if len(embed.fields) == 25: break # Prevent exceeding Discord's embed field limit await interaction.followup.send(embed=embed) # 🔹 Slash Command: Display the Current Sound Queue @bot.tree.command(name="queue", description="Display the current sound queue.") async def display_queue(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id if guild_id not in bot.queue or bot.queue[guild_id].empty(): embed = discord.Embed( title="🔄 Sound Queue 🔄", description="The queue is currently empty.", color=discord.Color.orange() ) await interaction.followup.send(embed=embed) return # Peek into the queue without removing items temp_list = list(bot.queue[guild_id].queue) embed = discord.Embed( title="🔄 Sound Queue 🔄", description="Here's the current queue:", color=discord.Color.purple() ) for idx, (sound, repeat, duration_play, speed) in enumerate(temp_list, start=1): sound_name = os.path.splitext(os.path.basename(sound))[0] embed.add_field( name=f"{idx}. {sound_name}", value=f"Repeat: {repeat} time(s)\nPlay Duration: {duration_play} seconds\nSpeed: {speed}x", inline=False ) if len(embed.fields) == 25: break # Prevent exceeding Discord's embed field limit await interaction.followup.send(embed=embed) # 🔹 Slash Command: Delete a Sound (Admin Only) @bot.tree.command(name="delete", description="Delete a sound from the soundboard. (Admin only)") @app_commands.describe(name="Name of the sound to delete") @app_commands.default_permissions(administrator=True) async def delete_sound(interaction: discord.Interaction, name: str): await interaction.response.defer(ephemeral=True) # Locate the sound file if name not in bot.sounds: await interaction.followup.send(f"❌ Sound `{name}` not found.", ephemeral=True) return sound_file = bot.sounds[name]["file_path"] try: os.remove(sound_file) del bot.sounds[name] bot.save_sounds() # Also remove the sound from all guild queues if present for guild_id, queue in bot.queue.items(): temp_queue = asyncio.Queue() while not queue.empty(): item = await queue.get() if item[0] != sound_file: await temp_queue.put(item) bot.queue[guild_id] = temp_queue # Confirm deletion with an embed embed = discord.Embed( title="🗑️ Sound Deleted 🗑️", description=f"Sound `{name}` has been deleted successfully.", color=discord.Color.dark_red() ) if interaction.user.avatar: embed.set_thumbnail(url=interaction.user.avatar.url) await interaction.followup.send(embed=embed, ephemeral=True) except Exception as e: logging.error(f"Failed to delete sound: {e}") await interaction.followup.send(f"❌ Failed to delete sound: {e}", ephemeral=True) # 🔹 Error handling for delete command permissions @delete_sound.error async def delete_sound_error(interaction: discord.Interaction, error): if isinstance(error, app_commands.errors.MissingPermissions): await interaction.response.send_message("🔒 You don't have permission to delete sounds.", ephemeral=True) else: await interaction.response.send_message("⚠️ An error occurred while trying to delete the sound.", ephemeral=True) # =========================== # AI Chatbot Commands # =========================== # 🔹 Slash Command: Set Chat Mode (Text, Speech, Both) @bot.tree.command(name="set_chat_mode", description="Set the chat response mode for the AI chatbot.") @app_commands.describe( mode='Chat mode to set: "text", "speech", or "both"' ) async def set_chat_mode(interaction: discord.Interaction, mode: ChatMode): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id bot.chat_modes[guild_id] = mode.value logging.info(f"Guild {guild_id} chat mode changed to {mode.value.capitalize()}.") await interaction.followup.send(f"✅ Chat mode set to '{mode.value}'.", ephemeral=True) # 🔹 Slash Command: Set AI Personality (Admin Only) @bot.tree.command(name="set_personality", description="Set the AI chatbot's personality. (Admin only)") @app_commands.describe( personality="A description of the AI's personality." ) @app_commands.default_permissions(administrator=True) async def set_personality(interaction: discord.Interaction, personality: str): await interaction.response.defer(ephemeral=True) personality_file = os.path.join("data", "personality.json") bot.personality = personality try: with open(personality_file, "w") as f: json.dump({"personality": personality}, f, indent=4) logging.info("Updated personality.json") await interaction.followup.send("✅ AI personality has been updated successfully.", ephemeral=True) except Exception as e: logging.error(f"Failed to update personality: {e}") await interaction.followup.send(f"❌ Failed to update personality: {e}", ephemeral=True) # 🔹 Slash Command: Show Chat Mode @bot.tree.command(name="show_chat_mode", description="Show the current chat response mode.") async def show_chat_mode(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) guild_id = interaction.guild.id mode = bot.chat_modes.get(guild_id, ChatMode.BOTH.value) embed = discord.Embed( title="📝 Current Chat Mode 📝", description=f"The current chat response mode is set to **{mode.capitalize()}**.", color=discord.Color.blue() ) await interaction.followup.send(embed=embed, ephemeral=True) # 🔹 Slash Command: Show AI Personality @bot.tree.command(name="show_personality", description="Show the AI chatbot's current personality.") async def show_personality(interaction: discord.Interaction): await interaction.response.defer(ephemeral=True) embed = discord.Embed( title="🤖 AI Personality 🤖", description=f"{bot.personality}", color=discord.Color.green() ) await interaction.followup.send(embed=embed, ephemeral=True) # =========================== # Run the bot # =========================== def run_bot(self): self.run(TOKEN) # ======================= # Audio Source for Parallel Playback # ======================= class ParallelAudioSource(discord.AudioSource): def __init__(self): self.sources: List[discord.AudioSource] = [] self.finished = False def add_source(self, source: discord.AudioSource): self.sources.append(source) def is_opus(self): return False def read(self): if not self.sources: self.finished = True return b'' frames = [] for source in self.sources[:]: frame = source.read() if frame: frames.append(frame) else: # EOF for this source self.sources.remove(source) if not frames: self.finished = True return b'' # Assume all frames are the same size frame_size = len(frames[0]) mixed_frame = bytearray(frame_size) # Mixing frames for i in range(0, frame_size, 2): sample_sum = 0 for frame in frames: # Get the sample from the frame sample = int.from_bytes(frame[i:i+2], 'little', signed=True) sample_sum += sample # Prevent clipping sample_sum = max(min(sample_sum, 32767), -32768) # Write back to mixed_frame mixed_frame[i:i+2] = int(sample_sum).to_bytes(2, 'little', signed=True) return bytes(mixed_frame) # ======================= # Instantiate and Run the Bot # ======================= bot = SoundboardBot() bot.run_bot()Explanation of the Added Functionality
-
AI Chatbot Integration:
- The bot listens to messages in the channel with ID
1288185653494026292. - When a user sends a message in this channel, the bot appends the message to the chat history and sends the entire conversation to the Ollama
openhermesmodel to generate a response. - The response is then sent back to the channel as text, speech, or both based on the current chat mode.
- The bot listens to messages in the channel with ID
-
Chat Modes:
- Text: The bot responds with text messages.
- Speech: The bot responds by generating speech using Coqui TTS and playing it in the user's current voice channel.
- Both: The bot responds with both text and speech.
- Users can set the chat mode using the
/set_chat_modecommand and view the current mode with/show_chat_mode.
-
AI Personality:
- The bot's personality is defined in a
personality.jsonfile. - Administrators can set the AI's personality using the
/set_personalitycommand and view it with/show_personality.
- The bot's personality is defined in a
-
Chat History:
- The bot maintains a chat history for each guild (server) to provide context to the AI model. This ensures more coherent and contextually relevant responses.
-
Speech Generation with Coqui TTS:
- The bot uses Coqui TTS to convert AI-generated text responses into speech.
- Generated audio files are stored temporarily and played in the user's current voice channel.
- The bot handles connecting to and moving between voice channels as needed.
-
Error Handling & Logging:
- Comprehensive logging is implemented to track the bot's operations and catch any errors during execution.
- Users receive appropriate feedback in Discord based on the success or failure of commands.
-
Administrator Controls:
- Certain commands like setting the AI personality and disconnect durations are restricted to administrators to prevent misuse.
Commands Summary
-
Soundboard Commands:
/soundboard: Displays the soundboard with clickable buttons to play sounds./add: Adds a new sound via file upload./add_from_url: Adds a new sound by providing a direct URL to the audio file./play: Plays a specific sound with optional repeat count, duration, and speed./play_all: Plays all sounds in the queue with optional repeat count, duration, and speed./set_mode: Sets the playback mode (queue,sync, orparallel)./set_disconnect_duration: Sets how long the bot stays connected after the queue is empty (Admin Only)./skip: Skips the currently playing sound./stop: Stops playback and clears the queue./clear: Clears the entire sound queue./list: Lists all available sounds with their durations./queue: Displays the current sound queue./delete: Deletes a sound from the soundboard (Admin Only).
-
AI Chatbot Commands:
/set_chat_mode: Sets the chat response mode (text,speech, orboth)./show_chat_mode: Shows the current chat response mode./set_personality: Sets the AI chatbot's personality (Admin Only)./show_personality: Shows the AI chatbot's current personality.
Notes
-
Voice Channel for Speech: The bot plays speech responses in the voice channel where the user who sent the message is currently connected. Ensure that users are in a voice channel when expecting speech responses.
-
Audio File Cleanup: Currently, the generated speech audio files are stored temporarily but are not deleted after playback. For better resource management, consider implementing a cleanup mechanism to remove temporary files after they're played.
-
Concurrency: The bot uses asynchronous tasks to handle playback and AI responses. Ensure that the hosting environment supports asynchronous operations efficiently.
-
Security: Make sure to keep your
.envfile secure and never expose your bot token or API keys publicly. -
Extensibility: This bot serves as a foundation. You can extend its functionality further by adding more features like user-specific chat histories, rate limiting for AI responses, or integrating other AI models.
Conclusion
This comprehensive bot integrates a soundboard with an AI chatbot, providing versatile interaction capabilities within your Discord server. Users can interact with the AI in text, speech, or both, and manage sounds seamlessly. Ensure all dependencies are correctly set up, and adjust configurations as needed to fit your specific requirements.
-