Initial module system

watson
Talon 2021-04-05 16:09:36 +02:00
parent 9050e4b6ed
commit 62e3adf8a7
4 changed files with 88 additions and 71 deletions

0
api.js 100644
View File

View File

@ -6,58 +6,26 @@ const fs = require('fs');
const sha1 = require('sha1'); const sha1 = require('sha1');
let joinedVoiceChannels = []; let joinedVoiceChannels = [];
let modules = [];
const bot = new Discord.Client(); 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) {
const api = {
isInVoiceChannel: (channel) => {
return joinedVoiceChannels.includes(channel); return joinedVoiceChannels.includes(channel);
} },
function getConnectionForVoiceChannel(channel) { getConnectionForVoiceChannel: (channel) => {
return bot.voice.connections.find((conn) => conn.channel === channel); return bot.voice.connections.find((conn) => conn.channel === channel);
} },
async function generateVoice(string) { generateVoice: async (string) => {
const hash = sha1(string); const hash = sha1(string);
const filepath = process.env.VOICE_TMP_PATH + hash + ".mp3"; const filepath = process.env.VOICE_TMP_PATH + hash + ".mp3";
if (!fs.existsSync(filepath)) { if (!fs.existsSync(filepath)) {
@ -69,24 +37,38 @@ async function generateVoice(string) {
fs.writeFileSync(filepath, buf); fs.writeFileSync(filepath, buf);
} }
return filepath; return filepath;
} },
async function joinChannel(channel) { joinChannel: async (channel) => {
if (!isInVoiceChannel(channel)) { if (!api.isInVoiceChannel(channel)) {
const res = await channel.join(); const res = await channel.join();
joinedVoiceChannels.push(channel); joinedVoiceChannels.push(channel);
} }
} },
async function leaveChannel(channel) { leaveChannel: async (channel) => {
if (joinedVoiceChannels.includes(channel)) { if (joinedVoiceChannels.includes(channel)) {
joinedVoiceChannels = joinedVoiceChannels.filter((chan) => chan !== channel); joinedVoiceChannels = joinedVoiceChannels.filter((chan) => chan !== channel);
await channel.leave(); await channel.leave();
} }
} },
async function speak(channel, message) { speak: async (channel, message) => {
const conn = getConnectionForVoiceChannel(channel); const conn = api.getConnectionForVoiceChannel(channel);
const filepath = await generateVoice(message); const filepath = await api.generateVoice(message);
if (conn) conn.play(filepath); if (conn) conn.play(filepath);
} }
}
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));
}
registerModules();
bot.login(process.env.TOKEN);

View File

@ -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);
})
}

View File

@ -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()}`);
})
}