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')
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')
Related
i'm trying to make embed and my bot is sending empty message
I have token, i just deleted him from this message.
And with deleted 'help' don't working too.
bot = commands.Bot (command_prefix=config['prefix'], intents=intents, help_command=None)
bot.remove_command('help')
#bot.event
async def on_ready():
print('Bot connected')
#bot.command()
async def info(ctx):
embed = discord.Embed(
title='title',
description='description',
colour=discord.Colour.dark_gold()
)
await ctx.send(embed=embed)
#bot.event
async def on_message(ctx):
if ctx.author != bot.user:
if filter_mata(ctx.content) == True:
await ctx.delete()
print(f'message {ctx.id} by {ctx.author} deleted')
await bot.process_commands(ctx)
bot.run (config['token'])
import discord
from discord.ext import commands
client = discord.Client()
bot = commands.Bot(command_prefix="!")
#client.event
async def on_ready():
print("I'm logged in as {0.user}.".format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("!test"):
await message.channel.send("Test passed successfully!")
#commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
#commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
The bot doesn't join I don't know what I did wrong. It should only join my voice channel nothing else. I watched this Tutorial but as I said it didn't worked.
You're mixing metaphors a little: your on_message is a plain function:
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("!test"):
await message.channel.send("Test passed successfully!")
Whereas your join and disconnect functions look as if they belong in a class (note the self argument):
#commands.command()
async def join(self,ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
#commands.command()
async def disconnect(self,ctx):
await ctx.voice_client.disconnect()
If you remove the self argument from those functions, you should be able to get it working:
async def join(ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = client.get_channel(ctx.author.voice.channel.id)
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
await ctx.send("idk")
#commands.command()
async def disconnect(ctx):
await ctx.voice_client.disconnect()
I made so it connects to the voice channel by the command .join and it supposed to play a sound that i saved. This is made for a voice channel with chill music so that they don't have to copy and paste a YouTube link. I am using python 3.9
import discord
from discord.ext import commands
bot = discord.Client()
bot = commands.Bot(command_prefix=".")
#bot.command()
async def join(ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
guild = ctx.guild
voice_client: discord.VoiceClient = discord.utils.get(bot.voice_clients, guild=guild)
audio_source = discord.FFmpegPCMAudio('music.mp3')
if not voice_client.is_playing():
voice_client.play(audio_source, after=None)
else:
await ctx.voice_client.move_to(voice_channel)
bot.run("token")
Try this instead:
import discord
from discord.ext import commands
from youtube_dl import YoutubeDL
bot = discord.Client()
bot = commands.Bot(command_prefix=".")
#bot.command()
async def join(ctx):
if ctx.author.voice is None:
await ctx.send("You are not in a voice channel")
return
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
audio_source = discord.PCMVolumeTransformer(discord.FFmpegPCMAudio('music.mp3'))
if not ctx.voice_client.is_playing():
ctx.voice_client.play(audio_source, after=lambda e: print('Player error: %s' % e) if e else None)
bot.run("token")
So I want to make a bot for my discord server so it joins a voice channel I am currently in and mutes all member and unmutes them on command, like vcmute and vcunmute, but somethings missing and nothing is working. here is the code I wrote
import discord
from discord.ext import commands
import os
client = discord.Client()
DISCORD_TOKEN = os.getenv("myToken")
bot = commands.Bot(command_prefix="$")
#client.event
async def on_ready():
print('BOT ACTIVATED')
#bot.command()
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
#bot.command()
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)
#bot.command()
async def vcunmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=False)
bot.run("myToken")
The code works for me, the bot is probably missing admin permissions which prevents it from muting and unmuting members.
You could also add the #commands.has_permissions() decorator to have it check for admin permissions, example for your code:
#bot.command()
#commands.has_permissions(administrator=True)
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
#bot.command()
#commands.has_permissions(administrator=True)
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)
#bot.command()
#commands.has_permissions(administrator=True)
async def vcunmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=False)
this is my code rn:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix="-")
#client.event
async def on_ready():
print('BOT ACTIVATED')
#client.command()
async def hello(ctx):
await ctx.send("hei this is a test just dont mind me")
#client.command()
async def join(ctx):
channel = ctx.message.author.voice.channel
await channel.connect()
client.run('mytoken')
how do i mute everyone in that voice channel?
Iterate through all members of the voice channel and pass mute=True to the edit function.
#client.command()
async def vcmute(ctx):
vc = ctx.author.voice.channel
for member in vc.members:
await member.edit(mute=True)