Discord.js Voice Channel with Slash Commands - discord.js

I want to create a Music Bot with the new Discord Slash Commands, but I don't know how to get the voice channel which the user is in.
With a normal Message-Command it's message.member.voice.channel.

Because interaction.member doesn't contain any voice information, i defined the voice channel as follows:
const guild = client.guilds.cache.get(interaction.guild_id)
const member = guild.members.cache.get(interaction.member.user.id);
const voiceChannel = member.voice.channel;

const guild = client.guilds.cache.get(interaction.guild_id)
const member = guild.member(interaction.member.user.id)
const vchannel = member.voice.channel
vchannel.join()
This is what worked for me.

You can use Interaction#member which will return the member who sent the interaction if it was sent in a guild, or null if it was not sent in a guild.

const voice_channel_id = interaction.guild.members.cache.get(interaction.member.user.id).voice.channelId

Related

get the number of users in a voice channel with my discord bot in python

I'm looking for a way to get the number of users in a voice channel and to get their username to use it in one of my discord bot command, I found that I can use ctx.guild.member to get the members of users in the server, but not in a specific channel, is there a method to do that ?
I tried this
from discord.ext import commands
bot = commands.Bot(intents=discord.Intents.all(),command_prefix = "/")
#bot.command()
async def teamup(ctx):
channel = ctx.author.VoiceChannel
Or this
bot = commands.Bot(intents=discord.Intents.all(),command_prefix = "/")
#bot.command()
async def teamup(ctx):
channel = ctx.message.author.voice.channel
None of them works, the first tells me that member objects has no attribute VoiceChannel
and the second one tells me that ctx.message.author.voice is a NoneType object and has no attribute channel
You're very close.
#bot.command()
async def teamup(ctx):
channel = ctx.message.author.voice.channel
ctx has an author property; this returns the user/member object for the individual that invoked the command. The user/member has a voice property that returns the VoiceState object of the user. This has a channel property which will return the voice channel that the user is in. Importantly, this will be None if the user isn't in a voice channel. So, putting it together:
#bot.command()
async def teamup(ctx):
voice = ctx.author.voice
if not voice.channel:
await ctx.send("You are not currently in a voice channel")
return
channel = voice.channel
# do what you want with the channel here
await ctx.send(f"You're in {channel.name}!")

How would I create a string option in my slash command that reads the string that the user sent in, and uses that as an argument?

So I have a verified Discord.JS bot, and I've been trying to create a slash command with music (Distube). I want to register the play command, and then, allow the user to input a string (the song title) and use that and play the song. How would I do this?
This is my command file:
const Discord = require('discord.js');
module.exports = {
name: "play",
description: "Play music!",
async execute (interaction, client) {
const music = args.join(" ");
client.distube.play(message, music)
}
}
Like I said, how would I change this to match the slash command? I'm using the Discord.JS v13 Guide for this.
Thanks!
You should use const music = interaction.options.get("query").value instead of const music = args.join(' ') because d.js v13 slash commands using interactions, not arguments.
For getting bot joining your voice, you should use client.distube.playVoiceChannel, for searching user voice, we need get this also from interaction, const voice = interaction.member.voice.channel
After this, your code should look like this:
const music = interaction.options.get("query").value
const voice = interaction.member.voice.channel
client.distube.play(voice, music)

Discord.js - Bot join command author Voice Channel

I am making a discord.js bot and i want that the bot join the voice channel of the command author. My code its actually this:
client.on("message", (message) => {
if (message.content.startsWith(prefix + "join")){
const vchannel = message.member.voiceChannel
vchannel.join()
}
});
Im using Discord.js Ver. 11 and when i run the bot and execute the command it says: Cannot read property "join"
Instead of using message.member.voiceConnection or message.member.voiceChannel, i recently learned this:
if (message.content.startsWith(prefix + "join")){
const { voiceChannel } = message.member;
if (!voiceChannel) {
return message.reply('please join a voice channel first!');
}
voiceChannel.join()
}
And, of course, please look at discord.js guide if you haven't allready. If you need help, just refer to it in most cases. They have a tut for a music bot which is really simple (no queue) but teaches you how to play music.
It worked for me, I'm guessing it should work for you.
So the property member of the class Message has no property voiceChannel.
You should instead do this:
let voiceChannel = message.member.voice.channel;
if (!voiceChannel){return message.reply(' Please join a voice channel first!');}
voiceChannel.join();
source: https://discord.js.org/#/docs/main/stable/general/welcome

How do I send message to different channel at the same time in discord.js?

My goal is to send a message to a channel that have the name global-chat.
I tried:
const Discord = require("discord.js")
const client = new Discord.Client();
client.on("message", async(msg) => {
if(msg.channel.name !== "global-chat")return;
let message = msg.content
await client.channels.find("name", "global-chat").send(message)
})
but when I send message in the global-chat channel in one server, it won't send to global-chat on other servers. Can anyone help me fix this?
If I understand your question correctly, you want to send a message to the channel with the same name in all other guilds.
This line will simultaneously relay the message to all of the channels the bot is watching named global-chat. Here's how it works...
Promise.all() executes Promises in a parallel manner.
Client.channels is a Collection of channels the bot is watching. Using Collection.filter(), you can form a new Collection with just the channels named global-chat.
Collection.map() returns an array of the values returned by the function provided for each value in the Collection. In this case, it returns an array of Promises for Promise.all() to use.
await Promise.all(client.channels.filter(c => c.name === 'global-chat').map(c => c.send(msg.content)))
If you use discord.js v12, you'll need to replace client.channels.filter by client.channels.cache.filter.

How can I send a message to 2 server the bot is in with node discord

I would like it so that when I type a command and a message it will forward that message to a channel in 2 server the bot is connected to with node discord.
Thanx :)
You can use
var message = '[Message]';
bot.channels.get('[ChannelIDServer1]').send(message);
bot.channels.get('[ChannelIDServer2]').send(message);
To get the ID of a channel just right click the channel -> copy ID
If you have named your client differently just change bot to the name you use.
const Discord = require('discord.js');
const bot = new Discord.Client();

Resources