Initial module system
parent
9050e4b6ed
commit
62e3adf8a7
86
index.js
86
index.js
|
@ -6,58 +6,26 @@ 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) {
|
||||
|
||||
|
||||
|
||||
|
||||
const api = {
|
||||
isInVoiceChannel: (channel) => {
|
||||
return joinedVoiceChannels.includes(channel);
|
||||
}
|
||||
},
|
||||
|
||||
function getConnectionForVoiceChannel(channel) {
|
||||
getConnectionForVoiceChannel: (channel) => {
|
||||
return bot.voice.connections.find((conn) => conn.channel === channel);
|
||||
}
|
||||
},
|
||||
|
||||
async function generateVoice(string) {
|
||||
generateVoice: async (string) => {
|
||||
const hash = sha1(string);
|
||||
const filepath = process.env.VOICE_TMP_PATH + hash + ".mp3";
|
||||
if (!fs.existsSync(filepath)) {
|
||||
|
@ -69,24 +37,38 @@ async function generateVoice(string) {
|
|||
fs.writeFileSync(filepath, buf);
|
||||
}
|
||||
return filepath;
|
||||
}
|
||||
},
|
||||
|
||||
async function joinChannel(channel) {
|
||||
if (!isInVoiceChannel(channel)) {
|
||||
joinChannel: async (channel) => {
|
||||
if (!api.isInVoiceChannel(channel)) {
|
||||
const res = await channel.join();
|
||||
joinedVoiceChannels.push(channel);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
async function leaveChannel(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 speak(channel, message) {
|
||||
const conn = getConnectionForVoiceChannel(channel);
|
||||
const filepath = await generateVoice(message);
|
||||
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);
|
|
@ -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);
|
||||
})
|
||||
}
|
|
@ -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()}`);
|
||||
})
|
||||
}
|
Loading…
Reference in New Issue