![](/img/trans.png)
[英]Cant use .send when its not used in client.on(ready) discord.js v14
[英]Discord.js V14 - client.on message create not firing
我正在尝试 Discord.js 的 V14,并且有很多新东西,但这整个意图。 由于某种原因,我没有得到消息,我的 messageCreate 没有被触发,并且来自其他帖子。 我相信这与意图有关,我已经搞砸了意图。 但似乎没有任何效果:这是我的代码:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
console.log("new message...")
console.log(message.message)
console.log(message.content)
if (message.mentions.users.first() === client) {
message.reply("Hey!.")
}
});
client.login(process.env.token);
谢谢!!
想通了:您需要在开发人员门户中打开消息内容意图:图片
此外,您需要在顶部有 messageContent 意图:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildBans,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
if (message.content === "<@1023320497469005938>") {
message.reply("Hey!")
}
});
client.login(process.env.token);
至于从消息中获取 ping,不是我最初的问题,而是我需要解决的问题,只需使用if (message.content) === "<@BotID>" {CODE HERE}
您需要GuildMessages
意图。
注意:您不需要在 ping 机器人的消息中使用MessageContent
意图,因为机器人可以从没有意图的情况下 ping 它们的消息中接收内容。
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
});
// ...
client.on("messageCreate", (message) => {
// ...
if (message.mentions.users.has(client.user.id)) { // this could be modified to make it have to *start* with or have only a ping
message.reply("Hey!.")
}
});
// ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.