Here's a basic Discord bot code in Python that joins a voice channel when the bot is turned on and listens to voice commands:
Note: This code requires the discord.py library, which you can install using pip install discord.py. You'll also need to replace YOUR_BOT_TOKEN with your actual bot token and YOUR_VC_CHANNEL_ID with the ID of the voice channel you want the bot to join.
import discord
from discord.ext import commands
import speech_recognition as sr
import pyttsx3
import threading
# Create a new bot instance
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Voice recognition and text-to-speech
recognizer = sr.Recognizer()
engine = pyttsx3.init()
# Event to indicate the bot is ready
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
voice_channel = bot.get_channel(int('YOUR_VC_CHANNEL_ID'))
if voice_channel:
await bot.change_presence(activity=discord.Game(name="Listening..."))
channel = bot.get_channel(int('YOUR_VC_CHANNEL_ID'))
await channel.connect()
print(f'Joined voice channel {voice_channel.name}')
threading.Thread(target=listen_to_voice).start()
# Listen to voice commands
def listen_to_voice():
while True:
with sr.Microphone() as source:
print('Listening...')
audio = recognizer.listen(source)
try:
command = recognizer.recognize_google(audio).lower()
print(f'Recognized command: {command}')
if 'mute' in command:
# Mute the user who spoke the command
for member in bot.voice_clients[0].members:
if member == bot.user:
continue
if member.voice:
await member.edit(mute=True)
print(f'Muted {member.name}')
engine.say(f'Muted {member.name}')
engine.runAndWait()
break
elif 'unmute' in command:
# Unmute the user who spoke the command
for member in bot.voice_clients[0].members:
if member == bot.user:
continue
if member.voice:
await member.edit(mute=False)
print(f'Unmuted {member.name}')
engine.say(f'Unmuted {member.name}')
engine.runAndWait()
break
elif 'deafen' in command:
# Deafen the user who spoke the command
for member in bot.voice_clients[0].members:
if member == bot.user:
continue
if member.voice:
await member.edit(deafen=True)
print(f'Deafened {member.name}')
engine.say(f'Deafened {member.name}')
engine.runAndWait()
break
elif 'undeafen' in command:
# Undeafen the user who spoke the command
for member in bot.voice_clients[0].members:
if member == bot.user:
continue
if member.voice:
await member.edit(deafen=False)
print(f'Undeafened {member.name}')
engine.say(f'Undeafened {member.name}')
engine.runAndWait()
break
except Exception as e:
print(f'Error: {e}')
# Run the bot
bot.run('YOUR_BOT_TOKEN')
This code listens to voice commands and performs the following actions:
mute: Mutes the user who spoke the command.
unmute: Unmutes the user who spoke the command.
deafen: Deafens the user who spoke the command.
undeafen: Undeafens the user who spoke the command.
Note that this code requires the speech_recognition and pyttsx3 libraries, which you can install using pip install SpeechRecognition pyttsx3. You'll also need to replace YOUR_BOT_TOKEN with your actual bot token and YOUR_VC_CHANNEL_ID with the ID of the voice channel you want the bot to join.
Also, make sure to enable the voice_states intent in the Discord Developer Portal.
Important: This code uses a separate thread to listen to voice commands, which may cause issues if not implemented correctly. Make sure to test the code thoroughly and handle any exceptions that may occur.
Known Issues:
- The bot may not recognize voice commands correctly, especially if the voice quality is poor.
- The bot may not respond immediately to voice commands, as it needs to process the audio and recognize the command.
- The bot may not work correctly if multiple users are speaking at the same time.
Please test the code and report any issues you encounter.