forked from qmk/qmk_helper_bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqmkbot.js
211 lines (172 loc) · 5.76 KB
/
qmkbot.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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Initialize Discord Bot
const Discord = require('discord.js');
const bot = new Discord.Client();
// Include config
require('dotenv').config();
const token = process.env.TOKEN;
// Import utils.js
const {prefix, baseurl, msg, docsSwitch, authroles, parse, bare, firmware, toolbox, plainhelp, disclaimer, ohshitgit, git, xkcd, promicro, protonc, elitec, msys, coc, kbdfans, vid, vidq} = require("./utils.js");
let cooldown = require("./utils.js").cooldown;
bot.on('ready', () => {
// Reset cooldown every 30 seconds
setInterval(() => {
cooldown = [];
}, 30000);
});
//const sender = { message, author };
bot.on('message', message => {
// Destructure discord.js elements
const { content, author, channel, member, id} = message;
// Bot ignores itself while it talks
if(author.bot) return;
// Sanitize hyphens
input = content.split('-').join('');
// Input starts with '!'
if (input.startsWith(prefix)) {
// Parse commands
var args = parse(input);
var cmd = args[0];
args.forEach(function(item) {
//doc = docsSwitch(item);
//doc = docsSwitch(item).str.replace(/-/g, "");
doc = docsSwitch(item);
if (typeof doc !== 'undefined') {
// construct docSwitch embed
const docmsg = {
color: 0x344703,
fields: [
{
name: 'Try this:',
value: `${baseurl}${doc}`,
},
],
footer: {
icon_url: `https://cdn.discordapp.com/avatars/${author.id}/${author.avatar}.png?size=256`,
text: `${message.author.username} invoked !${doc}`,
}
};
if(channel.type === "dm") {
//author.send(`${baseurl}${doc}`);
author.send({ embed: docmsg });
return;
};
// commented out -- doc commands are being opened up to all members
/*
if ((member.roles.some(r=>authroles.includes(r.name))) && !cooldown.includes(doc)) {
channel.send(bare(`${baseurl}${doc}`));
cooldown.push(doc);
} else {
author.send(`${baseurl}${doc}`);
}
*/
// enforce 30 second cooldown on docs sent to channel
if (!cooldown.includes(doc)) {
//channel.send(bare(`${baseurl}${doc}`));
channel.send({ embed: docmsg });
cooldown.push(doc);
}
}
});
/*
// construct embed for links outside of docSwitch
const cmdmsg = {
color: 0x344703,
footer: {
icon_url: `https://cdn.discordapp.com/avatars/${author.id}/${author.avatar}.png?size=256`,
text: `${message.author.username} invoked !${cmd}`,
}
};
*/
const cmdmsg = new Discord.MessageEmbed()
.setColor(0x344703)
.setFooter(`${message.author.username} invoked !${cmd}`, `https://cdn.discordapp.com/avatars/${author.id}/${author.avatar}.png?size=256`);
// switch statement here for help menu and links outside of docSwitch
switch (cmd) {
// PM plaintext help menu
case 'help':
//author.send(plainhelp);
author.send(msg);
//author.send(disclaimer);
break;
//case 'plain': // plaintext fallback
//author.send(plainhelp);
//break;
case 'potato': // PM a potato
author.send(':potato:');
break;
case 'ohshit': // send channel a link to https://ohshitgit.com/
//channel.send(bare(ohshitgit));
cmdmsg.addFields({ name: "Try this:", value: ohshitgit });
channel.send(cmdmsg);
break;
case 'git': // send channel a link to QMK git best practices and xkcd #1597
//channel.send(xkcd);
//channel.send(bare(git));
cmdmsg.setImage(xkcd);
cmdmsg.addFields({ name: "Try this:", value: git });
channel.send(cmdmsg);
break;
case 'conduct': // send channel a link to https://qmk.fm/coc/
//channel.send(coc);
cmdmsg.addFields({ name: "Review QMK Code of Conduct:", value: coc });
channel.send(cmdmsg);
break;
case 'kbdfans':
//channel.send(kbdfans);
cmdmsg.addFields({ name: "Dear KBDFans users:", value: kbdfans });
channel.send(cmdmsg);
break;
/*
case 'promicro': // send channel image of pro micro pinout
channel.send({files:[promicro]});
break;
*/
case 'promicro': // send channel image of pro micro pinout
cmdmsg.setTitle("Pro Micro Pinout");
cmdmsg.setImage(promicro);
channel.send(cmdmsg);
break;
case 'protonc': // send channel image of proton c pinout
cmdmsg.setTitle("Proton-C Pinout");
cmdmsg.setImage(protonc);
channel.send(cmdmsg);
//channel.send({files:[protonc]});
break;
case 'elitec': // send channel image of elite c pinout
cmdmsg.setTitle("Elite-C Pinout");
cmdmsg.setImage(elitec);
channel.send(cmdmsg);
//channel.send({files:[elitec]});
break;
case 'toolbox': // send channel link to qmk_toolbox repo
//channel.send(bare(toolbox));
cmdmsg.addFields({ name: "Get QMK Toolbox here:", value: toolbox });
channel.send(cmdmsg);
break;
case 'qmkfirmware': // send channel link to qmk_firmware repo
//channel.send(bare(firmware));
cmdmsg.addFields({ name: "QMK Firmware repository:", value: firmware });
channel.send(cmdmsg);
break;
case 'msys': // send channel link to msys page
//channel.send(bare(msys));
cmdmsg.addFields({ name: "Get QMK MSYS here:", value: msys });
channel.send(cmdmsg);
break;
case 'vid': // send channel link to USB usage page
//channel.send(bare(vid));
cmdmsg.addFields({ name: "View USB usage page here", value: vid });
channel.send(cmdmsg);
break;
case 'vidq': // send channel link to VID/PID query
//channel.send(bare(vidq));
cmdmsg.addFields({ name: "Query VID/PID usage here:", value: vidq });
channel.send(cmdmsg);
break;
}
// Delete message
setTimeout(() => message.delete().catch(err => console.log(err)), 100); // testing
}
});
// Authenticate bot
bot.login(token);