最近又開始寫起Discord機器人, 用的discord.js是14.3的版本, 跟上次寫的時候用的12.3版有一些差異, 於此記錄一下
Login
12.3的login:
const { Client } = require('discord.js');
const client = new Client({ partials: [/*需要的partial字串, ex:'CHANNEL'*/] });
client.login(/*機器人的token*/);
14.3的login:
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const client = new Client({
intents: [/*需要的GatewayIntentBits列舉, ex:GatewayIntentBits.Guilds*/],
partials: [/*需要的Partials列舉, ex: Partials.Channel*/]
});
client.login(/*機器人的token*/);
接收訊息
12.3:
const client = new Client({ partials: ['MESSAGE', 'CHANNEL'] });
client.on('message', msg => {
console.log(msg);
});
14.3:
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
partials: [Partials.Channel, Partials.Message]
});
client.on('messageCreate', msg => {
console.log(msg);
});
接收反應
12.3:
const client = new Client({ partials: ['MESSAGE', 'CHANNEL', 'REACTION'] });
client.on('messageReactionAdd', async (reaction, user) => {console.log(reaction);});
client.on('messageReactionRemove', async (reaction, user) => {console.log(reaction);});
14.3:
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMessageReactions],
partials: [Partials.Message, Partials.Channel, Partials.Reaction]
});
client.on('messageReactionAdd', async (reaction, user) => {console.log(reaction);});
client.on('messageReactionRemove', async (reaction, user) => {console.log(reaction);});
Yorumlar