tardis-bot/index.js

90 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-04-05 13:50:51 +00:00
const Discord = require('discord.js');
require('dotenv').config();
const tts = require('google-tts-api');
const fetch = require('node-fetch');
const fs = require('fs');
const sha1 = require('sha1');
2021-04-05 15:49:04 +00:00
const sqlite = require('sqlite3');
2021-04-05 13:50:51 +00:00
let joinedVoiceChannels = [];
2021-04-05 14:09:36 +00:00
let modules = [];
2021-04-05 17:11:08 +00:00
let commandHandlers = new Map();
2021-04-05 13:50:51 +00:00
2021-04-05 15:49:04 +00:00
const bot = new Discord.Client();
2021-04-05 13:50:51 +00:00
2021-04-05 15:49:04 +00:00
const db = new sqlite.Database(process.env.DB_FILE);
2021-04-05 13:50:51 +00:00
2021-04-05 14:09:36 +00:00
const api = {
2021-04-05 15:49:04 +00:00
db: db,
2021-04-05 14:09:36 +00:00
isInVoiceChannel: (channel) => {
return joinedVoiceChannels.includes(channel);
},
getConnectionForVoiceChannel: (channel) => {
return bot.voice.connections.find((conn) => conn.channel === channel);
},
generateVoice: async (string) => {
const hash = sha1(string);
const filepath = process.env.VOICE_TMP_PATH + hash + ".mp3";
if (!fs.existsSync(filepath)) {
const url = tts.getAudioUrl(string, {lang: "en-us"});
console.log("Generated url: " + url);
const data = await fetch(url);
const contents = await data.arrayBuffer();
const buf = Buffer.from(contents);
fs.writeFileSync(filepath, buf);
}
return filepath;
},
2021-04-05 13:50:51 +00:00
2021-04-05 14:09:36 +00:00
joinChannel: async (channel) => {
if (!api.isInVoiceChannel(channel)) {
const res = await channel.join();
joinedVoiceChannels.push(channel);
}
},
leaveChannel: async (channel) => {
if (joinedVoiceChannels.includes(channel)) {
joinedVoiceChannels = joinedVoiceChannels.filter((chan) => chan !== channel);
await channel.leave();
}
},
speak: async (channel, message) => {
const conn = api.getConnectionForVoiceChannel(channel);
const filepath = await api.generateVoice(message);
if (conn) conn.play(filepath);
2021-04-05 17:11:08 +00:00
},
registerCommand: async (commandString, commandFunc) => {
commandHandlers.set(commandString, commandFunc);
2021-04-05 13:50:51 +00:00
}
}
2021-04-05 14:09:36 +00:00
function registerModules() {
console.log(`Registering modules...`);
const moduleDirectories = fs.readdirSync('./modules');
moduleDirectories.forEach((dir) => {
modules.push(require(`./modules/${dir}/index.js`));
console.log(`Loading ./modules/${dir}/index.js`)
})
modules.forEach((mod) => mod(bot, api));
2021-04-05 13:50:51 +00:00
}
2021-04-05 14:09:36 +00:00
2021-04-05 17:11:08 +00:00
function handleMessage(message) {
2021-04-05 21:21:47 +00:00
if (message.content.startsWith(process.env.PREFIX)) {
2021-04-05 17:11:08 +00:00
const args = message.contents.split(" ");
const command = args[0].substr(1, args[0].length);
const execution = commandHandlers.get(command);
if (command) {
command(args, message);
}
}
}
2021-04-05 14:09:36 +00:00
registerModules();
2021-04-05 17:11:08 +00:00
bot.login(process.env.TOKEN);
bot.on('message', handleMessage);