Moving a user to the message author's voice channel - python-3.x

Trying to make my discord bot move a user to the voice channel that the message author is in. E.g. I write !move #john Then the bot would move "john" to my voice channel.
# command to move a user to current channel
#bot.command()
async def move(ctx,member:discord.Member=None):
channel= discord.utils.get(ctx.guild.voice_channels)
if not member:
await ctx.send("Who am I trying to move? Use !move #user")
await member.move_to(channel)
At the moment it moves the users, but only to the first voice channel in the server. How do I get it to move it to the author's voice channel?

The VoiceChannel the author is in is stored in ctx.author.voice.channel. Notably, .voice and .channel can be None, so we need check for that
#bot.command()
async def move(ctx,member:discord.Member=None):
if ctx.author.voice and ctx.author.voice.channel:
channel = ctx.author.voice.channel
else:
await ctx.send("You are not connected to voice!")
if not member:
await ctx.send("Who am I trying to move? Use !move #user")
await member.move_to(channel)

Related

How to detect if a user join in anyone of voice channel?

I would simply like that when a specific user enters any voice chat on my discord server, my bot will detect it and then go into voice and say "x user joined the chat or something like this" (I already know how). The question is how to detect the user entering? (Detection must be instant).
I tried to using this code:
#bot.event
async def on_voice_state_update(message, after, before, member):
ctx = await bot.get_context(message)
u = ctx.guild.get_member(428938023057620994)
for channel in ctx.guild.voice_channels:
if sebastiano in channel:
tts = gTTS(text=u + "in voice chat", lang='en')
tts.save("u.mp3")
try:
vc = await channel.connect()
await vc.play(discord.FFmpegPCMAudio('sebastiano.mp3'))
await vc.disconnect()
except:
await ctx.send("Scusate ho la 104 e non funziono.")
But doesn't work:
File "/home/diego/Documents/Python 3/CancerBot/cancerbot.py", line 26, in on_voice_state_update
ctx = await bot.get_context(message)
File "/home/diego/.local/lib/python3.9/site-packages/discord/ext/commands/bot.py", line 880, in get_context
view = StringView(message.content)
AttributeError: 'Member' object has no attribute 'content'
you have some problems here:
there is no message or ctx in an on_voice_state_update.
the parameters have to be as follow:
member - the member who joined/left/moved channel
before - the voice state before. if he had entered a channel and didn't move from one channel to another, the channel attribute would be None
after - the voice state after. if he left a channel and didn't move channels the channel attribute would be None
you answer your question to check if someone has joined I would do it also if he moved I would do:
if after.channel:
then do what you've wanted

Check TextChannel's id and do some actions

Hey everyone I want to delete all messages from a specific text channel I use this method
#client.event
async def on_message(message):
for guild in client.guilds:
for channel in guild.text_channels:
if channel.id == 818522056261369906:
await message.delete()
it works but it delete all text channels messages not just that text channel with the id above
What is the problem
it works but it delete all text channels messages not just that text channel with the id above What is the problem
You're deleting the message upon checking if a certain channel exists in your bot (i.e by checking if that channel exists in any of all the guilds your bot is in). You're not checking if the message's channel's id matches the provided ID or not. That's why it only checks if that specific channel exists and continues to delete the message regardless of weather they're in that specific channel or not.
for channel in guild.text_channels:
# you're checking if a certain channel in guild's
# text_channel matches the ID or not
if channel.id == 818522056261369906:
await message.delete()
But you ought to check if your message's channel's id matches the ID or not. You can do so by message.channel.id == 818522056261369906. So your code should look something like this
if message.channel.id == 818522056261369906:
await message.delete()
The simplest way to do this would be to include get_channel(id) and await purge if you want to continuously delete messages from a channel.
#client.event
async def on_message(message):
channel = client.get_channel(818522056261369906) # this gets the specific channel through id
await channel.purge()
await client.process_commands(message)
However, if you only want to purge it once in a given channel, you would use a #client.command with similar code as above.
#client.command()
async def test(ctx):
channel = ctx.channel # getting the current channel
await channel.purge()
Edit: Don't forget to add your await client.process_commands(message) if you're using both the commands extension and the on_message event!

Print the list of members in a voice channel

i'm coding a discord bot and I need a function that kick all members in my channel. I wrote this code:
#client.command()
async def separaci(ctx):
canale = ctx.message.author.voice.channel
utenti = canale.members #This return an empty list
for utente in utenti:
await utente.edit(voice_channel = None)
I don't know why canale.members return an empty list. Can you help me? Thanks you :)
Try this:
#client.command()
async def separaci(ctx):
if ctx.author.voice: # if the author is connected to a voice channel
canale = ctx.message.author.voice.channel
utenti = canale.members #This return an empty list
for utente in utenti:
await utente.edit(voice_channel = None)
await ctx.send("Kicked all the members from the voice channel!")
else:
await ctx.send("You need to be in a voice channel!")
return
NOTE:
You need to be in a voice channel while using this command.
Make sure the bot has the permission to disconnect the members present in the voice channel.
Make sure you have the members intent enabled in your developer portal.
You have to enable member intents, also make sure to enable them in the developer portal
intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix='', intents=intents)
#client.command()
async def separaci(ctx):
channel = ctx.author.voice.channel
members = channel.members
A Primer Gateway to Intents

discord.py send a message if author isnt in a voice channel

I want my bot to send a message if im not in a voice channel when i type a command.
Heres my current code:
#client.command()
async def play(ctx):
channel = ctx.author.voice.channel
if channel:
await channel.connect()
await ctx.send('Joining voicechat.')
elif channel is None:
await ctx.send('You have to be in a voice channel first.')
It joins and sends a message when i'm in a voice channel, but when i'm not, it returns this error in terminal:
Command raised an exception: AttributeError: 'NoneType' object has no attribute 'channel'
Member.voice will be None, you need to check that
Below is the revised code:
#client.command()
async def play(ctx):
channel = ctx.author.voice
if channel:
await channel.channel.connect()
await ctx.send('Joining voicechat.')
else:
await ctx.send('You have to be in a voice channel first.')

discord.ext.commands.errors.MissingPermissions: You are missing Mute Members permission(s) to run this command

I'm trying to program a command that mutes every member in the author's voice channel. I'm doing this so myself and my friends can mute ourselves when we swap to in-game voice chat automatically. But for reasons I cannot explain, I can never get it to work. Here's my code:
#commands.command()
#commands.has_permissions(mute_members=True)
async def mute(self, ctx):
if ctx.author.voice and ctx.author.voice.channel:
channel = ctx.author.voice.channel
for member in channel.members:
await member.edit(mute=True)
else:
await ctx.send("You are not connected to a voice channel!")
This is the full error
I understand that the bot and the author need a mute members permission, but both of them do! I even made sure that they were at the top of the role list, and I edited the voice channel permissions to allow mute members for the author and bot. No matter what I do, I always keep getting the same error! Any help would be greatly appreciated!
async def has_channel_permissions(**kwargs):
def predicate(ctx):
if ctx.author.voice is not None:
if ctx.author.voice.channel is not None:
if kwargs in dict(ctx.author.voice.channel.permissions_for(ctx.author)) and kwargs in dict(commands.user:
return True
else:
return False
await ctx.send("You are not connected to a voice channel!")
return False
return commands.check(predicate)
#commands.command()
#has_channel_permissions(mute_members=True)
async def mute(self, ctx):
channel = ctx.author.voice.channel
for member in channel.members:
await member.edit(mute=True)

Resources