-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
193 lines (164 loc) · 5.76 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
const fs = require('node:fs');
const path = require('node:path');
const { Client, Collection, GatewayIntentBits, Partials, ActivityType, Events } = require('discord.js');
const {ActionRowBuilder, ButtonBuilder, ButtonStyle } = require('discord.js')
require("dotenv").config();
const rs = require("./roll_seed.js");
const { metadata } = require("./metadata.js");
// Initialize the bot
const bot = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.DirectMessages],
partials: [Partials.Channel]
});
// Attach the commands
bot.commands = new Collection();
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
console.log(`Loaded implementation for command: ${file}`)
const filePath = path.join(commandsPath, file);
const command = require(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ('data' in command && 'execute' in command) {
bot.commands.set(command.data.name, command);
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
// Message for when we boot up
bot.on(Events.ClientReady, () => {
bot.user.setActivity({name: "DM me for a seed!", game: "DM me!", type: ActivityType.Playing});
console.log("RSLBot is online!");
});
bot.on(Events.MessageCreate, msg => {
if(msg.channel.type === 1) {
parseDM(msg);
}
});
function parseDM(msg) {
if(msg.author.bot) { return; }
const userinfo = parse_user_info(msg, "MESSAGE");
// Check for a bot restart command
if (msg.content.startsWith("!reset")) {
if (userinfo.user_level === "admin") {
msg.author.send({ content: "Restarting..." });
process.exit();
}
else {
msg.author.send({ content: "Permission denied." });
return;
}
}
// Return the user's status
if (msg.content.startsWith("!status")) {
msg.author.send({ content: `You have permissions level ${userinfo.user_level}.` });
return;
}
// Parse generic message and send button options
const row = new ActionRowBuilder()
.addComponents(
rsl_seasonal_button,
rsl_lite_button,
new ButtonBuilder()
.setCustomId('view_presets')
.setLabel('View Presets')
.setStyle(ButtonStyle.Secondary)
)
msg.author.send({
content: "Click a button below to see what I can do!",
components: [row]
});
}
// Parse buttons
bot.on(Events.InteractionCreate, interaction => {
if (interaction.isButton()) {
handleButtonInteraction(interaction);
} else {
const command = interaction.client.commands.get(interaction.commandName);
if (!command) {
console.error(`No command matching ${interaction.commandName} was found.`);
return
}
command.execute(interaction);
}
});
function handleButtonInteraction(interaction) {
const ctime = new Date(Date.now());
const cid = interaction.customId;
const userinfo = parse_user_info(interaction, "INTERACTION");
if(cid.startsWith("roll_")) {
rs.rollSeed(interaction, userinfo, ctime);
}
else if(cid === "unlock_log") {
rs.unlockSeed(interaction);
}
else if(cid === "view_presets") {
interaction.update({
content: "Roll a seed with one of the following presets:",
components:[presetrow]
})
}
}
function set_user_level(username) {
if (metadata.admin.includes(username)) {
return "admin";
}
else if (metadata.moderator.includes(username)) {
return "moderator";
}
else if (metadata.organizer.includes(username)) {
return "organizer";
}
else {
return "user";
}
}
function parse_user_info(event, event_type) {
let user_object = null;
if (event_type === "INTERACTION") {
user_object = event.user;
}
else if (event_type === "MESSAGE") {
user_object = event.author;
}
else {
console.log(`While parsing user info, event type ${event_type} not recognized.`)
return null;
}
const username = `${user_object.username}#${user_object.discriminator}`;
const user_level = set_user_level(username);
return { username: username, user_level: user_level }
}
const rsl_seasonal_button = new ButtonBuilder()
.setCustomId(`roll_Season${metadata.season}`)
.setLabel(`Roll an S${metadata.season} RSL Seed (v${metadata.rslVersion})`)
.setStyle(ButtonStyle.Primary);
const rsl_lite_button = new ButtonBuilder()
.setCustomId('roll_Lite')
.setLabel("Roll an RSL Lite Seed")
.setStyle(ButtonStyle.Success)
const presetrow = new ActionRowBuilder()
.addComponents(
rsl_seasonal_button,
new ButtonBuilder()
.setCustomId('roll_Lite')
.setLabel('Roll an RSL Lite Seed')
.setStyle(ButtonStyle.Success),
new ButtonBuilder()
.setCustomId('roll_Intermediate')
.setLabel('Roll an Intermediate RSL Seed')
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId('roll_Bingo')
.setLabel('Roll a Bingo RSL Seed')
.setStyle(ButtonStyle.Secondary),
new ButtonBuilder()
.setCustomId('roll_DDR')
.setLabel('Roll a DDR RSL Seed')
.setStyle(ButtonStyle.Secondary)
)
bot.login(process.env.CLIENT_TOKEN);