Created
October 6, 2017 02:11
-
-
Save nicholastay/113c455220aa645fd45a34e13f97cde9 to your computer and use it in GitHub Desktop.
Old POC for when reactions first came out on discord to use them as a question sort of thing
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
'use strict'; | |
const Eris = require('eris') | |
, fs = require('fs') | |
, repl = require('repl'); | |
let client = new Eris(fs.readFileSync('./key.txt', 'utf8')); | |
client.on('ready', () => console.log('connected')); | |
// Reaction awaiting struct | |
let waitForReactions = {}; // obj[questionMessage.id] = reactionHandler; | |
class ReactionYesNo { | |
constructor(questionMessage, user, timeout, cb) { | |
this.message = questionMessage; | |
this.user = user; | |
this.cb = cb; | |
this.timeout = setTimeout(() => { | |
this.message.delete(); // ran out of time | |
}, timeout); | |
// attach the tick/cross | |
setTimeout(() => { | |
questionMessage.addReaction(encodeURI('✅')) | |
.then(() => questionMessage.addReaction(encodeURI('❌'))) | |
.catch(console.log); | |
}, 750); // just wait a sec | |
} | |
handle(emoji, userID) { | |
if (userID !== this.user.id) | |
return; // wrong user reacting | |
if (emoji.id !== null) | |
return; // must be the stock emoji | |
if (emoji.name === '✅') | |
return this.approve(); | |
if (emoji.name === '❌') | |
return this.reject(); | |
} | |
clean() { | |
clearTimeout(this.timeout); | |
delete waitForReactions[this.message.id]; | |
} | |
approve() { | |
console.log(this.message.id + ' approved'); | |
this.clean(); | |
this.message.delete() | |
.then(this.cb); | |
} | |
reject() { | |
console.log(this.message.id + ' rejected'); | |
this.clean(); | |
this.message.delete(); | |
} | |
} | |
client.on('messageCreate', message => { | |
if (message.content !== '!reactiontest') | |
return; | |
let testMsgToCleanup; | |
message.channel.createMessage('Kappa') | |
.then(m => testMsgToCleanup = m) | |
.then(() => message.channel.createMessage('Are you sure you wish to clean up the above message? *(Click the options below to approve or reject.)*')) | |
.then(m => waitForReactions[m.id] = new ReactionYesNo(m, message.author, 30000, () => testMsgToCleanup.delete())); | |
}); | |
client.on('messageReactionAdd', (message, emoji, userID) => { | |
if (userID === client.user.id || !waitForReactions[message.id]) | |
return; | |
console.log('handling ' + message.id) | |
waitForReactions[message.id].handle(emoji, userID); | |
}); | |
client.connect(); | |
let replS = repl.start({ prompt: 'r> '}); | |
replS.context.client = client; | |
replS.context.waitForReactions = waitForReactions; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment