javascript - 使用 Discord.js 向频道发送消息时遇到问题
问题描述
我正在尝试制作一个机器人,一旦用户发送特定消息,就会向频道发送消息。一旦机器人登录,我已经设法让它发送一条消息,但该client.on()
功能不会做任何事情。如果我做错了什么,请告诉我,提前谢谢!
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
意图:
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
});
这将使您能够接收MESSAGE_CREATE
在公会中发送的消息的事件。
完整的意图列表可以在 Discord 开发者文档中找到。
此外,如果您使用的是 Discord.js v13,则该message
事件已被弃用,因为它已重命名为messageCreate
.
推荐阅读
- macos - AH00558:httpd:无法可靠地确定服务器的完全限定域名,使用
- jupyter-lab - 如何在 jupyterlab 启动器中添加自定义文件类型?
- c# - 如何对连接的 TPL 数据流块进行错误处理?
- node.js - AWS MSK 触发器 - Lambda(消费者)无限运行
- java - 映射外键 JPA
- javascript - 无法让 Google Sheets Node.js API 从工作表中获取值并将它们返回到数组中
- python - 如何使用 pyautogui 添加开和关按钮?
- python-3.x - 将数据从一个视图传递到另一个视图:Django
- android - 在最新版本的 android studio 中哪里可以找到 r.java
- git - 如何在所有 git 提交中搜索涉及多个字符串的更改,并对未更改的内容有一定的限制?