問題描述
我正在嘗試制作一個機器人,一旦用戶發送特定消息,就會向頻道發送消息.我已經設法讓它在機器人登錄后發送一條消息,但是 client.on()
函數不會做任何事情.如果我做錯了什么,請告訴我,提前謝謝!
I'm trying to make a bot that sends a message to a channel once a user sends a specific message. I've managed to make it send a message once the bot logs in, but the client.on()
function won't do anything. Please let me know if I'm doing something wrong, thank you in advance!
const { Client, Intents } = require("discord.js");
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.login("<bot token>");
client.once("ready", () => {
console.log("Ready!");
channel.send("hello world"); //This works
const guild = client.guilds.cache.get("<server id>");
const channel = guild.channels.cache.get("<channel id>");
//This is the issue. Nothing happens when I send "!ping" in the server
client.on("message", message => {
if (message.content === "!ping") {
channel.send("pong");
}
});
});
推薦答案
您需要啟用 GUILD_MESSAGES
Intent:
You need to enable the GUILD_MESSAGES
intent:
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
這將使您能夠接收在公會中發送的消息的 MESSAGE_CREATE
事件.
This will enable you to receive the MESSAGE_CREATE
event for messages sent in guilds.
可以在 Discord 開發者文檔中找到完整的意圖列表.
A full list of intents can be found on the Discord developer docs.
此外,如果您使用 Discord.js v13,message
事件已被棄用,因為它已重命名為 messageCreate
.
Additionally, if you are using Discord.js v13, the message
event has been deprecated as it has been renamed to messageCreate
.
這篇關于使用 Discord.js 向頻道發送消息時遇到問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!