I have some code to greet the server when the bot is added.
client.on("guildCreate", guild => {
const defaultChannel = guild.channels.find(c => c.permissionsFor(guild.me).has("SEND_MESSAGES"));
defaultChannel.send(`Hi! Thanks for adding me to your server!`).catch(console.error);
});
It finds a channel where it can send messages to and sends a thanking message.
When I run the above code I get the follwing error: TypeError: defaultChannel.send is not a function
Related
GOAL: How do I emit an event inside client.on("message", message =>...) function with any given string whenever I start my server.
A workaround would be to type, "test", on a channel where this bot is integrated and listen for the function being emitted inside this above mentioned method.
Working on a discord bot and was wondering how to invoke client.on(..) function call using a command like :
client.on("message", "test");
My server.js file which i run using npm start is given below:
client.once("ready", () => {
//some code
});
client.login(discord_token); // starts the bot up
client.on("message", message => { // runs whenever a message is sent
//message.content
var textMessage = message.content;
textMessage = textMessage.toLowerCase();
if (textMessage === "test".toLocaleLowerCase()) {
//some code
}
}
EDIT: The code produces the following error when I run it
UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The "listener" argument must be of type function. Received type string ('test')
at checkListener (events.js:111:11)
You could send the message in the client.on("ready") so that it sends your command as soon as it can. For example:
client.once("ready", () => {
// vv id of the channel you want to test in
client.channels.fetch("id").then(channel => {
channel.send("your message here")
})
})
I'd like to create a command, that moves all users in my Discord voice channel.
Here is what i tried.
...
client.on('message', async message =>{
//Check message is not Bot
if(message.author.bot) return;
if(message.content=="!movetome"){
if(message.member.voice.channel) {//Is user in voicechannel
message.guild.members.cache.forEach(member => { //Loop every user
if(member.id!=message.member.id&&member.voice.channel){//Is user in voicechannel and is user the command executer
member.setVoiceChannel(message.member.voice.channel)//Sets user to channel
}
});
}
}
});
...
After I tried to run the command "!movetome" in discord chat I got the following error message:
(node:12268) UnhandledPromiseRejectionWarning: TypeError: member.setVoiceChannel is not a function
Thanks for your help :)
Firstly this seems like a bad idea if any user can do it but regardless, .setVoiceChannel is v11, they moved it to <GuildMember>.voice.setChannel()
Change the contents inside of if(message.content=="!movetome") to this
const channel = message.member.voice.channel;
message.guild.members.cache.forEach(member => {
//guard clause, early return
if(member.id === message.member.id || !member.voice.channel) return;
member.voice.setChannel(channel);
});
help me with discord-api. Sending private messages to the user who just logged on to the server. I have the following code:
const robot = new Discord.Client();
robot.on("guildMemberAdd", (gMembAdd) =>
{
gMembAdd.guild.channels.find("name", "test").sendMessage(gMembAdd.toString() + "hello guys");
});
Added the following code:
robot.on("guildMemberAdd", (gMembAdd) =>
{
gMembAdd.guild.channels.find("name", "test").sendMessage(gMembAdd.toString() + "hello guys");
gMembAdd.mentions.users.first().sendMessage("Test");
});
I received an error message. Help me please.
First thing you shouldn't use .sendMessage() as it was deprecated in newer versions. You need to use .send().
When you subscribing to guildMemberAdd you will recieve a GuildMember, from there you can directly send a message:
robot.on("guildMemberAdd", (gMembAdd) => {
gMembAdd.guild.channels.find("name", "test").send(gMembAdd.toString() + "hello guys");
gMembAdd.send("Test");
});
This should send a message directly to the member that joined.
I am trying to make a discord bot, but I can't quite understand Discord.js.
My code looks like this:
client.on('message', function(message) {
if (message.content === 'ping') {
client.message.send(author, 'pong');
}
});
And the problem is that I can't quite understand how to send a message.
Can anybody help me ?
The send code has been changed again. Both the items in the question as well as in the answers are all outdated. For version 12, below will be the right code. The details about this code are available in this link.
To send a message to specific channel
const channel = <client>.channels.cache.get('<id>');
channel.send('<content>');
To send a message to a specific user in DM
const user = <client>.users.cache.get('<id>');
user.send('<content>');
If you want to DM a user, please note that the bot and the user should have at least one server in common.
Hope this answer helps people who come here after version 12.
You have an error in your .send() line. The current code that you have was used in an earlier version of the discord.js library, and the method to achieve this has been changed.
If you have a message object, such as in a message event handler, you can send a message to the channel of the message object like so:
message.channel.send("My Message");
An example of that from a message event handler:
client.on("message", function(message) {
message.channel.send("My Message");
});
You can also send a message to a specific channel, which you can do by first getting the channel using its ID, then sending a message to it:
(using async/await)
const channel = await client.channels.fetch(channelID);
channel.send("My Message");
(using Promise callbacks)
client.channels.fetch(channelID).then(channel => {
channel.send("My Message");
});
Works as of Discord.js version 12
The top answer is outdated
New way is:
const channel = await client.channels.fetch(<id>);
await channel.send('hi')
To add a little context on getting the channel Id;
The list of all the channels is stored in the client.channels property.
A simple console.log(client.channels) will reveal an array of all the channels on that server.
There are four ways you could approach what you are trying to achieve, you can use message.reply("Pong") which mentions the user or use message.channel.send("Pong") which will not mention the user, additionally in discord.js you have the option to send embeds which you do through:
client.on("message", () => {
var message = new Discord.MessageEmbed()
.setDescription("Pong") // sets the body of it
.setColor("somecolor")
.setThumbnail("./image");
.setAuthor("Random Person")
.setTitle("This is an embed")
msg.channel.send(message) // without mention
msg.reply(message) // with mention
})
There is also the option to dm the user which can be achieved by:
client.on("message", (msg) => {
msg.author.send("This is a dm")
})
See the official documentation.
Below is the code to dm the user:
(In this case our message is not a response but a new message sent directly to the selected user.)
require('dotenv').config({ path: __dirname + '/.env.local' });
const Discord = require("discord.js");
const client = new Discord.Client();
client.on("ready", () => {
console.log(client.users.get('ID_OF_USER').send("hello"));
});
client.login(process.env.DISCORD_BOT_TOKEN);
Further documentation:
https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/frequently-asked-questions.md#users-and-members
You can only send a message to a channel
client.on('message', function(message) {
if (message.content === 'ping') {
message.channel.send('pong');
}
});
If you want to DM the user, then you can use the User.send() function
client.on('message', function(message) {
if (message.content === 'ping') {
message.author.send('pong');
}
});
Types of ways to send a message:
DM'ing whoever ran the command:
client.on('message', function(message) {
if (message.content === 'ping') {
message.author.send('pong');
}
});
Sends the message in the channel that the command was used in:
client.on('message', function(message) {
if (message.content === 'ping') {
message.channel.send('pong');
}
});
Sends the message in a specific channel:
client.on('message', function(message) {
const channel = client.channels.get("<channel id>")
if (message.content === 'ping') {
channel.send("pong")
}
});
It's message.channel.send("content"); since you're sending a message to the current channel.
Hello I'm trying to deafen a particular person in discord but I keep getting the following error:
TypeError: message.setDeaf is not a function
The discord js docs state that you should deafen members like this.
.setDeaf(deaf)
Deafen/undeafen a user.
I'm unsure as to why I'm getting this error, below is my code;
var Discord = require("discord.js");
var client = new Discord.Client();
var user = "30898500862111287"
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', function(message) {
if (message.content === '$deafen') {
message.setDeaf(user);
}
});
setDeaf() is a function derived from GuildMember, not Message. Since Message does not contain a function called setDeaf(), it gave you that error.
In order to get GuildMember, which is the user you want to deafen/undeafen, you can first get the user from the Message, in your case, it will be message.author, which will return the user who sent that message.
Now, on Guild, there is a FetchMember() function that returns a GuildMember datatype. For that function's argument, all you have to do is just to put in your user that you want to target.
(Your Guild will of course be the guild where the message is in! Message.Guild should do the trick.)
Last step is just to deafen/undeafen the user.
You're using setDeaf() on a Message and not a GuildMember (SetDeaf() is a GuildMember method).
Additionally, you're passing an unexisting value user to setDeaf(). I'm guessing you were trying to pass the message.author, but setDeaf() takes a boolean and an optional reason string, not a User.
You can achieve what you're looking for with this:
if(message.content == "$deafen")
message.member.setDeaf(true);
Alternatively, add a reason:
if(message.content == "$deafen")
message.member.setDeaf(true, "reason");
References:
Message Object
GuildMember Object
you cannot invoke a Server Deaf on Message. You can do it on Guild_Member.
If you want to get the first mentioned member:
let mMember = message.mentions.members.first()
mMember.setDeaf(true)
You can also invoke it on the message_author with a member property.
message.member.setDeaf(true)
But you cannot set a Server Deaf on a Message.
It looks like they've changed the code for this a little bit, instead of:
member.setDeaf(true);
You now use:
member.voice.setDeaf(true);
The code below is solution:
const user = message.mentions.members.first()
if(!args[1]) {
message.channel.send('send me a user to mute in voice')
} else {
user.voice.setDeaf(true, `${args[2]}`)
}
Seeing as you may only deafen guild members a useful approach could be getting the guild member.
You pass the set deaf an id, this would in short throw a logic error because you passed an invalid argument type.
The following should achieve what you were going for.
var Discord = require("discord.js");
var client = new Discord.Client();
client.on('ready', () => {
console.log('I am ready!');
});
const guild = client.guilds.cache.get("YOUR GUILD ID");
const user = guild.members.cache.get("30898500862111287");
client.on('message', message => {
if (message.content === '$deafen') {
user.voice.setDeaf(true);
}
});
However, if you instead want to deafen a mentioned member you could take this approach.
var Discord = require("discord.js");
var client = new Discord.Client();
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', message => {
let mentioned = message.mentions.members.first();
if (message.content === '$deafen') {
mentioned.voice.setDeaf(true);
}
});
(On a side note I'd recommend the use of constants for your discord and client object so they may not be mutated)
const Discord = require("discord.js");