Skip to content

Commit

Permalink
tts!!
Browse files Browse the repository at this point in the history
abuse another free service to get some sweet tts
also now uses a temp directory for temporary files
  • Loading branch information
artifishvr committed Apr 15, 2024
1 parent 6d7109a commit 6b69f7c
Show file tree
Hide file tree
Showing 4 changed files with 509 additions and 7 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,4 @@ dist
# TernJS port file
.tern-port

how.txt
db
temp
46 changes: 42 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Client, GatewayIntentBits } from 'discord.js'
import { Client, GatewayIntentBits } from 'discord.js';
import { joinVoiceChannel, createAudioPlayer, createAudioResource } from '@discordjs/voice';

import fs from 'fs'
import fs from 'fs';
import path from 'path';
import { DateTime } from 'luxon';

import { io } from "socket.io-client";
import CharacterAI from 'node_characterai';
import locateChrome from 'locate-chrome';
import { MsEdgeTTS } from "msedge-tts";

import * as dotenv from 'dotenv'

Expand All @@ -22,10 +24,13 @@ const client = new Client({
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildVoiceStates,
],
allowedMentions: { parse: [], repliedUser: false }
});

if (!fs.existsSync('./temp')) fs.mkdirSync('./temp');

const characterAI = new CharacterAI();
characterAI.requester.puppeteerPath = await new Promise(resolve => locateChrome((arg) => resolve(arg))) || '';
await characterAI.authenticateWithToken(process.env.CHARACTERAI_TOKEN);
Expand Down Expand Up @@ -110,17 +115,50 @@ client.on("messageCreate", async message => {

// Handle long responses
if (response.text.length >= 2000) {
fs.writeFileSync(path.resolve('./how.txt'), response.text);
message.reply({ content: "", files: ["./how.txt"], failIfNotExists: false });
fs.writeFileSync(path.resolve('./temp/how.txt'), response.text);
message.reply({ content: "", files: ["./temp/how.txt"], failIfNotExists: false });
return;
}

// Send AI response
message.reply({ content: `${response.text}`, failIfNotExists: false });

// tts!
if (message.member.voice.channel) {
tts(message, response.text);
}
} catch (error) {
console.error(error);
return message.reply(`❌ Error! Yell at arti.`);
}
});

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 channel = message.member.voice.channel;

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

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

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!');
}
}

client.login(process.env.DISCORD);
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spongegpt",
"version": "1.25.0",
"version": "1.26.1",
"description": "custom AI chatbot for discord",
"main": "index.js",
"type": "module",
Expand All @@ -11,10 +11,13 @@
"author": "artifish",
"license": "AGPL-3.0",
"dependencies": {
"@discordjs/voice": "^0.16.1",
"discord.js": "^14.13.0",
"dotenv": "^16.3.1",
"libsodium-wrappers": "^0.7.13",
"locate-chrome": "^0.1.1",
"luxon": "^3.4.4",
"msedge-tts": "^1.3.4",
"node_characterai": "^1.2.7",
"socket.io-client": "^4.7.2"
},
Expand Down
Loading

0 comments on commit 6b69f7c

Please sign in to comment.