how to move a member to a different channel - node.js

So I have been looking around how to make a command to move someone to a different channel but they use a command called .setVoiceChannel but I can't find it? I know think might be kind of a noob question.
here is what I currently have
const user = message.author.id;
const member = message.guild.member(user);
// what a I trying to do
member.setVoiceChannel(/* them parameters */); // not defined and I can't find it in documents

// The member is the message author.
const member = message.member;
// Getting the channel.
const channel = client.channels.cache.get("Channel");
// Checking if the channel exists and if the channel is a voice channel.
if (!channel || channel.type !== "voice") return console.log("Invalid channel");
// Checking if the member is in a voice channel.
if (!member.voice.channel) return console.log("The member is not in a voice channel.");
// Moving the member.
member.voice.setChannel(channel).catch(e => console.error(`Couldn't move the user. | ${e}`));
Note: setVoiceChannel() is a valid method of GuildMember in Discord JS v11.
It has been changed to GuildMember.voice.setChannel(ChannelResolvable) in Discord JS v12.

Related

Discord.js V12 Sending an embed to a channel in a different guild

HI i was making a command where it sends an embed to a channel with an id however the command works normally when used in the guild where it has that channel but when the command is used in a different guild it throws error "TypeError: Cannot read property 'send' of undefined" is there any way to fix this? i want the command to be usable in other guilds as well
const channel = message.guild.channels.cache.find(c => c.id === "746423099871985755")
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#FFFF33')
.setTitle(message.member.user.tag)
.setAuthor(`Order ID: ${ticketID}`)
.setDescription(order)
.setThumbnail(message.author.avatarURL())
.setTimestamp()
.setFooter(`From ${message.guild.name} (${message.guild.id})`);
channel.send(exampleEmbed);
Message#guild is the current guild. So if you want to send the message into a channel from another guild you need to search for that guild in your cache from your client. You can do this by writing:
const guild = message.client.guilds.cache.get("/* Your guild id */");
const channel = guild.channels.cache.get("/* Your channel id */");
You can also do it a bit compacter:
const channel = message.client.guilds.cache.get("/* Your guild id */").channels.cache.get("/* Your channel id */");
If you want to search with channel-names:
const channel = message.client.guilds.cache.get("/* Your guild id */").channels.cache.find(c => c.name === "/* Channel name */");
Since every single channel on Discord has a different ID, you can't get two different channel objects with one ID. What you can do instead is use a channel's name (the channel will have to have the same name in both servers), or you can hard code both channels manually.
e.g.
// using names
const channel = message.guild.channels.cache.find(c => c.name === "channel-name");
// hardcoding IDs
const channel = message.guild.channels.cache.find(c => c.id === "ID1" || c.id === "ID2");
This should also work message.guild.channels.cache.get('GUILD_ID').send(embed);

Discord.js voiceStateUpdate not registering properly

I have this piece code for discord.js copied from reddit which is supposed to detect if user has joined or left the channel but it sends leave notifications only no matter if someone joins or leaves the channel. About a year ago when I last tried this same code, it worked well, but now it doesn't.
bot.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
if(oldUserChannel === undefined && newUserChannel !== undefined) {
// User Joins a voice channel
console.log('join');
} else if(newUserChannel === undefined){
// User leaves a voice channel
console.log('leave');
}
})
Has discord.js changed or what am I doing wrong? If you want the whole code, I can send it aswell.
Since you are using Discord V12, voiceChannel is no longer a property of the GuildMember object. Use voice property instead.
guildMember.voice.channel;
There is a nice guide where you can find what has changed in V12 when upgrading from V11.

UnhandledPromiseRejectionWarning when trying to send message

I am trying to send a message through discord.js and I am getting the following error:
(node:10328) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'send' of undefined
Here is my code:
// Init
const Discord = require("discord.js");
const bot = new Discord.Client();
const channel = bot.users.cache.get('4257');
// Vars
// Code
bot.on("ready", () => {
console.log("The Bot is ready!");
channel.send("Test");
});
// Login
bot.login(" "); // I will hide this.
What is wrong? Is it the id on the channel variable? I just put in the id of my bot since I didn't know what to put in it.
At first I gave it all the permissions under "Text Permissions", but I also tried giving him admin privs. It still didn't work. What am I doing wrong?
The problem is this line:
const channel = bot.users.cache.get('4257');
Here's what's wrong with it:
const channel = bot.users.cache // this returns a collection of users, you want channels.
.get('4257'); // this is a user discriminator, you want a channel ID
Here's how to fix it:
const id = <ID of channel you want to send the message to>
const channel = bot.channels.cache.get(id)
// ...
channel.send('Test')
Here's an example:
const channel = bot.channels.cache.get('699220239698886679')
channel.send('This is the #general channel in my personal Discord')
How to get a Channel ID
ChannelManager Docs
const channel is empty here. You need to make sure value should be assigned in it.
channel.send("Test");
If its not mendatory that value will come then use try-catch.
try {
channel.send("Test");
} catch(e) {
console.log(e)
}
Please support with vote or answered if it helps, thanks.

