はじめに
JavaScript (Node.js) を使って、Discord Botを作成する基本的なコードです。 Shikiによる強力なシンタックスハイライトが行われていることを確認できます。
ping-pong.js
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", message => {
// Bot自身のメッセージには反応しない
if (message.author.bot) return;
if (message.content === "!ping") {
message.reply("Pong!");
}
});
client.login("YOUR_BOT_TOKEN_HERE");
このように、変数や関数名、文字列が綺麗に色分けされて表示されます。