From 487bc13970d1649356758cd29ae28d10b434e6e6 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Sat, 15 Jun 2024 03:03:43 -0400 Subject: [PATCH] Adding proper support for audio messages via bot --- chatBot/commands/test-audio.js | 10 ++++++++++ chatBot/includes/Client.js | 16 +++++++++++++++- chatBot/includes/message/AudioMessage.js | 12 ++++++++---- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 chatBot/commands/test-audio.js diff --git a/chatBot/commands/test-audio.js b/chatBot/commands/test-audio.js new file mode 100644 index 0000000..d185839 --- /dev/null +++ b/chatBot/commands/test-audio.js @@ -0,0 +1,10 @@ +export default { + handler: function(bot, args, message) { + // Specify the path to the audio file you want to send + const audioFilePath = '/Users/raven/discord-linux.mp3'; // Replace with the actual audio file path + const audioType = 'audio'; // Specify the correct audio file type + + // Send the audio message using the bot instance + bot.sendAudioMessage(audioFilePath, audioType); + } + }; \ No newline at end of file diff --git a/chatBot/includes/Client.js b/chatBot/includes/Client.js index 011d17e..6a48679 100644 --- a/chatBot/includes/Client.js +++ b/chatBot/includes/Client.js @@ -114,7 +114,7 @@ class Client extends EventEmitter { if (msgType === "audio") { const audioBuffer = await this.drive.get(`/audio/${messageObj.audioName}`); - this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, `http://localhost:${this.servePort}/audio/${messageObj.audioName}`, messageObj.audioType)); + this.emit('onAudio', peer, new AudioMessage(peerName, peerAvatar, this.currentTopic, timestamp, `http://localhost:${this.servePort}/audio/${messageObj.audioName}`, messageObj.audioType, messageObj.audioData)); } } }); @@ -163,6 +163,20 @@ class Client extends EventEmitter { } } + async sendAudioMessage(filePath, audioType) { + try { + await this.drive.ready(); + const audioBuffer = fs.readFileSync(filePath); + const audioName = path.basename(filePath); + await this.drive.put(`/audio/${audioName}`, audioBuffer); + const audioUrl = `http://localhost:${this.servePort}/audio/${audioName}`; + const audioMessage = AudioMessage.new(this, audioUrl, audioType, audioBuffer); // Pass audioBuffer to the new method + this.sendMessage(audioMessage); + } catch (error) { + console.error('Error sending audio message:', error); + } + } + sendMessage(message) { if (!(message instanceof Message)) { console.error(`message does not extend Message class (TextMessage, FileMessage, AudioMessage).`, message); diff --git a/chatBot/includes/message/AudioMessage.js b/chatBot/includes/message/AudioMessage.js index d1e2108..fd0dce6 100644 --- a/chatBot/includes/message/AudioMessage.js +++ b/chatBot/includes/message/AudioMessage.js @@ -1,22 +1,26 @@ import Message from "./Message.js"; +import b4a from "b4a"; class AudioMessage extends Message { - constructor(peerName, peerAvatar, topic, timestamp, audioUrl, audioType) { + constructor(peerName, peerAvatar, topic, timestamp, audioUrl, audioType, audioData) { super("audio", peerName, peerAvatar, topic, timestamp); this.audioUrl = audioUrl; this.audioType = audioType; + this.audioData = audioData; // Add audio data property } toJsonString() { return JSON.stringify({ ...this.toJson(), audioUrl: this.audioUrl, - audioType: this.audioType + audioType: this.audioType, + audio: this.audioData // Include audio data in JSON }); } - static new(bot, audioUrl, audioType) { - return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType); + static new(bot, audioUrl, audioType, audioBuffer) { + const audioData = b4a.toString(audioBuffer, 'base64'); // Convert audio buffer to base64 + return new AudioMessage(bot.botName, bot.botAvatar, bot.currentTopic, Date.now(), audioUrl, audioType, audioData); } }