Discord Bot not responding to ping command - node.js

I know some basic python and I decided to try my hand at making a discord bot, but failed to get the bot to respond to a command. I tried changing the '-ping' to 'ping' and tried typing on my discord server:
ping
-ping
but neither did anything. Furthermore,
console.log('This does not run');
does not show up in console.
I'm not quite sure where I wrong. Any help would be appreciated. Thanks!
const {Client, Intents} = require('discord.js');
const {token} = require('./config.json');
const client = new Client({intents:[Intents.FLAGS.GUILDS]});
const prefix = '-';
client.once('ready', () => {
console.log('Bot works');
});
client.on('message', message => {
console.log('This does not run');
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ */);
const command = args.shift().toLowerCase();
if(command === '-ping'){
message.channel.send('pong');
}
});
client.login(token);

I think you need the GUILD_MESSAGES intent enabled. You can add it to the list of intents in the client constructor, like so:
const client = new Client({intents:[Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]});

Related

Discord.js not sending message nor giving an error

My discord.js version is v13, and it's not sending a message, nor is it giving an error in the console so that I can try to figure out what is wrong with it.
The code itself is over 4000 lines of code, so I tried putting it in a separate script to only test one function (which is the ping function), yet still nothing.
If you ask, this the async is only apart of another function in the main script where I use await a lot.
const Discord = require('discord.js');
const client = new Discord.Client({ intents: [ 'DIRECT_MESSAGES', 'GUILD_MESSAGES' ] });
const config = require("./config.json");
client.on("ready", () => {
console.log(`Main script ready! Currently in ${client.guilds.size} servers!`);
client.user.setActivity("Using a test script");
});
client.on('messageCreate', async message => {
if(message.author.bot) return;
if(message.content.indexOf(config.prefix) !== 0) return;
const args = message.content.slice(config.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase()
if (message.channel.type == "dm") return;
if(command === "ping") {
const m = await message.channel.send("Loading...")
m.edit(`Pong! ${m.createdTimestamp - message.createdTimestamp}ms. API ${Math.round(client.ping)}ms`)
}
});
client.login(config.token);
You are missing Intents.FLAGS.GUILDS.
Change:
const client = new Discord.Client({ intents: [ 'DIRECT_MESSAGES', 'GUILD_MESSAGES' ] });
to
const client = new Discord.Client({ intents: [ 'GUILDS', 'DIRECT_MESSAGES', 'GUILD_MESSAGES' ] });

Why doesn't my discordjs bot code work anymore?

I decided to create a new bot so I copied the old simplified code I had and installed everything I needed but i'm getting an error for some reason while my older bot with basically the same code works.
require('dotenv').config();
const Discord = require('discord.js');
const axios = require('axios');
const client = new Discord.Client();
const prefix = 's.';
const fs = require('fs');
// const { join } = require('path');
client.commands = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith(".js"));
for(const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on("ready", () => {
console.log(`${client.user.tag} : Online`);
});
client.on("message", async (message) => {
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
//normal command table
if(command == 'ping') {
client.commands.get('ping')
.run(message, args);
} else if (command == 'help') {
client.commands.get('help')
.run(message, args);
} else if (command == 'muta') {
client.commands.get('muta')
.run(message, args);
}
});
client.login(process.env.BOT_TOKEN);
The error i'm getting is: [Symbol(code)]: 'CLIENT_MISSING_INTENTS' and I'm guessing
const Discord = require('discord.js');
is causing the error since it doesn't say that "Discord" is an alias module for "discord.js".
If you have any idea on why, i'd appreciate the help!
You tried importing a discord bot that was made in a version before v13. v13 is the latest discordjs version and it requires you to add certain intents to Client.
Example:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
To see what intents your bot needs, you can look at the entire list of intents available here.
When you installed your dependencies you probably upgraded to v13 of Discord.js, which now requires a list of Intents when initialising your Discord Client.
Since your bot reads and responds to messages, it will need the following intents:
const client = new Discord.Client({
intents: [Discord.Intents.FLAGS.GUILDS, Intents.FLAGS.GUILDS_MESSAGES]
});
You can read more about how discord.js uses intents here

im new to making discord bots with node.js, i am having trouble with an error, it says that message is not defined in line 14 and 18

const Discord = require('discord.js');
const client = new Discord.Client()
const prefix = '+';
client.once('ready' , () => {
console.log('MemeStrifer is online')
});
client.on('message', message => {
if(message.content.startsWith(prefix) ||message.author.bot) return;
const args = message.content.slice(prefix.lenght).split(/+/);
const command = args.shift().toLowerCase();
if(command === 'blackhumour'){
message.channel.send('What is the difference between a kid and a bowling ball? when you put your fingers in one it doesnt scream');
} else if (command === 'sex') {
message.channel.send('no you f***ing pervert');
}
});
client.login('***');
Sorry if I am making any stupid errors, I just started yesterday by a YouTube video.
here,try this:
client.on('message', message =>{
if(message.content.startsWith(prefix) ||message.author.bot) return;
const args = message.content.slice(prefix.lenght).split(/+/);
const command = args.shift().toLowerCase();
if(command === 'blackhumour'){
message.channel.send('What is the difference between a kid and a bowling ball? when you put your fingers in one it doesnt scream');
} else if(command === 'sex'){
message.channel.send('no you fucking pervert');
}
});

Making a simple music bot for discord server, not working

Working on making a bot for a personal discord server, wanting it to simply join general voicechat when command "Blyat" is detected, play mp3 file, when its done to leave. Code:
var Discord = require('discord.js');
var client = new Discord.Client();
var isReady = true;
client.on('message', message => {
if (command === "Blyat") {
var VC = message.member.voiceChannel;
if (!VC) return message.reply("MESSAGE IF NOT IN A VOICE CHANNEL")
VC.join()
.then(connection => {
const dispatcher = connection.playFile('C:\Users\Wyatt\Music\soviet-anthem.mp3');
dispatcher.on("finish", end => { VC.leave() });
})
.catch(console.error);
};
});
client.login('token, not putting in code, is there in real code');
Error: "ReferenceError: command is not defined
at Client. (C:\Users\jeffofBread\Desktop\Discord Bot\main.js:6:5)"
Any help or tips will be much appreciated, haven't coded in many years and have lost any knowledge I had once held.
Your problem comes from an unidentified variable. Fortunatly that is very easy to fix. All you have to do is define command before you call it.
For this we'll splice the message content into two parts. The command and any arguments that may be included like mentions or other words. We do that with:
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
Note: The args constant is an array.
Which would make your entire onMessage event look like this:
client.on('message', message => {
const prefix = "!";
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if (command === "blyat") {
var VC = message.member.voice.channel;
if (!VC) return message.reply("MESSAGE IF NOT IN A VOICE CHANNEL")
VC.join()
.then(connection => {
const dispatcher = connection.play('C:\Users\Wyatt\Music\soviet-anthem.mp3');
dispatcher.on("finish", end => { VC.leave() });
})
.catch(console.error);
};
});

making a discord bot followed a tutorial and it dint work

I'm using node.js and visual studio
the bot is seemingly not working but does respond to the node . command but doesn't respond to the -ping command and I can find out why, is it spelling or updated code or am I just stupid please help.
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix = '-';
client.once('ready', () => {
console.log('sirjunkbot is awake');
});
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
} else if (command == 'test'){
message.channel.send('123');
}
});
What does the console say?
Also you dont have client.login()

Resources