Sending message to specific channel, not working

Having trouble sending a bot message on a specific channel. I very simply want to have the bot send a message to #general when it activates, to let everyone know it's working again.
In the bot.on function, I've tried the classic client.channels.get().send(), but the error messages I'm getting show that it thinks client is undefined. (It says "cannot read property 'channels' of undefined")
bot.on('ready', client => {
console.log("MACsim online")
client.channels.get('#general').send("#here");
})
The bot crashes immediately, saying: Cannot read property 'channels' of undefined
The ready event doesn't pass any client parameter.
To get a channel by name use collection.find():
client.channels.find(channel => channel.name == "channel name here");
To get a channel by ID you can simply do collection.get("ID") because the channels are mapped by their IDs.
client.channels.get("channel_id");
Here is an example I made for you:
const Discord = require("discord.js");
const Client = new Discord.Client();
Client.on("ready", () => {
let Channel = Client.channels.find(channel => channel.name == "my_channel");
Channel.send("The bot is ready!");
});
Client.login("TOKEN");
You need to get the guild / server by ID, and then the channel by ID, so your code will be something like :
const discordjs = require("discord.js");
const client = new discordjs.Client();
bot.on('ready', client => {
console.log("MACsim online")
client.guilds.get("1836402401348").channels.get('199993777289').send("#here");
})
Edit : added client call
The ready event does not pass a parameter. To gain the bot's client, simply use the variable that you assigned when you created the client.
From your code, it looks like the bot variable, where you did const bot = new Discord.Client();.
From there, using the bot (which is the bot's client), you can access the channels property!

I keep getting an error in discord.js "discord is not defined" OR I get no response from the bot. Not even an error/

I am coding a discord bot using discord.js.
FOR SOME REASON it says Discord not defined error or the bot has no response. Please could you help me? I have never had this error before. Here's the code.
I have include this command in the same main.js file:
if(cmd === `${prefix}help`){
reporthelp = "Report a user. Requires a channel named `reports`to work!";
kickhelp = "Kick a user from the server. Requires a channel named `incidents` to work; you must have the manage messages permission.";
banhelp = "Ban a user from the server. Requires a channel named `incidents` to qork; toy need the BAN MEMBERS permission to use this command!";
warnhelp = "**COMMAND COMING SOON!**";
requiredperms = "**Embed Links, channel named `incidents`; channel named `reports`; channel named `suggestions`; Kick Members, Ban Members, Manage Roles, Manage Members.**"
helpEmbed = new Discord.RichEmbed()
.setDescription(" 💬 **__Help Is Here!__** 💬")
.setColor("#936285")
.addField("**/report #user <reason>**", reporthelp)
.addField("**(COMING SOON)** /warn #user <reason>", warnhelp)
.addField("**/kick #user <reason>**", kickhelp)
.addField("**/ban #user <reason>**", banhelp)
.addField("**/mytag**", "Shows you **your** discoord username#discrim and your user ID. Requires Embed Links permission.")
.addField("**/invite**", "Get a linkto incite meto our server, and an invite link to my suppoer server")
.addField("**/owner**", "see who currently owns the bot")
.addField("**/git**", "**SOURCE CODE AVAILIBLE SOON**")
.addField("**/token**", "View the bot's super secret bot token!")
.addField("**/serverinfo**", "displays some basic server information!")
.addField("**/botinfo**", "displays basic bot information!")
.addField("**/ping**", "Get the bot to respond with `pong` ")
.addField("**/hosting**", "Bot finds a good website for bot/web hosting")
.addField("**/quotes**", "bot responds with some lovely quotes!")
.addField("**__Required Permissions__**", requiredperms)
// .addField("**COMING SOON!**/suggest <suggestion>", "suggest something. Requires channel named `suggestions`")
.setTimestamp()
.setFooter("Use /invite to invite me to your server!")
return message.channel.send({embed: helpEmbed});
}
This is how I've defined everything:
const botconfig = require("./botconfig.json");
const tokenfile = require("./token.json");
const Discord = require("discord.js");
const bot = new Discord.Client({disableEveryone: true});
bot.on("ready", async () => {
console.log(`${bot.user.username} is active in ${bot.guilds.size} servers!`);
bot.user.setActivity("rewriteing main.js...", {type: "WATCHING"});
//bot.user.setGame("Lookin' out for ya!");
});
bot.on("message", async message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
const prefix = botconfig.prefix;
const messageArray = message.content.split(" ");
const cmd = messageArray[0];
const args = messageArray.slice(1);
const bicon = bot.user.displayAvatarURL;
I either get no response/error, or I get the error discord is not defined.
I'm also very new here, sorry If I didn't ask a question clearly...
The code is correct, but I had redfined discord in a different part of the file....
my advice would be to alays read the code over again!

Resources