-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
231 lines (214 loc) · 7.78 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
const { Client, GatewayIntentBits, EmbedBuilder } = require("discord.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const Database = require("simple-json-db");
const config = require("./config");
const db = new Database("./data.json");
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
]
});
const commands = [
{
name: "give",
description: "Give a Nitro to a user",
options: [
{
name: "user",
description: "The user to send the Nitros to",
type: 6,
required: true,
},
{
name: "quantity",
description: "The number of Nitros to give",
type: 4,
required: true,
},
{
name: "item",
description: "The item to give for",
choices: [
{ name: "Nitro Basic", value: "NitroBasic" },
{ name: "Nitro Boost", value: "NitroBoost" },
],
type: 3,
required: true,
},
],
},
{
name: "stocks",
description:
"Shows stock of Nitro Basic and Nitro Boost",
},
{
name: "help",
description:
"Displays bot commands",
},
{
name: "reset",
description:
"Reset the db of Nitro Basic or Nitro Boost",
options: [
{
name: "item",
description: "The item of Nitros to reset",
type: 3,
required: true,
choices: [
{ name: "Nitro Basic", value: "NitroBasic" },
{ name: "Nitro Boost", value: "NitroBoost" },
],
},
],
},
{
name: "restock",
description:
"Restock Nitro Basic or Nitro Boost",
options: [
{
name: "item",
description: "The item of Nitros to restock",
type: 3,
required: true,
choices: [
{ name: "Nitro Basic", value: "NitroBasic" },
{ name: "Nitro Boost", value: "NitroBoost" },
],
},
{
name: "link",
description: "The link to restock",
type: 3,
required: true,
},
],
},
];
const rest = new REST({ version: "10" }).setToken(config.token);
(async () => {
try {
// console.log("Started refreshing application (/) commands.");
await rest.put(Routes.applicationGuildCommands(config.clientId, config.guildId), {
body: commands,
});
// console.log("Successfully reloaded application (/) commands.");
} catch (e) {
console.error(`Please specify the bot id in the config.clientId and specify a server in config.guildId otherwise the bot will not work : ${e}`);
}
})();
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag} (${client.user.id})`);
});
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName, options, user } = interaction;
const userId = user.id;
if (!config.allowedUserIds.includes(userId)) {
const embed = new EmbedBuilder()
.setDescription(`\`🕷️\`〃*You are not authorized to use this command !*`)
.setColor(config.color);
return interaction.reply({ embeds: [embed], ephemeral: true });
}
if (commandName === "give") {
const user = options.getUser("user");
const quantity = options.getInteger("quantity");
const item = options.getString("item");
if (user.bot) {
const embed = new EmbedBuilder()
.setDescription(`\`❌\`〃*Bots cannot receive Nitro!*`)
.setColor(config.color);
return interaction.reply({ embeds: [embed], ephemeral: true });
}
if (!db.has(item)) db.set(item, []);
const itemArray = db.get(item);
if (itemArray.length < quantity) {
const embed = new EmbedBuilder()
.setDescription(`\`❌\`〃*Not enough ${item} to distribute!*`)
.setColor(config.color);
return interaction.reply({ embeds: [embed], ephemeral: true });
}
const codesToSend = itemArray.slice(0, quantity);
try {
const codeList = codesToSend
.map((code, index) => `**\`${index + 1}\`** - || ${code} ||`)
.join("\n");
const embed = new EmbedBuilder()
.setDescription(codeList)
.setColor(config.color);
await user.send({ embeds: [embed] });
const remainingCodes = itemArray.slice(quantity);
db.set(item, remainingCodes);
const embed2 = new EmbedBuilder()
.setDescription(`\`✅\`〃*${quantity} ${item} code(s) distributed to ${user} (\`${user.id}\`)!*`)
.setColor(config.color);
await interaction.reply({ embeds: [embed2] });
} catch (e) {
console.error(`❌〃Failed to send ${quantity} ${item} code(s) to ${user.tag} (\`${user.id}\`) : ${e}`);
const embed = new EmbedBuilder()
.setDescription(`\`❌\`〃*Failed to send ${quantity} ${item} code(s) to ${user} (\`${user.id}\`)!*`)
.setColor(config.color);
await interaction.reply({ embeds: [embed] });
return;
}
} else if (commandName === "stocks") {
const NitroBasic = db.get("NitroBasic") || [];
const NitroBoost = db.get("NitroBoost") || [];
const embed = new EmbedBuilder()
.setTitle("\`🕷️\`〃Stocks de Nitro")
.setDescription(`> *Nitro Basic :* \`${NitroBasic.length}\`\n> *Nitro Boost :* \`${NitroBoost.length}\``)
.setColor(config.color);
await interaction.reply({ embeds: [embed] });
}else if (commandName === "help") {
const embed = new EmbedBuilder()
.setTitle("\`🕷️\`〃Help")
.setDescription(`\`/help\`\n*-Displays bot commands*\n\`/stocks\`\n*-Shows stock of Nitro Basic and Nitro Boost*\n\`/restock\`\n*-Restock Nitro Basic or Nitro Boost*\n\`/give\`\n*-Give a Nitro to a user*\n\`/reset\`\n*-Reset the db of Nitro Basic or Nitro Boost*\n`)
.setColor(config.color);
await interaction.reply({ embeds: [embed] });
} else if (commandName === "reset") {
const item = options.getString("item");
if (item === "NitroBasic" || item === "NitroBoost") {
db.set(item, []);
const embed = new EmbedBuilder()
.setTitle("\`✅\`〃Successful Reset")
.setDescription(`> *Database for \`${item}\` has been reset.*`)
.setColor(config.color);
await interaction.reply({ embeds: [embed] });
} else {
const embed = new EmbedBuilder()
.setTitle("\`❌\`〃Reset error")
.setDescription("> *Invalid reset item. Use `NitroBasic` or `NitroBoost`.*")
.setColor(config.color);
await interaction.reply({ embeds: [embed] });
}
}
else if (commandName === "restock") {
const type = options.getString("item");
const link = options.getString("link").split(/\s+/);
let embed;
if (type === "NitroBasic" || type === "NitroBoost") {
const NitroBasic = db.get("NitroBasic") || [];
const NitroBoost = db.get("NitroBoost") || [];
const existingLink = db.get(type) || [];
const updatedLink = [...existingLink, ...link];
db.set(type, updatedLink);
const totalLinks = type === "NitroBasic" ? NitroBasic.length : NitroBoost.length;
embed = new EmbedBuilder()
.setTitle("\`✅\`〃Successful restock")
.setDescription(`> *Restocked \`${type}\` with \`${totalLinks + 1}\` links now.*`)
.setColor(config.color);
} else {
embed = new EmbedBuilder()
.setTitle("\`❌\`〃Restock error")
.setDescription("> *Invalid restock type. Use `NitroBasic` or `NitroBoost`.*")
.setColor(config.color);
}
await interaction.reply({ embeds: [embed] });
}
});
client.login(config.token);