From a5149d5ca126b341ea618620d2cd166aa6a52628 Mon Sep 17 00:00:00 2001 From: ntki72 Date: Sat, 9 Nov 2024 15:52:09 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=86=E3=82=AD=E3=82=B9=E3=83=88=E8=AA=AD?= =?UTF-8?q?=E3=81=BF=E4=B8=8A=E3=81=92=E3=82=92=E8=A1=8C=E3=81=88=E3=82=8B?= =?UTF-8?q?=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 + docker-compose.override.yml | 10 ++- docker-compose.yml | 4 +- package-lock.json | 132 +++++++++++++++++++++++++++++++++++- package.json | 5 +- src/commands/join.js | 12 +++- src/events/messageCreate.js | 26 +++++-- src/index.js | 3 + src/services/tts.js | 23 +++++++ 9 files changed, 204 insertions(+), 13 deletions(-) create mode 100644 src/services/tts.js diff --git a/Dockerfile b/Dockerfile index 6a4d0ac..2296290 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,7 @@ FROM node:23.1.0-alpine +RUN apk add --no-cache ffmpeg + WORKDIR /app COPY package*.json ./ diff --git a/docker-compose.override.yml b/docker-compose.override.yml index 9ad536f..e3c06cf 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -5,9 +5,15 @@ services: NODE_ENV: development env_file: - config/development.env + environment: + - NODE_ENV=development volumes: - .:/app - /app/node_modules - environment: - - NODE_ENV=development command: ["npm", "run", "dev"] + depends_on: + - voicevox + voicevox: + image: voicevox/voicevox_engine:latest + ports: + - "50021:50021" diff --git a/docker-compose.yml b/docker-compose.yml index c1c3dc5..b67dcbb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,8 @@ services: NODE_ENV: production env_file: - config/production.env + environment: + - NODE_ENV=production volumes: - .:/app restart: always - environment: - - NODE_ENV=production diff --git a/package-lock.json b/package-lock.json index f336124..227d559 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,8 +10,11 @@ "license": "ISC", "dependencies": { "@discordjs/voice": "^0.17.0", + "axios": "^1.7.7", "discord.js": "^14.16.3", - "dotenv": "^16.4.5" + "dotenv": "^16.4.5", + "libsodium-wrappers": "^0.7.15", + "uuid": "^11.0.2" }, "devDependencies": { "nodemon": "^3.1.7" @@ -268,6 +271,23 @@ "node": ">= 8" } }, + "node_modules/asynckit": { + "version": "0.4.0", + "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "license": "MIT" + }, + "node_modules/axios": { + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", + "integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==", + "license": "MIT", + "dependencies": { + "follow-redirects": "^1.15.6", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "node_modules/balanced-match": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", @@ -337,6 +357,18 @@ "fsevents": "~2.3.2" } }, + "node_modules/combined-stream": { + "version": "1.0.8", + "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", + "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", + "dependencies": { + "delayed-stream": "~1.0.0" + }, + "engines": { + "node": ">= 0.8" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -362,6 +394,15 @@ } } }, + "node_modules/delayed-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", + "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", + "engines": { + "node": ">=0.4.0" + } + }, "node_modules/discord-api-types": { "version": "0.37.100", "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.37.100.tgz", @@ -425,6 +466,40 @@ "node": ">=8" } }, + "node_modules/follow-redirects": { + "version": "1.15.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.9.tgz", + "integrity": "sha512-gew4GsXizNgdoRyqmyfMHyAmXsZDk6mHkSxZFCzW9gwlbtOW44CDtYavM+y+72qD/Vq2l550kMF52DT8fOLJqQ==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "license": "MIT", + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, + "node_modules/form-data": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.1.tgz", + "integrity": "sha512-tzN8e4TX8+kkxGPK8D5u0FNmjPUjw3lwC9lSLxxoB/+GtsJG91CO8bSWy73APlgAZzZbXEYZJuxjkHH2w+Ezhw==", + "license": "MIT", + "dependencies": { + "asynckit": "^0.4.0", + "combined-stream": "^1.0.8", + "mime-types": "^2.1.12" + }, + "engines": { + "node": ">= 6" + } + }, "node_modules/fsevents": { "version": "2.3.3", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", @@ -516,6 +591,21 @@ "node": ">=0.12.0" } }, + "node_modules/libsodium": { + "version": "0.7.15", + "resolved": "https://registry.npmjs.org/libsodium/-/libsodium-0.7.15.tgz", + "integrity": "sha512-sZwRknt/tUpE2AwzHq3jEyUU5uvIZHtSssktXq7owd++3CSgn8RGrv6UZJJBpP7+iBghBqe7Z06/2M31rI2NKw==", + "license": "ISC" + }, + "node_modules/libsodium-wrappers": { + "version": "0.7.15", + "resolved": "https://registry.npmjs.org/libsodium-wrappers/-/libsodium-wrappers-0.7.15.tgz", + "integrity": "sha512-E4anqJQwcfiC6+Yrl01C1m8p99wEhLmJSs0VQqST66SbQXXBoaJY0pF4BNjRYa/sOQAxx6lXAaAFIlx+15tXJQ==", + "license": "ISC", + "dependencies": { + "libsodium": "^0.7.15" + } + }, "node_modules/lodash": { "version": "4.17.21", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", @@ -534,6 +624,27 @@ "integrity": "sha512-/k20Lg2q8LE5xiaaSkMXk4sfvI+9EGEykFS4b0CHHGWqDYU0bGUFSwchNOMA56D7TCs9GwVTkqe9als1/ns8UQ==", "license": "MIT" }, + "node_modules/mime-db": { + "version": "1.52.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", + "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/mime-types": { + "version": "2.1.35", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", + "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", + "dependencies": { + "mime-db": "1.52.0" + }, + "engines": { + "node": ">= 0.6" + } + }, "node_modules/minimatch": { "version": "3.1.2", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", @@ -632,6 +743,12 @@ } } }, + "node_modules/proxy-from-env": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" + }, "node_modules/pstree.remy": { "version": "1.1.8", "resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz", @@ -748,6 +865,19 @@ "integrity": "sha512-ve2KP6f/JnbPBFyobGHuerC9g1FYGn/F8n1LWTwNxCEzd6IfqTwUQcNXgEtmmQ6DlRrC1hrSrBnCZPokRrDHjw==", "license": "MIT" }, + "node_modules/uuid": { + "version": "11.0.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-11.0.2.tgz", + "integrity": "sha512-14FfcOJmqdjbBPdDjFQyk/SdT4NySW4eM0zcG+HqbHP5jzuH56xO3J1DGhgs/cEMCfwYi3HQI1gnTO62iaG+tQ==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist/esm/bin/uuid" + } + }, "node_modules/ws": { "version": "8.18.0", "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", diff --git a/package.json b/package.json index 78e482d..0f21df5 100644 --- a/package.json +++ b/package.json @@ -12,8 +12,11 @@ "license": "ISC", "dependencies": { "@discordjs/voice": "^0.17.0", + "axios": "^1.7.7", "discord.js": "^14.16.3", - "dotenv": "^16.4.5" + "dotenv": "^16.4.5", + "libsodium-wrappers": "^0.7.15", + "uuid": "^11.0.2" }, "devDependencies": { "nodemon": "^3.1.7" diff --git a/src/commands/join.js b/src/commands/join.js index 58b1ba2..ec9111a 100644 --- a/src/commands/join.js +++ b/src/commands/join.js @@ -4,20 +4,28 @@ const { joinVoiceChannel } = require('@discordjs/voice'); module.exports = { data: new SlashCommandBuilder() .setName('join') - .setDescription('Join the voice channel you are in'), + .setDescription('Join the voice channel you are in and listen to this text channel'), async execute(interaction) { const voiceChannel = interaction.member.voice.channel; + const textChannel = interaction.channel; if (!voiceChannel) { return interaction.reply('ボイスチャンネルに接続されていません。'); } + // ボイスチャンネルに参加 joinVoiceChannel({ channelId: voiceChannel.id, guildId: voiceChannel.guild.id, adapterCreator: voiceChannel.guild.voiceAdapterCreator, }); - await interaction.reply('ボイスチャンネルに接続しました。'); + // サーバーごとにチャンネル情報を保存 + interaction.client.guildChannels.set(interaction.guild.id, { + textChannelId: textChannel.id, + voiceChannelId: voiceChannel.id + }); + + await interaction.reply('ボイスチャンネルに接続し、このテキストチャンネルのメッセージを読み上げます。'); }, }; diff --git a/src/events/messageCreate.js b/src/events/messageCreate.js index 202da56..0bc36f7 100644 --- a/src/events/messageCreate.js +++ b/src/events/messageCreate.js @@ -1,11 +1,27 @@ +const { createAudioPlayer, createAudioResource, getVoiceConnection } = require('@discordjs/voice'); +const { synthesizeSpeech } = require('../services/tts'); + module.exports = { name: 'messageCreate', - execute(message) { + async execute(message) { + const client = message.client; + + // サーバーごとのチャンネル情報を取得 + const guildChannels = client.guildChannels.get(message.guild.id); + if (!guildChannels) return; + + // 記憶されたテキストチャンネルのメッセージのみ処理 + if (message.channel.id !== guildChannels.textChannelId) return; if (message.author.bot) return; - if (message.mentions.has(message.client.user)) { - const response = message.content.replace(`<@${message.client.user.id}>`, '').trim(); - message.channel.send(response || "メッセージを受け取りました!"); - } + const connection = getVoiceConnection(message.guild.id); + if (!connection) return; + + const audioUrl = await synthesizeSpeech(message.content); + const resource = createAudioResource(audioUrl); + const player = createAudioPlayer(); + + player.play(resource); + connection.subscribe(player); }, }; diff --git a/src/index.js b/src/index.js index 8d3de4d..35b169a 100644 --- a/src/index.js +++ b/src/index.js @@ -12,6 +12,9 @@ const client = new Client({ ] }); +// サーバーごとのテキストチャンネルとボイスチャンネルの情報を保持 +client.guildChannels = new Map(); + // コマンドの読み込み client.commands = new Collection(); const commands = []; diff --git a/src/services/tts.js b/src/services/tts.js new file mode 100644 index 0000000..c6f2f0b --- /dev/null +++ b/src/services/tts.js @@ -0,0 +1,23 @@ +const axios = require('axios'); +const fs = require('fs'); +const path = require('path'); +const { v4: uuidv4 } = require('uuid'); + +async function synthesizeSpeech(text) { + if (process.env.NODE_ENV === 'development') { + console.log('[development] Synthesizing speech...'); + const voicevoxUrl = `http://voicevox:50021/audio_query?text=${encodeURIComponent(text)}&speaker=1`; + const { data } = await axios.post(voicevoxUrl); + + const synthesisUrl = `http://voicevox:50021/synthesis?speaker=1`; + const response = await axios.post(synthesisUrl, data, { responseType: 'arraybuffer' }); + const filePath = path.join('/tmp', `${uuidv4()}.mp3`); + fs.writeFileSync(filePath, Buffer.from(response.data)); + return filePath; + } else { + console.log('[production] Synthesizing speech...'); + return path.join(__dirname, 'dummy.mp3'); // 本番用の音声生成処理 + } +} + +module.exports = { synthesizeSpeech };