tardis-bot/index.js

158 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2021-04-05 13:50:51 +00:00
const Discord = require('discord.js');
const Voice = require("@discordjs/voice");
const adapterCreator = require("./adapter");
2021-04-05 13:50:51 +00:00
require('dotenv').config();
const fetch = require('node-fetch');
const fs = require('fs');
const sha1 = require('sha1');
const sqlite3 = require('sqlite3');
const { open } = require('sqlite')
2021-04-05 13:50:51 +00:00
let joinedVoiceChannels = [];
let joinedVoiceChannelConnections = new Map();
2021-04-05 14:09:36 +00:00
let modules = [];
2021-04-05 13:50:51 +00:00
let commandHandlers = new Map();
const player = Voice.createAudioPlayer();
const rest = new Discord.REST({ version: '10' }).setToken(process.env["TOKEN"]);
const bot = new Discord.Client({
intents: [
Discord.GatewayIntentBits.GuildMembers,
Discord.GatewayIntentBits.GuildMessageReactions,
Discord.GatewayIntentBits.GuildMessages,
Discord.GatewayIntentBits.GuildPresences,
Discord.GatewayIntentBits.GuildVoiceStates,
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.MessageContent
]
});
2021-04-05 13:50:51 +00:00
async function initDB() {
console.log(__dirname);
api.db = await open({
2022-11-30 22:38:55 +00:00
filename: process.env["DB_FILE"],
driver: sqlite3.Database
});
}
2021-04-05 13:50:51 +00:00
2021-04-05 14:09:36 +00:00
const api = {
player: player,
db: undefined,
2021-04-09 11:23:13 +00:00
queue: undefined,
strings: require('./strings/' + process.env.STRING_SET + '.json'),
2021-04-06 00:16:09 +00:00
ttsEngines: (() => {
2021-04-06 23:36:35 +00:00
let engines = {};
console.log(`Registering TTS engines...`);
const engineDirectories = fs.readdirSync('./tts');
engineDirectories.forEach((dir) => {
if (dir.startsWith('.')) return;
eng = require(`./tts/${dir}/index.js`);
engines[dir] = new eng;
console.log(`Loading ./tts/${dir}/index.js`)
})
return engines;
2021-04-06 00:16:09 +00:00
})(),
2021-04-06 23:36:35 +00:00
announcementVoice: process.env.ANNOUNCEMENT_VOICE,
announcementEngine: undefined,
play: (file) => {
return player.play(Voice.createAudioResource(file));
},
2021-04-06 23:36:35 +00:00
respond: (message, text, voiceText) => {
let toSend = message.member.displayName + ", " + (voiceText ? voiceText : text);
if (message.member.voice.channel) {
2021-04-09 11:33:14 +00:00
api.queue.add(__dirname + "/sysmsg.wav");
2021-04-06 23:36:35 +00:00
api.speak(message.member.voice.channel, toSend);
} else {
message.reply(text);
}
},
getActiveVoiceChannel: () => joinedVoiceChannels[0],
2021-04-05 14:09:36 +00:00
isInVoiceChannel: (channel) => {
return joinedVoiceChannels.includes(channel);
},
getConnectionForVoiceChannel: (channel) => {
return joinedVoiceChannelConnections.get(channel);
2021-04-05 14:09:36 +00:00
},
2021-04-06 00:16:09 +00:00
generateVoice: async (string, engine, voice, params) => {
2021-04-06 23:36:35 +00:00
const hash = sha1(voice + string);
2021-04-06 00:16:09 +00:00
const filepath = process.env.VOICE_TMP_PATH + hash + '.' + engine.fileExtension;
2021-04-05 14:09:36 +00:00
if (!fs.existsSync(filepath)) {
2021-04-06 00:16:09 +00:00
await engine.getSpeechFile(string, filepath, voice, params);
2021-04-05 14:09:36 +00:00
}
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 = Voice.joinVoiceChannel({
channelId: channel.id,
guildId: channel.guild.id,
adapterCreator: adapterCreator(channel)
});
res.subscribe(player);
2021-04-05 14:09:36 +00:00
joinedVoiceChannels.push(channel);
joinedVoiceChannelConnections.set(channel, res);
2021-04-05 14:09:36 +00:00
}
},
leaveChannel: async (channel) => {
if (joinedVoiceChannels.includes(channel)) {
let con = joinedVoiceChannelConnections.get(channel);
2021-04-05 14:09:36 +00:00
joinedVoiceChannels = joinedVoiceChannels.filter((chan) => chan !== channel);
con.disconnect();
joinedVoiceChannelConnections.delete(channel);
2021-04-05 14:09:36 +00:00
}
},
2021-04-06 23:36:35 +00:00
speak: async (channel, message, engine = api.announcementEngine, voice = api.announcementVoice, params = {}) => {
2021-04-06 00:16:09 +00:00
const filepath = await api.generateVoice(message, engine, voice, params);
2021-04-09 11:23:13 +00:00
api.queue.add(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() {
2021-04-06 23:36:35 +00:00
console.log(`Registering modules...`);
2021-04-05 14:09:36 +00:00
const moduleDirectories = fs.readdirSync('./modules');
moduleDirectories.forEach((dir) => {
2021-04-06 23:36:35 +00:00
if (dir.startsWith('.')) return;
modules.push(require(`./modules/${dir}`));
2021-04-05 14:09:36 +00:00
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) {
console.log(`I got message`);
2021-04-05 21:21:47 +00:00
if (message.content.startsWith(process.env.PREFIX)) {
const args = message.content.split(" ");
2021-04-05 17:11:08 +00:00
const command = args[0].substr(1, args[0].length);
const execution = commandHandlers.get(command);
if (command) {
2021-04-06 23:36:35 +00:00
if (execution) execution(args, message);
2021-04-05 17:11:08 +00:00
}
}
}
api.announcementEngine = api.ttsEngines[process.env.ANNOUNCEMENT_ENGINE];
async function start() {
await initDB();
registerModules();
}
2021-04-05 17:11:08 +00:00
bot.login(process.env.TOKEN);
bot.on('messageCreate', handleMessage);
start();