-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (113 loc) · 3.04 KB
/
index.js
File metadata and controls
124 lines (113 loc) · 3.04 KB
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
require('colors');
const Discord = require('discord.js');
const fs = require('fs');
const util = require('./util.js');
const config = require('./config.json');
const text = require('./commands/text.json');
const { token } = require('./token.json');
const client = new Discord.Client();
client.commands = new Discord.Collection();
const commandFiles = fs
.readdirSync('./commands')
.filter((f) => f.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
const defaultThemeColor = Discord.Constants.Colors.ORANGE;
/**
* Return a embed message (default color orange #e67e22).
*/
function embed() {
return new Discord.MessageEmbed().setColor(defaultThemeColor);
}
// Login
client.once('ready', () => {
util.log('(init)'.grey);
});
client.login(token);
util.log('Login successful');
// Listen
client.on('message', (msg) => {
// Ignore non-command messages
if (!config.prefix.includes(msg.content.charAt(0)) && !msg.author.bot) return;
const msgServerName = msg.guild.name;
const msgUserName = msg.member.user.tag;
let msgContent;
// User tag in message (mention)
if (msg.content.match(/<@[!]?\d+>/)) {
const user = msg.mentions.users.first();
msgContent = `<@${user.username}#${user.discriminator}>`;
}
// Normal message
else {
msgContent = msg.content;
}
const msgLog = `(${msgServerName})<@${msgUserName}>: ${msgContent}`;
// Bot messages in color blue
util.log(msg.author.bot ? msgLog.blue : msgLog);
// No need to execute if it is a bot message
if (msg.author.bot) return;
// ?????
config.prefix.forEach((e) => {
if (msg.content.match(new RegExp(`^[${e}]+$`, 'g'))) {
util.log(`<${msgUserName}> feels confused...`);
return;
}
});
const args = msg.content
.replace(/\s+/g, ' ')
.trim()
.slice(1)
.trim()
.split(' ');
const cmd = args.shift().toLowerCase();
const channel = msg.channel;
let reply;
switch (cmd) {
case 'a':
case 'about':
reply = client.commands.get('about').execute();
break;
case 'h':
case 'help':
reply = client.commands.get('help').execute();
break;
case 'p':
case 'play':
// TODO: implement
// reply = client.commands.get('play').execute();
break;
case 'r':
case 'roll': {
const roll = client.commands.get('roll').execute(args);
if (!roll) return;
// Result as title
// Range as description
reply = embed().setTitle(roll[1]).setDescription(`from\n[${roll[0]}]`);
break;
}
case 'shit':
case 'cesuo':
case 'cheshuo':
reply = text.cheshuo;
break;
default:
util.err('Command undefined');
}
if (!reply) return;
channel.send(reply);
});
// Logout
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on('SIGINT', () => {
rl.question('Shut down the bot? (y/n): ', (answer) => {
if (answer.match(/^y(es)?$/i)) {
client.destroy();
rl.pause();
}
});
});