我想在不和谐上创建一个机器人,如果我在“ping”聊天中输入消息,它就会写“pong”。 由于某种原因它不起作用。
require('dotenv').config(); //initialize dotenv
const Discord = require('discord.js'); //import discord.js
const client = new Discord.Client({ intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages
]}) //create new client
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
}
});
//make sure this line is the last line
client.login(`TOKEN`); //login bot using token
该事件称为messageCreate,这是编码中唯一错误的部分。我仍然建议直接导入客户端等,而不仅仅是 Discord。
require('dotenv').config();
const {Client, Events, GatewayIntentBits}= require('discord.js');
const client = new Client({ intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]})
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on(Events.MessageCreate, msg => {
if (msg.content === 'ping') {
msg.reply('Pong!');
}
});
client.login(`TOKEN`);