-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
177 lines (165 loc) · 5.83 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
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
// #region imports
import Discord, { GatewayIntentBits } from 'discord.js';
import utils from './util/utils';
import constants from './util/constants';
import data from './util/data';
import commands from './util/commands';
import { token, prefix } from './config';
import Boss from './util/bosses';
// #endregion
// #region constants
// #endregion
// Discord client
const client = new Discord.Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessageReactions],
});
client.commands = commands;
client.once('ready', () => {
if (
data.getTargetNumber() === undefined
|| data.getTargetNumber() === null
|| Number.isNaN(data.getTargetNumber())
) {
data.setTargetNumber(utils.getRandomInt(0, constants.WIN));
}
Boss.load();
console.log('Logged in.');
client.user.setActivity(`${prefix}help`, { type: Discord.ActivityType.Listening });
});
const bind = async (messageObj, callback, errorCb) => {
const tokens = utils.tokenize(messageObj.content);
if (messageObj.member.permissions.has('MANAGE_GUILD') && tokens.length > 1) {
const channelId = tokens[1].trim().replace(/\D/g, '');
await client.channels
.fetch(channelId)
.catch((e) => {
if (errorCb) {
errorCb(e);
} else {
console.error(e);
}
})
.then(() => {
data.setChannelId(channelId);
});
if (callback) callback();
} else if (errorCb) errorCb();
};
client.on('messageCreate', async (message) => {
try {
const { author } = message;
if (author.bot) return; // message from bot
if (!message.guild) return; // DM
const userId = author.id;
if (!data.hasUser(userId)) {
data.createUser(userId);
}
const tokens = utils.tokenize(message.content);
if (message.content.toLowerCase() === 'blaze it') {
data.selectReaction(userId, 'blazeit');
}
// #region command
// explicity check for bind first
if (message.content.startsWith(prefix)) {
const command = tokens[0].substr(prefix.length);
if (command === 'bind') {
await bind(message);
}
if (!data.getChannelId()) {
// unbound
message.channel.send(
{ content: `Bot must be bound to a channel with \`${prefix}bind #<channel-name>\`.` },
);
return;
}
// wrong channel, allows bind first though
if (message.channel.id !== data.getChannelId()) return;
// heavy-lifting for commands
client.commands.get(command)?.execute(message);
}
// #endregion
const number = parseInt(tokens[0], 10);
if (data.getChannelId() === message.channel.id && !Number.isNaN(number)) {
if (data.getLastUserId() === userId) {
message.react('⏳');
data.incrementMiscount(userId);
data.removeCoins(userId, constants.COIN_LOSS);
return;
}
if (Math.abs(number - data.getCurrentNumber()) === 1) {
// increment user count
data.incrementCount(userId);
// check if win
data.setCurrentNumber(number);
if (Math.abs(number) === data.getTargetNumber()) {
data.incrementWins(userId);
data.addCrowns(userId, constants.CROWN_MULTIPLIER * (1 + data.getRoyalty(userId)));
data.setTargetNumber(utils.getRandomInt(0, constants.WIN));
message.react(data.getReaction(userId));
message.channel.send(
{ content: `👑 Congrats ${author}! New target: ±${data.getTargetNumber()}.` },
);
data.clearLastUserId();
} else {
let hasReacted = false;
if (Math.random() <= constants.ACROBATICS_RATE
* (data.getAcrobatics(userId) ?? 0)) {
hasReacted = true;
message.react(constants.ACROBATICS_EMOJI);
data.clearLastUserId();
} else if (Math.abs(Math.abs(number) - data.getTargetNumber()) > 1) {
data.setLastUserId(userId);
} else {
data.clearLastUserId();
}
if (Boss.instance) {
const bossName = `${Boss.instance.bossName}`;
const isBossDead = Boss.instance.hit(message.author.id, () => {
hasReacted = true;
message.react('💓'); // crit
});
if (isBossDead) {
message.channel.send({ content: `${bossName} was calmed down by ${message.author}! Paying rewards to everyone who helped...` });
const user = data.getUser(userId);
user.boss += 1;
} else if (Boss.instance.health % Boss.HEALTH_MULTIPLIER === 0) {
message.channel.send({ embeds: [Boss.instance.embed] });
}
} else if (Math.random() < constants.BOSS_SPAWN_RATE) {
Boss.instantiate();
message.channel.send({ embeds: [Boss.instance.embed] });
}
if (Math.random() <= constants.COIN_RATE) {
const gain = constants.COIN_GAIN * utils.getRandomInt(2, 10);
data.addCoins(userId, gain);
message.react('💰');
hasReacted = true;
}
if (!hasReacted) {
if (Math.abs(data.getCurrentNumber()) === 69) {
message.react('😎');
} else if (Math.abs(data.getCurrentNumber()) === 100) {
message.react('💯');
} else {
message.react(constants.REACT_CORRECT);
}
}
}
} else {
data.setLastUserId(userId);
data.incrementMiscount(userId);
data.removeCoins(userId, constants.COIN_LOSS);
message.react(constants.REACT_INCORRECT);
}
}
} catch (err) {
console.error(err);
}
});
// ensures data write when server killed
process.on('SIGINT', () => {
data.persistBoss(Boss.instance);
data.persistData();
process.exit(0);
});
client.login(token);