mega gaming
mega gaming

Reputation: 25

Make discord bot connect to vc with deafen

I use voice = await message.author.voice.channel.connect() to connect my bot to voice channel and i want the bot connect and deafen himself

All my code:

client = discord.Client()


@client.event
async def on_ready():
    print("Bot is ready!")

@client.event
async def on_message(message):
    arg = message.content.split(' ')
    if message.content.startswith('>play'):
        voice = await message.author.voice.channel.connect()
        voice.play(discord.FFmpegPCMAudio(YouTube(arg[1]).streams.first().download()))
        await message.guild.change_voice_state(channel=voice, self_mute=True, self_deaf=False)
client.run('my token')

Upvotes: 0

Views: 937

Answers (1)

Aditya Tomar
Aditya Tomar

Reputation: 1639

Use await message.guild.change_voice_state(channel=voice, self_mute=False, self_deaf=True)

Copy-paste this code that I wrote after looking at your comments:

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix='>')


@bot.event
async def on_ready():
    print("Bot is ready!")

@bot.event
async def on_message(message):
    if message.author == bot.user:
        return
    
    await bot.process_commands(message)

@bot.command()
async def join(ctx):
    if ctx.author.voice is None or ctx.author.voice.channel is None:
        return await ctx.send('You need to be in a voice channel to use this command!')

    voice_channel = ctx.author.voice.channel
    if ctx.voice_client is None:
        vc = await voice_channel.connect()
    else:
        await ctx.voice_client.move_to(voice_channel)
        vc = ctx.voice_client
    
    vc.play(discord.FFmpegPCMAudio(YouTube(arg[1]).streams.first().download()))
    await ctx.guild.change_voice_state(channel=vc, self_mute=False, self_deaf=True)

bot.run('my token')

Upvotes: 1

Related Questions