-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (86 loc) · 3.09 KB
/
index.js
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
require("dotenv").config();
const Discord = require("discord.js");
const client = new Discord.Client();
const { promisify } = require("util");
const readdir = promisify(require("fs").readdir);
const firestoreutils = require("./firestoreutils");
client.commands = new Map();
const TOKEN = process.env.TOKEN;
const PREFIX = process.env.PREFIX;
console.info("Using prefix: " + PREFIX);
// FIRESTORE SETUP
const admin = require("firebase-admin");
// const serviceAccount = require("./../beerbot-295821-f6a45f63797f.json");
const parsedCredentials = JSON.parse(process.env.GOOGLE_CREDENTIALS);
console.info("Connecting to firebase. . . .");
admin.initializeApp({
credential: admin.credential.cert(parsedCredentials),
});
const db = admin.firestore();
// END FIRESTORE SETUP
console.info("Logging into discord with token. . .")
client.login(TOKEN);
client.on("ready", () => {
console.info(`Logged in as ${client.user.tag}!`);
// register all our chat commands from the /commands folder
readdir("./commands/", (error, files) => {
if (error) throw error;
files.forEach((file) => {
if (!file.endsWith(".js")) return; // make sure the file is what you are looking for
try {
const properties = require(`./commands/${file}`);
client.commands.set(properties.help.name, properties);
} catch (err) {
throw err;
}
});
});
});
client.on("message", (msg) => {
if (!msg.guild) return; // exit if the message does not have a guild
if ((msg.author.username === "Beerform")) {
try {
firestoreutils.setBeerRatingFromForm(msg, db);
} catch (e) {
// uh oh - most likely this was a problem parsing the response
msg.channel.send("Uh Oh - something went wrong. . .")
}
}
if (msg.author.bot) return; // exit if the message author is a bot
var userMention = msg.mentions.members.first();
// remove the prefix and map each arg to an array
const args = msg.content
.slice(PREFIX.length)
.trim()
.split(/ +/g)
.map(Function.prototype.call, String.prototype.trim);
const command = args.shift().toLowerCase();
const cmd = client.commands.get(command);
if (!cmd) {
// the message is not a command we know of
// check for user mention functionality
if (userMention) {
// for some reason sometimes when beerbot is mentioned the username that comes through is "Beerform"
// this is bandage fix for now - not sure why this is happening
var isBotMention =
userMention.user.username === "beerbot" ||
userMention.user.username === "Beerform";
if (isBotMention && msg.content.toLowerCase().includes("how do i make")) {
const howdoimakecmd = client.commands.get("howdoimake");
howdoimakecmd.run(
client,
msg,
args.slice(args.indexOf("make") + 1),
db
);
} else if (args[0] === "🍻") {
// there's at least one mentioned user and a cheers emoji
const cheercmd = client.commands.get("cheer");
cheercmd.run(client, msg, args, db);
}
}
} else {
// run the command
cmd.run(client, msg, args, db);
}
});