Last active
April 30, 2018 12:45
-
-
Save intercaetera/aded293e0032ff53e73302ad9a1f8fef to your computer and use it in GitHub Desktop.
Discovery server watcher for Discord
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config() | |
const Discord = require("discord.js") | |
const client = new Discord.Client() | |
const mongoose = require('mongoose') | |
mongoose.connect(process.env.MONGO) | |
const db = mongoose.connection | |
db.on('error', console.error.bind(console, 'connection error ')) | |
db.once('open', () => { console.log('connected to db') }) | |
const Disco = require('discoverygc').default | |
const disco = new Disco({ key: process.env.DISCOAPI }) | |
const Guild = require('./model') | |
const PREFIX = "mocca " | |
const HELP_MESSAGE = `**Syntax**: | |
- \`mocca watch [tag] [system] [channel_id] [importance (1-3)]\` | |
- \`mocca delete [tag]\` | |
- \`mocca list\`` | |
let status = {} | |
client.on('ready', () => { | |
setInterval(tick, 60*1000) | |
tick() | |
console.log(`Logged in as ${client.user.tag}!`) | |
}) | |
client.on('message', msg => { | |
if(!msg.guild) return | |
if(msg.author.id != msg.guild.ownerID) return | |
if(msg.content.startsWith(PREFIX)) { | |
let args = msg.content.split(' ') | |
if(args[1] === 'help') { | |
msg.reply(HELP_MESSAGE) | |
} | |
if(args[1] === 'watch') { | |
// Destructure the command | |
let [ prefix, | |
command, | |
tag, | |
system, | |
channel, | |
importance ] = args | |
if(!tag) { | |
msg.reply('Tag is required.') | |
return | |
} | |
// Defaults and parsing | |
if(system) system = system.split('_').join(' ') | |
else system = '*' | |
if(!channel || channel === 'here') channel = msg.channel.id | |
if(!importance || importance > 3 || importance < 1) importance = 1 | |
else importance = parseInt(importance) | |
// find guild | |
Guild.find({ guildId: msg.guild.id }, (err, res) => { | |
if(err) { | |
console.log(err.message) | |
msg.reply("Error: " + err.message) | |
return | |
} | |
// if not found, create one with a single watcher | |
if(!res[0]) { | |
const guild = { | |
guildId: msg.guild.id, | |
guildName: msg.guild.name, | |
guildOwner: msg.guild.owner.user.tag, | |
watchers: [ { tag, system, channel, importance } ] | |
} | |
Guild.create(guild, err => { | |
if(err) { | |
console.log(err.message) | |
msg.reply("Error: " + err.message) | |
return | |
} | |
msg.reply(`Created a watcher for \`${tag}\` in \`${msg.guild.channels.get(channel).name}\`.`) | |
}) | |
} | |
// if found add a new watcher | |
else { | |
let guild = res[0] | |
guild.watchers.push({ tag, system, channel, importance }) | |
guild.save(err => { | |
if(err) { | |
console.log(err.message) | |
msg.reply("Error: " + err.message) | |
return | |
} | |
msg.reply(`Created a watcher for \`${tag}\` in \`${msg.guild.channels.get(channel).name}\`.`) | |
}) | |
} | |
}) | |
} | |
if(args[1] == 'delete') { | |
let tag = args[2] | |
if(!tag) { | |
msg.reply('Tag is required.') | |
return | |
} | |
Guild.find({ guildId: msg.guild.id }, (err, res) => { | |
if(err) { | |
console.log(err.message) | |
msg.reply("Error: " + err.message) | |
return | |
} | |
if(!res[0]) { | |
msg.reply("There are no watchers for this guild.") | |
} | |
else { | |
let guild = res[0] | |
let deletedAnything = false | |
for(;;) { | |
const i = guild.watchers.findIndex(el => { | |
return el.tag === tag | |
}) | |
if(i >= 0) { | |
guild.watchers.splice(i, 1) | |
deletedAnything = true | |
} | |
else break | |
} | |
if(!deletedAnything) { | |
msg.reply(`Tag \`${tag}\` not found.`) | |
return | |
} | |
guild.save(err => { | |
if(err) { | |
console.log(err) | |
msg.reply("Error: " + err.message) | |
return | |
} | |
msg.reply(`Deleted all watchers for \`${tag}\`.`) | |
}) | |
} | |
}) | |
} | |
if(args[1] == 'list') { | |
Guild.find({ guildId: msg.guild.id }, (err, res) => { | |
if(err) { | |
console.log(err.message) | |
msg.reply("Error: " + err.message) | |
return | |
} | |
if(!res[0]) { | |
msg.reply("There are no watchers for this guild.") | |
} | |
else { | |
let guild = res[0] | |
if(guild.watchers.length <= 0) { | |
msg.reply("There are no watchers for this guild.") | |
return | |
} | |
let watchers = guild.watchers.sort((a, b) => { | |
if(a.importance > b.importance) return -1 | |
else return 1 | |
}) | |
console.log(watchers) | |
const list = watchers.reduce((acc, el) => { | |
let importanceEmoji = getImportanceEmoji(el.importance) | |
if(!msg.guild.channels.get(el.channel)) msg.reply(`Channel with id ${el.channel} doesn't exist.`) | |
return acc += `${importanceEmoji} **Tag:** \`${el.tag}\` **System:** \`${el.system}\` **Channel:** \`${msg.guild.channels.get(el.channel).name}\`\n` | |
}, '\n') | |
msg.reply(list) | |
} | |
}) | |
} | |
} | |
}) | |
async function tick() { | |
status.lastCheck = new Date() | |
if(status.current) status.previous = status.current | |
status.current = await disco.players() | |
status.current.players = status.current.players.filter(el => { | |
if(el.system != "Connecticut") return true | |
}) | |
let difference | |
if(!status.previous) { | |
status.previous = status.current | |
difference = status.current.players | |
return | |
} | |
else { | |
const previousNames = status.previous.players.map(el => el.name) | |
difference = status.current.players.filter(el => { | |
if(!previousNames.includes(el.name)) return true | |
}) | |
console.log(difference) | |
} | |
Guild.find({}, (err, guilds) => { | |
if(err) { | |
console.log(err) | |
return | |
} | |
for(const guild of guilds) { | |
for(const watcher of guild.watchers) { | |
for(const player of difference) { | |
if(player.name.includes(watcher.tag)) { | |
if(player.system.includes(watcher.system) || watcher.system === '*') { | |
const channel = client.guilds.get(guild.guildId).channels.get(watcher.channel) | |
const importanceEmoji = getImportanceEmoji(watcher.importance) | |
const notifier = getNotifier(watcher.importance) | |
channel.send(`${importanceEmoji} ${notifier} **${player.name}** has entered **${player.system}**, online for \`${player.time}\` ` ) | |
} | |
} | |
} | |
} | |
} | |
}) | |
} | |
function getImportanceEmoji(importance) { | |
let importanceEmoji | |
if(importance == 2) importanceEmoji = ":grey_exclamation:" | |
else if(importance == 3) importanceEmoji = ":exclamation:" | |
else importanceEmoji = ':grey_question:' | |
return importanceEmoji | |
} | |
function getNotifier(importance) { | |
let notifier | |
if(importance == 2) notifier = "@here" | |
else if(importance == 3) notifier = "@everyone" | |
else notifier = "" | |
return notifier | |
} | |
client.login(process.env.TOKEN) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose') | |
const Schema = mongoose.Schema | |
const guildSchema = new Schema({ | |
guildId: String, | |
guildName: String, | |
guildOwner: String, | |
watchers: [ | |
{ | |
tag: String, | |
system: String, | |
channel: String, | |
importance: Number | |
} | |
] | |
}) | |
const Guild = mongoose.model('Guild', guildSchema) | |
module.exports = Guild |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment