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

132
index.js
View File

@ -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;
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);
}
}
let username = newState.member.displayName;
let str = "";
if (!joined) {
str = username + " left the channel";
} else {
str = username + " joined the channel";
}
speak(channel, str);
})
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);
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);
}
}
async function leaveChannel(channel) {
if (joinedVoiceChannels.includes(channel)) {
joinedVoiceChannels = joinedVoiceChannels.filter((chan) => chan !== channel);
await channel.leave();
}
}
async function speak(channel, message) {
const conn = getConnectionForVoiceChannel(channel);
const filepath = await generateVoice(message);
if (conn) conn.play(filepath);
}

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