From 62e3adf8a75ca95a0c0f4cd2137c1d39dc5c06b1 Mon Sep 17 00:00:00 2001 From: Talon Date: Mon, 5 Apr 2021 16:09:36 +0200 Subject: [PATCH] Initial module system --- api.js | 0 index.js | 124 ++++++++++++++++--------------------- modules/announcer/index.js | 25 ++++++++ modules/welcomer/index.js | 10 +++ 4 files changed, 88 insertions(+), 71 deletions(-) create mode 100644 api.js create mode 100644 modules/announcer/index.js create mode 100644 modules/welcomer/index.js diff --git a/api.js b/api.js new file mode 100644 index 0000000..e69de29 diff --git a/index.js b/index.js index 9d8694c..323cee3 100644 --- a/index.js +++ b/index.js @@ -6,87 +6,69 @@ const fs = require('fs'); const sha1 = require('sha1'); let joinedVoiceChannels = []; - +let modules = []; const bot = new Discord.Client(); -bot.on('message', (message) => { - console.log("I got a message", message); -}) - -bot.on('ready', async () => { - console.log("Bot initialized and listening"); - const guild = await bot.guilds.fetch(process.env.GUILD); - const channel = await bot.channels.fetch(process.env.CHANNEL); - await joinChannel(channel); - - speak(channel, `Hi! I'm alive. It is now ${new Date().toLocaleTimeString()} on ${new Date().toLocaleDateString()}`); -}) - -bot.on('voiceStateUpdate', async (oldState, newState) => { - if (newState.member.user.bot) return; - if (oldState.channel && newState.channel) return; - const channel = oldState.channel || newState.channel; - if (!channel) return; - if (channel.members.array().length < 2) { - return await leaveChannel(channel); - } - await joinChannel(channel); - let joined = false; - if (!oldState.channel) { - joined = true; - } - - let username = newState.member.displayName; - let str = ""; - if (!joined) { - str = username + " left the channel"; - } else { - str = username + " joined the channel"; - } - speak(channel, str); -}) -bot.login(process.env.TOKEN); -function isInVoiceChannel(channel) { - return joinedVoiceChannels.includes(channel); -} -function getConnectionForVoiceChannel(channel) { - return bot.voice.connections.find((conn) => conn.channel === channel); -} -async function generateVoice(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; -} -async function joinChannel(channel) { - if (!isInVoiceChannel(channel)) { - const res = await channel.join(); - joinedVoiceChannels.push(channel); + +const api = { + 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; + }, + + 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); } } -async function leaveChannel(channel) { - if (joinedVoiceChannels.includes(channel)) { - joinedVoiceChannels = joinedVoiceChannels.filter((chan) => chan !== channel); - await channel.leave(); - } +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)); } -async function speak(channel, message) { - const conn = getConnectionForVoiceChannel(channel); - const filepath = await generateVoice(message); - if (conn) conn.play(filepath); -} +registerModules(); +bot.login(process.env.TOKEN); \ No newline at end of file diff --git a/modules/announcer/index.js b/modules/announcer/index.js new file mode 100644 index 0000000..5404380 --- /dev/null +++ b/modules/announcer/index.js @@ -0,0 +1,25 @@ +module.exports = function(bot, api) { + bot.on('voiceStateUpdate', async (oldState, newState) => { + if (newState.member.user.bot) return; + if (oldState.channel && newState.channel) return; + const channel = oldState.channel || newState.channel; + if (!channel) return; + if (channel.members.array().length < 2) { + return await api.leaveChannel(channel); + } + await api.joinChannel(channel); + let joined = false; + if (!oldState.channel) { + joined = true; + } + + let username = newState.member.displayName; + let str = ""; + if (!joined) { + str = username + " left the channel"; + } else { + str = username + " joined the channel"; + } + api.speak(channel, str); + }) +} diff --git a/modules/welcomer/index.js b/modules/welcomer/index.js new file mode 100644 index 0000000..edd4001 --- /dev/null +++ b/modules/welcomer/index.js @@ -0,0 +1,10 @@ +module.exports = function(bot, api) { + bot.on('ready', async () => { + console.log("Bot initialized and listening"); + const guild = await bot.guilds.fetch(process.env.GUILD); + const channel = await bot.channels.fetch(process.env.CHANNEL); + await api.joinChannel(channel); + + api.speak(channel, `Hi! I'm alive. It is now ${new Date().toLocaleTimeString()} on ${new Date().toLocaleDateString()}`); + }) +} \ No newline at end of file