So my code is as below
`message.channel.send(
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'your bot token here';
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', message => {
// If message content = .ping
if (message.content === '.ping') {
message.channel.send(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
}
});
client.login(token);
);`
And I would like to put that into a string, However when attempting to im meant with a million syntax errors and so I googled escape character. I found the Javascript ones however when trying them the
message.channel.send(`Pong! Latency is ${m.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
}
ends up stopping the code with an unexpected identifier, When doing this without the above code it works.
If someone could hlep with formmating it, that would be great
P.S Im using Discord.js an addon for Node.js
Since you're not providing us with the error you're getting there could be a few things going on, but I've got a vague Idea of what you're mistake is. You're trying to calculate the difference of time between a received message and a not yet created message.
What you want to do is reply to the message simply with some dummy text, say ping, then update that message and calculate the difference between the received message and your reply. Something like this:
const Discord = require('discord.js');
const client = new Discord.Client();
const token = 'your bot token here';
client.on('ready', () => {
console.log('I am ready!');
});
client.on('message', message => {
// If message content = ping
if (message.content === 'ping') {
message.channel.send('Pong!')
.then(pongMessage => {
pongMessage.edit(`Pong! Latency is ${pongMessage.createdTimestamp - message.createdTimestamp}ms. API Latency is ${Math.round(client.ping)}ms`);
});
}
});
client.login(token);
Your comment about this script, that it's not supposed to be run confused me a little bit, but if this is supposed to be one huge template literal you're sending and the ${} is mistakenly interpreted as code, simply escape it with \
console.log(`${ 1 + 1 } | \${ 1 + 1}`) // 2 | ${ 1 + 1 }
Related
Im trying to make a discord bot that will send a message when a Minecraft server is online and when it turns off.
heres the code if you need it. (the only thing in the log is the bot has started so i will not be sending the logs
const Discord = require('discord.js');
const client = new Discord.Client();
const axios = require('axios');
let isServerOnline = false;
client.on('ready', () => {
console.log(`Bot is ready!`);
});
setInterval(async () => {
try {
const response = await axios.get('monkecraftsmp.feathermc.gg');
if (response.status === 200 && !isServerOnline) {
isServerOnline = true;
console.log("Server is online");
const channel = client.channels.cache.get('917953832431026276');
channel.send('✅ Server is online');
}
} catch (error) {
if (isServerOnline) {
isServerOnline = false;
console.log("Server has stopped");
const channel = client.channels.cache.get('917953832431026276');
channel.send('🛑 Server has stopped');
}
}
}, 6000);
also if you need it the discord.js version is 12
I have tried to check if the channel id is the right one and it is, then i checked if the token is right and its right.
i have checked the logs and nothing has shown up in it.
You might want to look into using an API, as this isn't (as far as i know) how Minecraft server statusses work. Check out this one as i've seen it's pretty good.
You're also not recieving an error because it's in a try catch statement. You can console.log the error for info on what's going wrong.
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');
}
});
so I'm trying to make a discord bot for my discord server that will respond to people referencing inside jokes, but whenever I have the "christmas checker" on it will repeatedly spam. even if the input isn't part of the RegExp.
const Discord = require('discord.js');
const client = new Discord.Client();
var d = new Date();
var n = d.getMonth() + 1;
client.on('ready', () =>{
console.log('This bot is online!');
});
client.on('message', message => {
const regex1 = message.content.match(new RegExp(/fish in a tube|fish tube|fish in the tube|tube fish|where he goin/i));
if(regex1) {
message.channel.send(`Shut the hell up `+ message.member);
}
if(message.author.bot) return;
});
client.on('message', message => {
const regex2 = message.content.match(new RegExp(/merry christmas|its december|/i));
if(regex2){
message.channel.send('It ain\'t december yet buddy')
}
if(message.author.bot) return;
});
client.login('bot key removed');
Your second regex /merry christmas|its december|/i will match anything because the third alternative has nothing entered. Regex101 example, regex101's explanation:
1st Alternative merry christmas
2nd Alternative its december
3rd Alternative — null, matches any position
Try changing it to /merry christmas|its december/i.
Also, you might want to change your code to only use one message handler. And put your check for a bot at the beginning of the handler, since if it's at the end it doesn't serve any purpose.
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.
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.