npmで複数のバージョンを共存させる。
npm環境で複数のDiscord.jsのバージョンを使ってみる
必要なの?
昨日Discord.jsをv12 => v13にする人がファイル毎に機能分けしてそれぞれでclient.loginしている方法を取っていて、一部移植が難しい機能をv12で動かしたいと要望があったため。
注意点:事前にpackage.jsonにバージョンを記述してから npm install
を実行すること。
index.js'use strict';
//require
const Discord = require('discord.js');
const Discordv13 = require('discord.js-v13');
//Intents.ALL & ~(Intents.FLAGS.GUILD_MESSAGE_TYPING | Intents.FLAGS.DIRECT_MESSAGE_TYPING)
//とか使うならIntents定義が必要
const { Intents } = require('discord.js-v13');
//v13のIntents指定
const options = {
intents: ["GUILDS", "GUILD_MESSAGES"],
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
restTimeOffset: 100
};
//clientの作成
const v13client = new Discordv13.Client(options);
const client = new Discord.Client();
//readyイベント
v13client.on('ready', () => { console.log("clientv13 login") });
client.on('ready', () => { console.log("client login") });
//v13のmessageイベント
v13client.on('messageCreate', msg => {
if (msg.author.bot) return;
const res = new Discordv13.MessageEmbed()
.setDescription("v13-client")
msg.channel.send({
embeds: [res]
});
});
//v12のmessageイベント
client.on('message', msg => {
if (msg.author.bot) return;
msg.channel.send("v12-client");
});
//clientへログイン
client.login(process.env.token);
v13client.login(process.env.token);
package.json{
"name": "w-clients",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "npm:discord.js@^12.5.1",
"discord.js-v13": "npm:discord.js@^13.0.0-dev.edf6f0ca7012b6b678367a1b5d716d0b85e23ba3"
}
}
これで一応動作確認も取れたので記事とさせていただきます。