-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexDev.ts
259 lines (204 loc) · 7.47 KB
/
indexDev.ts
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { Collection, Client, GatewayIntentBits, Guild, Interaction, Routes } from 'discord.js';
import { REST } from '@discordjs/rest';
export interface ServerSettings {
language: string;
}
export interface Cmd {
execute(bot: BotClient, interaction: Interaction, db: FirebaseFirestore.Firestore, lang: Record<string, string | any>, language: string, serverSettings: ServerSettings): void;
}
export class BotClient extends Client {
commands: Collection<string, Cmd> = new Collection<string, Cmd>();
}
const bot = new BotClient({ allowedMentions: { parse: ['users', 'roles'], repliedUser: true }, intents: [GatewayIntentBits.Guilds] });
import { config } from 'dotenv';
config();
import botConfig from './botConfig.json';
function setBotStatus() {
bot.user.setActivity({ name: `${bot.guilds.cache.size} server${bot.guilds.cache.size !== 1 ? 's' : ''}!`, type: 3 });
}
import fs from 'fs';
import admin from 'firebase-admin';
admin.initializeApp({
credential: admin.credential.cert({
projectId: 'gumball-and-penny',
privateKey: process.env.GOOGLE_APPLICATION_CREDENTIALS,
clientEmail: 'firebase-adminsdk-weska@gumball-and-penny.iam.gserviceaccount.com'
}),
databaseURL: 'https://gumball-and-penny.firebaseio.com'
});
const db = admin.firestore();
import getText from './src/functions/getText';
import moment from 'moment';
import { shopButtonHandler } from './src/functions/shopHandler';
import { quizButtonHandler } from './src/functions/quizHandler';
import { confirmLanguage } from './src/functions/setlanguageHandler';
const vips: Set<string> = new Set();
bot.once('ready', () => {
setBotStatus();
moment.locale('pt');
console.log(`Preparados! (${moment().format('LL')} ${moment().format('LTS')})`);
});
const settings: any = new Object();
const xpCooldown: Set<string> = new Set();
async function getServerSettings(guild: Guild) {
const refD = db.collection('definicoes').doc(guild.id);
if (!settings[guild.id]) {
await refD.get().then(docD => {
if (!docD.exists) {
db.collection('definicoes').doc(guild.id).set({
settings: botConfig.settings
});
}
settings[guild.id] = docD.get('settings') || botConfig.settings;
});
}
const serverSettings: ServerSettings = settings[guild.id];
return serverSettings;
}
const englishChannels = ['928784346826047488', '928784416917061683'];
const commands = [];
const commandsPath = './src/commands/';
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.ts'));
for (const file of commandFiles) {
const filePath = commandsPath + file;
const props = require(filePath);
commands.push(props.data.toJSON());
bot.commands.set(props.data.name, props);
}
const rest = new REST().setToken(process.env.TOKENDEV);
(async () => {
try {
await rest.put(
Routes.applicationGuildCommands(process.env.CLIENTDEV, '738540548305977366'),
{ body: commands }
);
console.log('Slash commands recarregados!');
}
catch (error) {
console.error(error);
}
})();
import items from './src/data/itemlist.json';
import titleCase from './src/functions/titleCase';
bot.on('interactionCreate', async interaction => {
if (!interaction.guild || interaction.channel.id === '810529155955032115' || interaction.user.bot) return;
if (interaction.isChatInputCommand()) {
const serverSettings = await getServerSettings(interaction.guild);
let language;
if (englishChannels.includes(interaction.channel.id)) language = 'en';
else language = serverSettings.language;
const lang = require(`./src/lang/${language}.json`);
const command = bot.commands.get(interaction.commandName);
if (!command) return;
if (!xpCooldown.has(interaction.user.id)) {
xpCooldown.add(interaction.user.id);
setTimeout(() => {
xpCooldown.delete(interaction.user.id);
}, 60000);
db.collection('perfis').doc(interaction.user.id).get().then(doc => {
if (!doc.exists) {
return;
}
else {
const xp: number = doc.get('xp');
const maxXP = 2000000;
const level = Math.floor(Math.sqrt(xp / maxXP) * 99) + 1;
let add = Math.floor(Math.random() * 10) + 20;
let newXP: number;
if (vips.has(interaction.user.id) || interaction.user.id === botConfig.botOwnerID || botConfig.collaboratorIDs.includes(interaction.user.id)) add *= 2;
newXP = xp + add;
if (newXP > maxXP) newXP = maxXP;
const newLevel = Math.floor(Math.sqrt(newXP / maxXP) * 99) + 1;
db.collection('perfis').doc(interaction.user.id).update({
xp: newXP
}).then(async () => {
if (newLevel > level) {
await db.collection('perfis').doc(interaction.user.id).update({
level: newLevel
});
const bal: number = doc.get('balance');
if (newLevel % 10 === 0) {
const reward = newLevel / 10 * 500;
await db.collection('perfis').doc(interaction.user.id).update({
balance: bal + reward
}).then( async () => await interaction.channel.send(getText(lang.levelUp.congratsReward, [interaction.user.tag, newLevel, reward])));
}
else {
await interaction.channel.send(getText(lang.levelUp.congrats, [interaction.user.tag, newLevel]));
}
}
});
}
});
}
try {
command.execute(bot, interaction, db, lang, language, serverSettings);
}
catch (err) {
console.error(err);
interaction.reply({ content: lang.error.cmd, ephemeral: true });
}
}
else
{
const serverSettings = await getServerSettings(interaction.guild);
let language: string, newLanguage: string;
if (englishChannels.includes(interaction.channel.id)) language = 'en';
else language = serverSettings.language;
const lang = require(`./src/lang/${language}.json`);
if (interaction.isButton()) {
if (interaction.customId.startsWith('shop')) shopButtonHandler(interaction, lang);
else if (interaction.customId.startsWith('quiz')) quizButtonHandler(interaction, lang);
else if (interaction.customId.startsWith('language')) confirmLanguage(interaction, db, newLanguage, lang, serverSettings);
}
else if (interaction.isSelectMenu()) {
if (interaction.customId.startsWith('languageMenu')) {
newLanguage = interaction.values[0];
}
}
else if (interaction.isAutocomplete()) {
const focusedOption = interaction.options.getFocused(true);
let choices;
if (interaction.commandName === 'shop') {
switch(focusedOption.name) {
case 'hudname':
choices = Object.entries(items.huds);
choices.shift();
choices.shift();
break;
case 'pethudname':
choices = Object.entries(items.petHuds);
choices.shift();
choices.shift();
break;
case 'itemname':
choices = Object.entries(items.items);
break;
case 'petname':
choices = Object.entries(items.pets);
break;
}
choices = Object.fromEntries(choices.map(([key, value]) => {
return [key.toLowerCase(), value];
}));
}
const filtered = Object.keys(choices).filter(choice =>
focusedOption.value !== '' ? focusedOption.value.length > 1 ? choice.includes(focusedOption.value.toLowerCase()) : choice.startsWith(focusedOption.value.toLowerCase()) : false
);
await interaction.respond(
filtered.map(choice => ({ name: titleCase(choice), value: titleCase(choice) }))
);
}
}
});
bot.on('guildCreate', async guildData => {
setBotStatus();
await db.collection('definicoes').doc(guildData.id).set({
settings: botConfig.settings
});
});
bot.on('guildDelete', async guildData => {
setBotStatus();
await db.collection('definicoes').doc(guildData.id).delete();
});
bot.login(process.env.TOKENDEV);