2024-09-08 13:13:51 +00:00
|
|
|
const sodium = require('libsodium-wrappers');
|
|
|
|
const { Client, GatewayIntentBits } = require('discord.js');
|
2024-09-08 14:19:10 +00:00
|
|
|
const { joinVoiceChannel, getVoiceConnection, VoiceConnectionStatus, EndBehaviorType } = require('@discordjs/voice');
|
2024-09-08 13:13:51 +00:00
|
|
|
const fs = require('fs');
|
2024-09-08 14:19:10 +00:00
|
|
|
const { OpusEncoder } = require('@discordjs/opus');
|
2024-09-08 13:13:51 +00:00
|
|
|
const { exec } = require('child_process');
|
2024-09-08 14:19:10 +00:00
|
|
|
const OpenAI = require('openai');
|
|
|
|
|
|
|
|
// OpenAI APIキーの確認
|
|
|
|
const apiKey = process.env.OPENAI_API_KEY || 'your-openai-api-key';
|
|
|
|
if (!apiKey || apiKey === 'your-openai-api-key') {
|
|
|
|
console.error('Error: OpenAI API key is missing or invalid. Set the API key in the environment variables.');
|
|
|
|
process.exit(1); // エラーコード1で終了
|
|
|
|
}
|
|
|
|
|
|
|
|
// OpenAI APIクライアントの作成
|
|
|
|
const openai = new OpenAI({
|
|
|
|
apiKey: apiKey,
|
|
|
|
});
|
2024-09-08 13:13:51 +00:00
|
|
|
|
|
|
|
sodium.ready.then(() => {
|
|
|
|
console.log('Sodium library loaded!');
|
|
|
|
});
|
|
|
|
|
|
|
|
const client = new Client({
|
|
|
|
intents: [
|
|
|
|
GatewayIntentBits.Guilds,
|
|
|
|
GatewayIntentBits.GuildVoiceStates,
|
|
|
|
GatewayIntentBits.GuildMessages,
|
|
|
|
GatewayIntentBits.MessageContent,
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
client.once('ready', () => {
|
|
|
|
console.log('Bot is ready!');
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('messageCreate', async message => {
|
|
|
|
if (message.content === '!join') {
|
|
|
|
if (message.member.voice.channel) {
|
|
|
|
const connection = joinVoiceChannel({
|
|
|
|
channelId: message.member.voice.channel.id,
|
|
|
|
guildId: message.guild.id,
|
|
|
|
adapterCreator: message.guild.voiceAdapterCreator,
|
|
|
|
selfDeaf: false,
|
|
|
|
selfMute: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.on(VoiceConnectionStatus.Ready, () => {
|
|
|
|
console.log('Bot has connected to the channel!');
|
|
|
|
startRecording(connection, message.member.voice.channel);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
message.reply('ボイスチャンネルに接続してください。');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (message.content === '!leave') {
|
|
|
|
const connection = getVoiceConnection(message.guild.id);
|
|
|
|
if (connection) {
|
|
|
|
connection.destroy();
|
|
|
|
message.reply('ボイスチャンネルを離れました。');
|
|
|
|
} else {
|
|
|
|
message.reply('ボイスチャンネルに接続していません。');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function startRecording(connection, voiceChannel) {
|
|
|
|
const receiver = connection.receiver;
|
|
|
|
const activeStreams = new Map(); // ユーザーごとの音声ストリーム管理
|
|
|
|
|
|
|
|
receiver.speaking.on('start', (userId) => {
|
|
|
|
const user = voiceChannel.members.get(userId);
|
|
|
|
if (!user) {
|
|
|
|
console.error('User is undefined');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Started receiving audio from user: ${user.id}`);
|
|
|
|
|
|
|
|
if (activeStreams.has(user.id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const audioStream = receiver.subscribe(user.id, {
|
|
|
|
end: EndBehaviorType.Manual
|
|
|
|
});
|
|
|
|
|
|
|
|
const filePath = `./recordings/${user.id}-${Date.now()}.pcm`;
|
|
|
|
const writableStream = fs.createWriteStream(filePath);
|
|
|
|
activeStreams.set(user.id, { audioStream, writableStream, filePath });
|
|
|
|
|
|
|
|
const opusDecoder = new OpusEncoder(48000, 2); // Opusデコーダーを使用
|
|
|
|
audioStream.on('data', (chunk) => {
|
|
|
|
try {
|
|
|
|
const pcmData = opusDecoder.decode(chunk);
|
|
|
|
writableStream.write(pcmData);
|
|
|
|
console.log(`Received ${chunk.length} bytes of audio data from user ${user.id}`);
|
|
|
|
} catch (err) {
|
|
|
|
console.error('Error decoding audio chunk:', err);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
audioStream.on('error', (err) => {
|
|
|
|
console.error('Error in the audio stream:', err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
receiver.speaking.on('end', (userId) => {
|
|
|
|
const streamData = activeStreams.get(userId);
|
|
|
|
if (!streamData) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { writableStream, filePath } = streamData;
|
|
|
|
|
|
|
|
writableStream.end(() => {
|
|
|
|
console.log(`Recording saved to ${filePath}`);
|
|
|
|
activeStreams.delete(userId);
|
|
|
|
|
|
|
|
fs.stat(filePath, (err, stats) => {
|
|
|
|
if (err) {
|
|
|
|
console.error('Error checking file:', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stats.size > 0) {
|
2024-09-08 14:19:10 +00:00
|
|
|
convertToWavAndTranscribe(filePath); // バックグラウンドで変換と文字起こしを実行
|
2024-09-08 13:13:51 +00:00
|
|
|
} else {
|
|
|
|
console.log(`File ${filePath} is empty, skipping conversion.`);
|
|
|
|
fs.unlinkSync(filePath); // 空のファイルを削除
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-08 14:19:10 +00:00
|
|
|
function convertToWavAndTranscribe(pcmPath) {
|
2024-09-08 13:13:51 +00:00
|
|
|
const wavPath = pcmPath.replace('.pcm', '.wav');
|
2024-09-08 14:19:10 +00:00
|
|
|
console.log(`Converting ${pcmPath} to ${wavPath} and transcribing in the background`);
|
2024-09-08 13:13:51 +00:00
|
|
|
|
2024-09-08 14:19:10 +00:00
|
|
|
const ffmpeg = exec(`ffmpeg -f s16le -ar 48000 -ac 2 -i ${pcmPath} ${wavPath}`, (error, stdout, stderr) => {
|
2024-09-08 13:13:51 +00:00
|
|
|
if (error) {
|
|
|
|
console.error(`Error during conversion: ${error.message}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.error(`FFmpeg stderr: ${stderr}`);
|
|
|
|
return;
|
|
|
|
}
|
2024-09-08 14:19:10 +00:00
|
|
|
});
|
2024-09-08 13:13:51 +00:00
|
|
|
|
2024-09-08 14:19:10 +00:00
|
|
|
ffmpeg.on('close', (code) => {
|
|
|
|
if (code === 0) {
|
|
|
|
console.log(`Successfully converted ${pcmPath} to ${wavPath}`);
|
|
|
|
fs.unlinkSync(pcmPath); // PCMファイルを削除
|
|
|
|
|
|
|
|
// Whisper APIを使って文字起こしを実行
|
|
|
|
transcribeAudio(wavPath);
|
|
|
|
} else {
|
|
|
|
console.error(`FFmpeg exited with code ${code}, conversion failed.`);
|
|
|
|
}
|
2024-09-08 13:13:51 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-08 14:19:10 +00:00
|
|
|
// Whisper APIを使って音声ファイルを文字起こしする関数
|
|
|
|
async function transcribeAudio(filePath) {
|
|
|
|
try {
|
|
|
|
console.log(`Transcribing file: ${filePath}`);
|
|
|
|
|
|
|
|
const response = await openai.audio.transcriptions.create({
|
|
|
|
file: fs.createReadStream(filePath),
|
|
|
|
model: 'whisper-1',
|
|
|
|
response_format: 'json' // レスポンス形式をJSONに指定
|
|
|
|
});
|
|
|
|
|
2024-09-08 14:39:40 +00:00
|
|
|
// 文字起こし結果を取得
|
2024-09-08 14:19:10 +00:00
|
|
|
if (response && response.text) {
|
2024-09-08 14:39:40 +00:00
|
|
|
const transcription = response.text;
|
|
|
|
console.log('Transcription:', transcription);
|
|
|
|
|
|
|
|
// ChatGPTを使用して返答を生成
|
|
|
|
const reply = await generateChatResponse(transcription);
|
|
|
|
console.log('ChatGPT response:', reply);
|
|
|
|
|
|
|
|
// 必要に応じて、返信内容をメッセージとして送信
|
|
|
|
// 例えば、メッセージを元のチャンネルに送信するなど
|
|
|
|
// message.channel.send(reply); // メッセージを送信する場合
|
|
|
|
|
2024-09-08 14:19:10 +00:00
|
|
|
} else {
|
|
|
|
console.error('Error: Transcription response is missing "text" field.');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error during transcription:', error.response ? error.response.data : error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 14:39:40 +00:00
|
|
|
// ChatGPTに応答を生成させる関数
|
|
|
|
async function generateChatResponse(transcription) {
|
|
|
|
try {
|
|
|
|
const chatResponse = await openai.chat.completions.create({
|
|
|
|
model: 'gpt-3.5-turbo', // ChatGPTのモデルを指定
|
|
|
|
messages: [
|
|
|
|
{ role: 'system', content: 'You are a helpful assistant.' },
|
|
|
|
{ role: 'user', content: transcription }
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
if (chatResponse && chatResponse.choices && chatResponse.choices.length > 0) {
|
|
|
|
return chatResponse.choices[0].message.content; // 応答内容を返す
|
|
|
|
} else {
|
|
|
|
console.error('Error: ChatGPT response is missing choices.');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error during ChatGPT response generation:', error.response ? error.response.data : error.message);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 14:19:10 +00:00
|
|
|
client.login(process.env.BOT_TOKEN);
|