Reputation: 21
I'm programming my first bot for discord by javascript and with discord.js v14. My bot simply obeys the command in private chat, it doesn't send a message back if it's through a server. Can someone help me? My code:
//npm é um gerenciador de pacotes
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const config = require('./config.json');
const { Guilds, GuildMembers, DirectMessages } = GatewayIntentBits;
const { Channel, GuildMember, Message, User, Reaction } = Partials;
const client = new Client({
intents: [Guilds, GuildMembers, DirectMessages],
partials: [Channel, GuildMember, Message, User, Reaction],
});
client.on('ready', () => {
console.log(
`Bot foi iniciado, com ${client.users.cache.size} usuários, em ${client.channels.cache.size} canais, em ${client.guilds.cache.size} servidores.`,
);
client.user.setActivity(
`Eu estou em ${client.guilds.cache.size} servidores.`,
); //atualiza status do bot
});
client.on('guildCreate', (guild) => {
console.log(
`O bot entrou nos servidores: ${guild.cache.name} (id: ${guild.cache.id}). População: ${guild.cache.memberCount} membros.`,
);
client.user.setActivity(`Estou em ${client.guilds.cache.size} servidores!`);
}); //toda vez q o bot entra num server, esse comando é executado e atualiza o status do bot
client.on('guildDelete', (guild) => {
console.log(
`O bot foi removido do servidor: ${guild.cache.name} (id: ${guild.cache.id}).); client.user.setActivity(Serving ${client.guilds.cache.size} servers!`,
);
}); //avisa caso o bot seja excluido de um servidor
client.on('messageCreate', async (message) => {
let prefix = config.prefix; //liga o prefixo do arquivo config para comando
if (message.author.bot) return; //nao responda outros bots
if (!message.content.startsWith(config.prefix)) return;
//if(message.channel.type === "dm") return; //nao responda por DM
const args = message.content.slice(config.prefix.length).trim().split(/ +/g); //liga o prefix do config para comando
const comando = args.shift().toLowerCase(); //parametros do comando
if (comando === 'ping') {
//define o comando depois do prefixo digitado pelo usuario
const m = await message.channel.send('Ping?'); //mensagem
m.edit(
`:ping_pong: Pong! A latencia é ${
m.createdTimestamp - message.createdTimestamp
} ms. A latencia da API é ${Math.round(client.ws.ping)} ms`,
); //edita a propria mensagem 'm'
}
}); //evento de mensagem do bot
client.login(config.token); //liga o bot
I tried to reinstall discord.js, I also installed an older version, but apparently the problem is not in the library because it works, but only in private chat.
Upvotes: 2
Views: 72
Reputation: 23161
It's because without the MessageContent
intent you only receive the message.content
in DMs but not in guilds. It will be an empty string in guilds.
You will need to add MessageContent
to your intent array:
const { Guilds, GuildMembers, DirectMessages, MessageContent } =
GatewayIntentBits;
const { Channel, GuildMember, Message, User, Reaction } = Partials;
const client = new Client({
intents: [Guilds, GuildMembers, DirectMessages, MessageContent],
partials: [Channel, GuildMember, Message, User, Reaction],
});
Upvotes: 1