Skip to content

Commit

Permalink
reduce cognitive complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
artifishvr committed Apr 24, 2024
1 parent e72ca83 commit 456ed0b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 45 deletions.
90 changes: 46 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,30 +115,7 @@ client.on("messageCreate", async message => {
return;
}

let imageDetails = '';
if (message.attachments.size > 0 && !backendsocket.disconnected) {
let promises = [];

for (const attachment of message.attachments.values()) {
try {
const url = attachment.url;
const promise = new Promise((resolve) => {
message.channel.sendTyping();
backendsocket.emit("imgcaption", url, (val) => {
imageDetails += `Attached: image of ${val[0].generated_text}\n`;
resolve();
});
});
promises.push(promise);
} catch (error) {
console.error(error);
return message.reply(`❌ Error! Yell at arti.`);
};
}

message.channel.sendTyping();
await Promise.all(promises);
}
let imageDetails = await imageRecognition(message)

// Send message to CharacterAI
let formattedUserMessage = `${message.author.username} (${await getPronouns(message.author.id)}) on ${DateTime.now().setZone('utc').toLocaleString(DateTime.DATETIME_FULL)}: ${message.content}\n${imageDetails}`;
Expand Down Expand Up @@ -183,32 +160,57 @@ client.on("messageCreate", async message => {
}
});

async function imageRecognition(message) {
if (message.attachments.size > 0 && !backendsocket.disconnected) {
let imageDetails = '';
let promises = [];

for (const attachment of message.attachments.values()) {
try {
const url = attachment.url;
const promise = new Promise((resolve) => {
message.channel.sendTyping();
backendsocket.emit("imgcaption", url, (val) => {
imageDetails += `Attached: image of ${val[0].generated_text}\n`;
resolve();
});
});
promises.push(promise);
} catch (error) {
console.error(error);
return message.reply(`❌ Error! Yell at arti.`);
};
}

await Promise.all(promises);
return imageDetails;
} else {
return "";
}
}

async function tts(message, text) {
if (message.member.voice.channel) {
const tts = new MsEdgeTTS();
await tts.setMetadata("en-US-AnaNeural", MsEdgeTTS.OUTPUT_FORMAT.AUDIO_24KHZ_96KBITRATE_MONO_MP3);
const filePath = await tts.toFile("./temp/audio.mp3", text);
const tts = new MsEdgeTTS();
await tts.setMetadata("en-US-AnaNeural", MsEdgeTTS.OUTPUT_FORMAT.AUDIO_24KHZ_96KBITRATE_MONO_MP3);
const filePath = await tts.toFile("./temp/audio.mp3", text);

const channel = message.member.voice.channel;
const channel = message.member.voice.channel;

const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});
const connection = joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: channel.guild.voiceAdapterCreator,
});

const player = createAudioPlayer();
const resource = createAudioResource(fs.createReadStream(filePath));
const player = createAudioPlayer();
const resource = createAudioResource(fs.createReadStream(filePath));

connection.subscribe(player);
player.play(resource);
connection.subscribe(player);
player.play(resource);

player.on('error', error => {
console.error(`Audio Error: ${error.message}`);
});
} else {
message.reply('You need to join a voice channel first!');
}
player.on('error', error => {
console.error(`Audio Error: ${error.message}`);
});
}

client.login(process.env.DISCORD);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spongegpt",
"version": "1.28.2",
"version": "1.28.3",
"description": "custom AI chatbot for discord",
"main": "index.js",
"type": "module",
Expand Down

0 comments on commit 456ed0b

Please sign in to comment.