我正在尝试为我所在的小型服务器制作一个不和谐的机器人,我希望它将某个消息固定到发送它的通道。我之前做过一些机器人,但语法似乎自从我上次使用它以来已经发生了变化,并且我要重用的代码不再有效。我已经设法解决了其中一些更改(例如意图),但是当我尝试检查已发送的消息时,它什么也没做。我当前的代码
const { Client, Intents } = require("discord.js");
const { token } = require("./config.json");
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", (msg) => {
if (msg.content == "something goes here") {
msg.pin()
}
});
client.login(token);
我已经尝试为
messageCreate
补充message
,就像我看到一些人推荐的那样,但似乎仍然没有任何作用。即使将 msg.pin()
更改为 console.log(msg)
仍然在控制台中没有显示任何内容
client.on("messageCreate", (msg) => {
if (msg.content == "something goes here") {
console.log(msg)
}
});
有人知道这里的问题是什么以及我该如何解决它吗?感谢任何帮助,谢谢!
首先,您尚未在客户端中启用
GUILD_MESSAGES
意图,因此您需要通过执行以下操作来添加:
const client = new Client({
intents: [
Intents.FLAGS.GUILDS
Intents.FLAGS.GUILD_MESSAGES
]
});
除此之外,
.pin()
功能依然存在,没有任何变化。您也可以提供一个理由。 .pin()
函数还返回一个承诺,因此您必须使用.then()
或await
,因此您的最终代码可能如下所示:
const { Client, Intents } = require("discord.js");
const { token } = require("./config.json");
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.once("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("message", (msg) => {
if (msg.content == "something goes here") {
msg.pin().then(() => console.log)
}
});
client.login(token);
您可以在此处了解有关
.pin()
功能的更多信息 => pin |不和谐.js
对于 Discord.js v13 使用以下命令:
const { Client } = require("discord.js");
const { token } = require("./config.json");
const client = new Client({
intents: new Intents(32767)
});
client.login(token)
client.on("message", (ctx) => {
if (ctx.content == "!pin") {
ctx.pin(ctx.id, "Pinned")
}
});