summaryrefslogtreecommitdiff
path: root/util.js
blob: f2e894b14589128220de56c798918988d7fc05a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/node
/* jshint esnext:true */
"use strict";

const Discord = require("discord.js"),
    ua = require("universal-analytics"),
    Promise = require("bluebird"),
    Channel = require("async-csp").Channel;

const GOOGLEANALYTICS_ID = process.env.GOOGLEANALYTICS_ID,
    REACTION_TIMEOUT = parseInt(process.env.REACTION_TIMEOUT) || 60,  // s
    PREVIEW = process.env.PREVIEW != "false",
    ROOTURL = (PREVIEW? "https://preview.vainsocial.com/":"https://vainsocial.com/");

const reactionsPipe = new Channel();

// embed template
module.exports.vainsocialEmbed = (title, link, command) => {
    return new Discord.RichEmbed()
        .setTitle(title)
        .setColor("#55ADD3")
        .setURL(ROOTURL + link + module.exports.track(command))
        .setAuthor("VainSocial" + (PREVIEW? " preview":""), ROOTURL + "images/brands/logo-blue.png",
            ROOTURL + module.exports.track(command))
        .setFooter("VainSocial" + (PREVIEW? " preview":""), ROOTURL + "images/brands/logo-blue.png")
};

// analytics url
module.exports.track = (command) => {
    return "?utm_source=discordbot&utm_medium=discord&utm_campaign=" + command;
};

// direct analytics
module.exports.trackAction = (msg, action, ign="") => {
    if (GOOGLEANALYTICS_ID == undefined) return;
    const user = ua(GOOGLEANALYTICS_ID, msg.author.id,
        { strictCidFormat: false });
    user.pageview({
        documentPath: action,
        documentTitle: ign,
        campaignSource: msg.guild.id,
        campaignMedium: msg.guild.name
    }).send();
};

// return the shortest version of the usage help
// just '?vm'
module.exports.usg = (msg, cmd) => {
    return msg.anyUsage(cmd, undefined, null);
};

// reaction -> pipe ->>> consumers
// handle new reaction event
module.exports.onNewReaction = (reaction) => {
    if (reaction.users.array().length <= 1) return;  // was me TODO
    reactionsPipe.put(reaction);
};

// create an iterator that returns promises to await new reactions
// the Promise result is the reaction name
module.exports.awaitReactions = (message, emoji, timeout=REACTION_TIMEOUT) => {
    const pipeOut = new Channel();
    let reactions = [];
    // stop listening after timeout
    setTimeout(() => {
        pipeOut.close();
        Promise.map(reactions, async (r) => r.remove());
    }, timeout*1000);

    // async in background
    Promise.each(emoji, async (em) =>
        reactions.push(await message.react(em)));

    let reaction;
    reactionsPipe.pipe(pipeOut);
    return {
        next: async function() {
            do {
                reaction = await pipeOut.take();
                if (reaction == Channel.DONE)
                    return undefined;
            } while (reaction.message.id != message.id ||
                emoji.indexOf(reaction.emoji.name) == -1);
            return reaction.emoji.name;
        }
    }
};

// respond or say text or embed
module.exports.respond = async (msg, data, response) => {
    if (response == undefined) {
        if (typeof data === "string") {
            response = await msg.say(data);
        } else {
            response = await msg.embed(data);
        }
    } else {
        if (typeof data === "string") {
            if (data != response.content)
                await response.edit(data);
        } else {
            if (new Date(response.embeds[0].createdTimestamp).getTime()
                != data.timestamp.getTime())
                // TODO how2 edit embed properly?!
                await msg.editResponse(response, {
                    type: "plain",
                    content: "",
                    options: { embed: data }
                });
        }
    }
    return response;
};

// tell the user that they need to store their name
module.exports.formatSorryUnknown = (msg) => {
    return `You're unknown to our service. Try ${util.usg(msg, "help vme")}.`;
};