Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8835

discord.jsで音声合成機能を作る

$
0
0

今回つくるもの

1.discord上で読み上げるテキストを指定するコマンドを実行
2.メッセージから読みあげたいテキストを取得
3.GoogleのTTSAPIにリクエスト送信
4.結果を取得
5.1が送信されたチャンネルに結果を送信

必要なもの

・node-fetch(npm i node-fetch)
・stream(npm i stream)
・util(npm i util)
・discord.js(npm i discord.js);
discord.jsの実行環境の整備は、ここでは省きます。

コード

constprefix="!";const{createWriteStream}=require('fs');const{pipeline}=require('stream');const{promisify}=require('util');constfetch=require("node-fetch");client.on("message",asyncmessage=>{if(message.author.bot)return;constargs=message.content.slice(prefix.length).trim().split(/ +/g);constcommand=args.shift().toLowerCase();if(command=="voicetext"){constt=args.join("");constm=Math.random().toString(36).slice(2,12);conststreamPipeline=promisify(pipeline);constresponse=awaitfetch(`https://www.google.com/speech-api/v1/synthesize?text=${encodeURI(t)}&nc=mpeg&lang=ja&speed=0.5&client=lr-language-tts`);if(!response.ok)thrownewError(`unexpected response ${response.statusText}`);awaitstreamPipeline(response.body,createWriteStream(`./voices/${m}.mp3`));message.channel.send("結果:",{files:[`./voices/${m}.mp3`]});}});

結果

!voicetext <text>を実行すると、音声合成した結果が返ってきます!
スクリーンショット 2021-03-27 174757.png


Viewing all articles
Browse latest Browse all 8835

Trending Articles