Here's a basic Discord bot code in Python that listens to voice chat 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.
python
import discord
from discord.ext import commands
# Create a new bot instance
intents = discord.Intents.default()
intents.message_content = True
intents.voice_states = True
bot = commands.Bot(command_prefix='!', intents=intents)
# Event to indicate the bot is ready
@bot.event
async def on_ready():
print(f'{bot.user} has connected to Discord!')
# Event to listen to voice chat commands
@bot.event
async def on_message(message):
# Ignore messages from the bot itself
if message.author == bot.user:
return
# Check if the message is in a voice channel
if message.channel.type == discord.ChannelType.voice:
# Get the voice channel and member
voice_channel = message.channel
member = message.author
# Check for specific commands
if 'mute him' in message.content.lower():
# Mute the member
await member.edit(mute=True)
await message.channel.send(f'{member.mention} has been muted!')
elif 'unmute him' in message.content.lower():
# Unmute the member
await member.edit(mute=False)
await message.channel.send(f'{member.mention} has been unmuted!')
elif 'deafen him' in message.content.lower():
# Deafen the member
await member.edit(deafen=True)
await message.channel.send(f'{member.mention} has been deafened!')
elif 'undeafen him' in message.content.lower():
# Undeafen the member
await member.edit(deafen=False)
await message.channel.send(f'{member.mention} has been undeafened!')
elif 'kick him' in message.content.lower():
# Kick the member from the voice channel
await member.move_to(None)
await message.channel.send(f'{member.mention} has been kicked from the voice channel!')
# Process other commands
await bot.process_commands(message)
# Run the bot
bot.run('YOUR_BOT_TOKEN')
This code listens to voice chat commands and performs the following actions:
mute him: Mutes the specified member.
unmute him: Unmutes the specified member.
deafen him: Deafens the specified member.
undeafen him: Undeafens the specified member.
kick him: Kicks the specified member from the voice channel.
Note that this code requires the voice_states intent to be enabled in the Discord Developer Portal. You can do this by following these steps:
Go to the Discord Developer Portal and select your application.
Click on the "Bot" tab.
Under "Privileged Gateway Intents", enable the "Server Members Intent" and "Voice States Intent" options.
Save your changes.
Also, make sure to replace YOUR_BOT_TOKEN with your actual bot token. You can obtain a bot token by creating a bot in the Discord Developer Portal and following the instructions to obtain a token.