I have done discord.js bot recently, but it does not respond to commands, like !greeting.
Here is a code of index.js
const { Client, GatewayIntentBits } = require('discord.js');
const robot = new Client
({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages ] });
const comms = require("./comms.js");
const fs = require('fs');
let config = require('./config.json');
let prefix = config.prefix;
let token = config.token;
robot.on("ready", function() {
console.log(robot.user.username + " пробудився, і готовий ділитися мудрістю...");
});
robot.on('message', (msg) => {
if (msg.author.username != robot.user.username && msg.author.discriminator != robot.user.discriminator) {
var comm = msg.content.trim() + " ";
var comm_name = comm.slice(0, comm.indexOf(" "));
var messArr = comm.split(" ");
for (comm_count in comms.comms) {
var comm2 = prefix + comms.comms[comm_count].name;
if (comm2 == comm_name) {
comms.comms[comm_count].out(robot, msg, messArr);
}
}
}
});
robot.login(token);
And here is comms.js
const Discord = require('discord.js');
let config = require('./config.json');
let prefix = config.prefix;
function test(robot, mess, args) {
mess.channel.send('Test!')
}
function greeting(robot, mess, args) {
mess.reply("Вітаю тебе! Почуй мою мудрість, і ставатимеш як я...")
}
var comms_list = [
{
name: "test",
out: test,
about: "Тестова команда"
}];
comms_list.push({
name: "greeting",
out: greeting,
about: "Команда для вітання"
});
module.exports.comms = comms_list;
What I should to do??? I with my friend sit on it for three days.
Looking forward to hearing from you!
>Solution :
You forgot to add the MessageContent
intent as well in your client. This flag is required if you want to read the contents of a message as well. Also you are using a deprecated event, you should use messageCreate
instead:
robot.on('messageCreate', (msg) => {
And while you’re at it, you are trying to read the author / user object that’s within the message collection. In order to read this, you also have to enable two other intents:
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences
So, the final client initialization should look like this:
const robot = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences
]
});
Also make sure to enable the intents on the Discord Developer Console as well otherwise you won’t be able to receive the events.