机器人看不到直接消息"/>
discord.js 机器人看不到直接消息
我决定在 node.js 上制作一个 discord 机器人,然后我找到了那个库
dicord.js
。我想让机器人接收用户的直接消息,但由于某种原因,它看不到直接发送给他的消息。
discord.js
的版本:
npm view discord.js version
14.9.0
node
和npm
的版本:
node -v
v18.15.0
npm -v
9.5.0
这是我的代码:
import discord from 'discord.js';
import config from './config.js';
const bot = new discord.Client({intents: config.intents, partials: config.partials})
bot.on('ready', () => {
console.log(`Logged in as ${bot.user.tag}!`);
});
bot.on('messageCreate', (message) => {
console.log(message);
});
bot.login(config.token);
还有我的
config.js
文件:
import discord from 'discord.js'
const { GatewayIntentBits, Partials } = discord
export default {
"token": xxxxxxx,
"intents": Object.keys(GatewayIntentBits),
"partials": [Object.keys(Partials)]
}
所以是的,我真的给了他所有的意图和部分,只是为了确保我没有错过一些。当我直接向他发送消息时,我希望在控制台中看到类似这样的内容:
<ref *1> Message {
channelId: '1101904091908689970',
guildId: '1101895271669055611',
id: '1102197229973938348',
createdTimestamp: 1682854697460,
type: 0,
system: false,
content: 'Hello',
author: User {
id: 'xxxxxxxx',
bot: false,
system: false,
flags: UserFlagsBitField { bitfield: 0 },
username: 'sewrprvrt',
discriminator: 'xxxx',
avatar: '49891ad2803d523d6edf79dd41b72ecb',
banner: undefined,
accentColor: undefined
},
pinned: false,
tts: false,
nonce: '1102197239196942336',
embeds: [],
components: [],
attachments: Collection(0) [Map] {},
stickers: Collection(0) [Map] {},
position: null,
roleSubscriptionData: null,
editedTimestamp: null,
reactions: ReactionManager { message: [Circular *1] },
mentions: MessageMentions {
everyone: false,
users: Collection(0) [Map] {},
roles: Collection(0) [Map] {},
_members: null,
_channels: null,
_parsedUsers: null,
crosspostedChannels: Collection(0) [Map] {},
repliedUser: null
},
webhookId: null,
groupActivityApplication: null,
applicationId: null,
activity: null,
flags: MessageFlagsBitField { bitfield: 0 },
reference: null,
interaction: null
}
所以我是怎么说的,我试图检查所有的意图和部分,但这没有用。我在互联网上搜索以了解如何修复它,但我发现的所有内容也无济于事。可能是因为 discord.js 最近更新到 14 版本,我没有找到适合我的工作选项。
如果有人知道我在哪里搞砸了,我将非常感激,在此先感谢。
回答如下:您需要
<user>.createDM()
才能让 discord.js 为该用户的 DM 触发 messageCreate
事件。您必须为每个想要从中获取 DM 的用户执行此操作,并在您的机器人重新启动时再次执行此操作。
我不确定为什么它会这样工作,但我认为它与仅缓存您实际需要的 DM 频道有关,而不是为您的机器人可以看到的每个用户缓存每个 DM 频道(可以访问数以百万计真的很快)。
如果您希望您的用户能够通过 DM 与您的机器人进行交互而不必这样做,我建议您考虑使用 Application Commands,因为无论您是否有
.createDM
'd,这些都会触发交互事件
更多推荐
discord.js 机器人看不到直接消息
发布评论