I'm attempting to make a discord bot that checks messages sent in channels for a prefix and argument (!send #Usermention "message"), but despite running, the program closes out as soon as a message is typed in my discord server, not outputting any error messages, so I'm not really sure what to do...
const Discord = require('discord.js');
const client = new Discord.Client();
const auth = require('./auth.json');
const prefix = "!";
client.on("message", (message) =>
{
msg = message.content.toLowerCase();
if (message.author.bot) { return; }
mention = message.mention.users.first(); //gets the first mention of the user's message
if (msg.startsWith (prefix + "send")) //!send #name [message]
{
if (mention == null) { return; } //prevents an error sending a message to nothing
message.delete();
mentionMessage = message.content.slice (6); //removes the command from the message to be sent
mention.sendMessage (mentionMessage); //sends message to mentioned user
message.channel.send ("message sent :)");
}
});
client.login(auth.token);
mention = message.mention.users.first();
It is message.mention**s**. You were missing an s.
Also, you might want to use send, rather than sendMessage, since sendMessage is deprecated.
Related
My bot is not reading the Discord chat. I want it to read the chat and if it finds certain words it will give a certain response. This is my current message event code. This is my first JavaScript project and I have just started learning it so please rip it apart so I can learn quicker :)
At the moment I can get the bot to load into discord. I can turn it on with .node but I can not get it to read a message using message.content.
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILD_MESSAGES", "DIRECT_MESSAGES"] });
var name = "Poker Bot";
var usersHand
let firstCardValue
let secondCardValue
let firstCardSuit
let secondCardSuit
//starts the bot and sets activity to a funny quote. it also will give a command prompt notification that the
// bot is online
client.on("ready", () => {
console.log(`Bot is online: ${name} !`);
client.user.setActivity('Burning The Fucking Casino Down');
});
//check discord chat to see if a user has posted.
client.on("messageCreate", message => {
//console.log is there to test user input. If it works the message in the discord channel will appear in console
console.log(`The user has said: ${message} `);
//look for poker hand ~~~ position ~~~~ event (ex: AA CO PF ) (PF= PreFlop)
if (message.content.toLowerCase == 'AK' || message.content.toLowerCase == 'AA' || message.content.toLowerCase == 'KK'){
message.reply("RECOMMENDED PLAY SHOVE: ALL IN")
}
.content is not a method, it's a property, you must now also enable the Message Content intent on your bot page as well as in your code.
const Discord = require("discord.js");
const client = new Discord.Client({ intents: ["GUILD_MESSAGES", "DIRECT_MESSAGES"] });
client.on("messageCreate", message => {
// || "String" like you did before would return "true" in every single instance,
// this is case sensitive, if you wanna make it case insensitive
// use `message.content.toLowerCase() == "lowercasestring"`
if (message.content == "AK" || message.content = "AA" || message.content == "KK") {
message.channel.send("Recommend Play is to shove all in" + message.author);
}
})
client.login(token);
Judging by your information, you dont just want to send a response if the message contains only those strings, but may just contain then.
To check for that, I would suggest to use regex#test
Still as #iiRealistic_Dev rightfully mentioned: message.content is not a function, so removing the brackets is the way to go.
client.on("messageCreate", (message) => {
if (/AK|AA|KK/.test(message.content)) {
message.channel.send("Recommend Play is to shove all in" + message.author);
console.log('it got to here');
}
});
I created a bot that responses to another bot; somehow related to Cleverbot. However, when the bot takes time to respond, its message gets stuck and it spams. I'm just wondering, how I can make the bot wait for the message of the other bot to send before it sends a message again. image
client.on('message', async message => {
if(!message.author.bot) return
message.channel.startTyping();
let content = message.content;
chatbot.getReply(content).then(r => message.channel.send(r))
message.channel.stopTyping();
});
Here's how I'd do that on the stable version of Discord.js(v12)
...
client.on('message', async message => {
var author = message.author;
var content = message.content;
var botChannel = message.channel;
if(author.bot){
//author is a bot so we respond
botChannel.startTyping();
botChannel.send(`${author}, <Your message here>`).then( (msg) => {
botChannel.stopTyping();
}
);
} else {
//nothing happens because it's a human
return;
}
);
I'm working on my discord bot and I want it to save messages that i sent it to save, how do I do this since the rest of the internet doesn't ask this question for some reason. i've been looking for someone to point me in a direction but haven't found anything
This is a really simplified version of what you want to do but I'm sure if you read it you'll understand and it will get the job done.
const discord = require('discord.js'); // Import discord.js
const client = new discord.Client(); // Initialize new discord.js client
const messages = [];
client.on('message', (msg) => {
if (!msg.content.startsWith('+')) return; // If the message doesn't start with the prefix return
const args = msg.content.slice(1).split(' '); // Split all spaces so you can get the command out of the message
const cmd = args.shift().toLowerCase(); // Get the commands name
switch (cmd) {
case 'add':
messages.push(args.join(' ')); // Add the message's content to the messages array
break;
case 'get':
msg.channel.send(messages.map((message) => `${message}\n`)); /* Get all the stored messages and map them + send them */
break;
}
});
client.login(/* Your token */); // Login discord.js
so I have a bot that takes whatever I say when I do the command /say and deletes my message. Since it still technically sends my message, people will see it through notifications and can tell that it was me that got the bot to send the text. I am doing this as a fun and troll thing with my friends so I wanted to figure out a way for the bot to take my /say command from a hidden text channel and put it in the general channel.
const Discord = require('discord.js') //Discord package
const client = new Discord.Client(); //New Discord Client
const prefix = '/'; //command prefix
client.on('ready', () => {
console.log('Bot is Online.');
});
client.on('message', message => {
if(message.member.roles.find('name', 'Bot')){ //only role 'Bot' can use the command
if (message.author.bot) return undefined; //bot does not reply to itself
let msg = message.content.toLowerCase();
let args = message.content.slice(prefix.length).trim().split(' '); //arguments
let command = args.shift().toLowerCase(); //shifts args to lower case letters
if (command === 'say'){
let say = args.join(' '); //space
message.delete(); //deletes the message you sent
message.channel.send(say);
}
}
});
This is my code so far and I've got it working for what I want it to do. I just need help with how to get it to copy a hidden channel's message to the general channel
Assume you have some channel named general.
The following will send a message to it:
client.on('message', message => {
if (message.author.bot) return undefined //bot does not reply to itself
let msg = message.content.toLowerCase()
let args = message.content
.slice(prefix.length)
.trim()
.split(' ') //arguments
let command = args.shift().toLowerCase() //shifts args to lower case letters
if (command === 'say') {
let say = args.join(' ') //space
message.delete() //deletes the message you sent
const generalChannel = message.guild.channels.find(channel => channel.name === "general")
generalChannel.send(say)
}
})
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.