From b56e272cdec6e58d8e8b7386de01d90af07330ac Mon Sep 17 00:00:00 2001 From: Remco Date: Sat, 15 Jul 2017 23:57:39 +0200 Subject: [PATCH] Version 0.0.3 (#60) * adding a pipe (#55) * adding a pipe * i think i got it? * Fix a Null like mistake, thanks to me misreading discord docs * Fix visual glitch * Remove extra info. * Fix timestamp, was done incorrectly first time through * Top 10 suggestions and version 0.0.3 because why not * i dont trust UV with dates * rip linebreaks again * Respond when regenerating is done * Whoops * Revert discordie requirement rip dev version, you will be missed :c * I suck at grammar * be gone 404 error logging --- Commands/Commands/queue.js | 38 ++++++++++++ Commands/Commands/top10.js | 107 +++++++++++++++++++++++++++++++++ Commands/Commands/uservoice.js | 6 +- Commands/Commands/utils.js | 2 +- index.js | 1 + package-lock.json | 6 +- package.json | 2 +- 7 files changed, 153 insertions(+), 9 deletions(-) create mode 100644 Commands/Commands/top10.js diff --git a/Commands/Commands/queue.js b/Commands/Commands/queue.js index 85030da..b3aa3f2 100644 --- a/Commands/Commands/queue.js +++ b/Commands/Commands/queue.js @@ -270,6 +270,44 @@ commands.registerVote = { return } switch (doc.type) { + case 'upvoteOnly' : { + if (reaction.id === '302138464986595339') { + getMail(uv, user.id).then(f => { + uv.v1.loginAs(f).then(c => { + c.post(`forums/${config.uservoice.forumId}/suggestions/${doc.UvId}/votes.json`, { + to: 1 + }).then((s) => { + if (user !== null) { + genlog.log(bot, user, { + message: 'Top10-voted', + affected: doc.UvId + }) + } + }).catch(e => { + if (e.statusCode !== 404) { + logger.log(bot, { + cause: 'top10_vote_apply', + message: (e.message !== undefined) ? e.message : JSON.stringify(e) + }, e) + } + }) + }).catch(e => { + logger.log(bot, { + cause: 'login_as', + message: (e.message !== undefined) ? e.message : JSON.stringify(e) + }, e).catch(e => { + if (e !== 'Not found') { + logger.log(bot, { + cause: 'email_search', + message: (e.message !== undefined) ? e.message : JSON.stringify(e) + }, e) + } + }) + }) + }) + } + break + } case 'chatVote': { if (reaction.id === '302137374920671233') { checker.getLevel(user.memberOf('268811439588900865'), function (l) { diff --git a/Commands/Commands/top10.js b/Commands/Commands/top10.js new file mode 100644 index 0000000..8f4a1cc --- /dev/null +++ b/Commands/Commands/top10.js @@ -0,0 +1,107 @@ +var Entities = require('html-entities').AllHtmlEntities +var config = require('../../config.js') +const entities = new Entities() +const Dash = require('rethinkdbdash') +const r = new Dash() + +var commands = [] + +commands.initializeTop = { + internal: true, + fn: function (bot, uv) { + setInterval(() => generateTop(bot, uv), 3600000) // update the top 10 hourly + } +} + +commands.regenerate = { + adminOnly: true, + modOnly: false, + fn: function (bot, msg, suffix, uv) { + msg.reply('regenerating the top 10, this could take a while...').then((msg) => { + generateTop(bot, uv).then(() => { + msg.edit('Done! You can check the results at <#268812972401360906>') + }) + }) + } +} + +function generateTop (bot, uv) { + return new Promise(function(resolve, reject) { + let channel = bot.Channels.find(c => c.name === 'top-10-suggestions') + let messages + channel.fetchMessages().then(msgs => { + messages = msgs.messages.filter(y => y.author.id === bot.User.id) + }) + uv.v1.loginAsOwner().then(f => { + f.get(`forums/${config.uservoice.forumId}/suggestions.json`).then(data => { + for (let suggestion of data.suggestions) { + generateEmbed(suggestion).then(embed => { + let message = messages.pop() + if (!message) { + channel.sendMessage('', false, embed).then(msg => { + r.db('DFB').table('queue').insert({ + id: msg.id, + type: 'upvoteOnly', + UvId: suggestion.id + }).then(() => { + msg.addReaction({ + id: '302138464986595339', + name: 'upvote' + }) + }) + }) + } else { + message.edit('', embed).then(msg => { + r.db('DFB').table('queue').get(msg.id).update({ + type: 'upvoteOnly', + UvId: suggestion.id + }) + }) + } + }) + } + return resolve() + }) + }) + }) +} + +function generateEmbed (data) { + return new Promise(function(resolve, reject) { + try { + let embedFields = (data.response) ? [{ + name: data.status ? `${entities.decode(data.status_changed_by.name)} set the status to ${entities.decode(data.status.name)}` : `${entities.decode(data.status_changed_by.name)} responded:`, + value: (data.response) ? entities.decode(data.response.text).length > 2000 ? '*Content too long, check the feedback website instead.*' : entities.decode(data.response.text) : 'No comment', + inline: false + }, { + name: "Votes", + value: data.vote_count, + inline: false + }] : [{ + name: "Votes", + value: data.vote_count, + inline: false + }] + return resolve({ + title: entities.decode(data.title), + description: entities.decode(data.text).length > 2000 ? '*Content too long, check the feedback website instead.*' : entities.decode(data.text), + url: data.url, + timestamp: new Date(data.created_at), + color: (data.status) ? parseInt(data.status.hex_color.substr(1), 16) : 0x595f68, + footer: { + text: (data.topic.forum.name) ? entities.decode(data.topic.forum.name) : 'Uncategorised' + }, + author: { + name: entities.decode(data.creator.name), + url: data.creator.url, + icon_url: data.creator.avatar_url + }, + fields: embedFields + }) + } catch (e) { + return reject(e) + } + }) +} + +exports.Commands = commands diff --git a/Commands/Commands/uservoice.js b/Commands/Commands/uservoice.js index 3258a02..386044e 100644 --- a/Commands/Commands/uservoice.js +++ b/Commands/Commands/uservoice.js @@ -261,7 +261,6 @@ commands.info = { }).then(response => { latestSuggLink = response.suggestions[0].url latestSuggTitle = response.suggestions[0].title - let moment = require('moment') return msg.channel.sendMessage(`Information for User ${data.user.name}:`, false, { color: 0xfc9822, title: `${data.user.name} - UserVoice`, @@ -292,9 +291,10 @@ commands.info = { } ], footer: { - text: 'User created on ' + moment(data.user.created_at).format("dddd, MMMM Do YYYY, k:mm:ss"), + text: 'User created on', icon_url: data.user.avatar_url - } + }, + timestamp: new Date(data.user.created_at) }) }).catch(() => {}) // Make bugsnag ignore it, error handling below }).catch(() => { diff --git a/Commands/Commands/utils.js b/Commands/Commands/utils.js index ee0d3d7..90ce92c 100644 --- a/Commands/Commands/utils.js +++ b/Commands/Commands/utils.js @@ -158,4 +158,4 @@ commands.shutdown = { } } -exports.Commands = commands +exports.Commands = commands \ No newline at end of file diff --git a/index.js b/index.js index a53a835..c567144 100644 --- a/index.js +++ b/index.js @@ -121,6 +121,7 @@ bot.Dispatcher.on(Events.GATEWAY_READY, () => { bot.Users.fetchMembers() // Hacky way to cache offline users, #blamelazyloading }, 600000) console.log('Feedback bot is ready!') + Commands['initializeTop'].fn(bot, uvClient) }) process.on('unhandledRejection', (reason, p) => { diff --git a/package-lock.json b/package-lock.json index 5fdecc4..4910c54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "discord-feedback", - "version": "0.0.1", + "version": "0.0.2", "lockfileVersion": 1, "dependencies": { "acorn": { @@ -249,9 +249,7 @@ "integrity": "sha1-3zrhmayt+31ECqrgsp4icrJOxhk=" }, "discordie": { - "version": "0.10.0", - "resolved": "https://registry.npmjs.org/discordie/-/discordie-0.10.0.tgz", - "integrity": "sha1-yapYfXzqRBueE9Dxj/a46+VSBBA=" + "version": "github:qeled/discordie#da57ebff217be7a9254fc77a615c7d72b3ba76e0" }, "doctrine": { "version": "https://registry.npmjs.org/doctrine/-/doctrine-1.5.0.tgz", diff --git a/package.json b/package.json index 4ddf373..c59cf9a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-feedback", - "version": "0.0.2", + "version": "0.0.3", "description": "Bot for the Discord Feedback server", "main": "index.js", "scripts": {