forked from GeorgeCiesinski/poke-guesser-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.js
181 lines (178 loc) · 5.88 KB
/
mod.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
const { ApplicationCommandType, ApplicationCommandOptionType } = require('discord.js');
const language = require('./language.js');
const util = require("./util");
const delayJS = require('./delay.js');
const timeoutJS = require('./timeout.js');
const championshipJS = require('./championship.js');
async function mod(interaction, db) {
await interaction.deferReply({ ephemeral: true }); // PokeBot is thinking
const lang = await language.getLanguage(interaction.guildId, db);
const subcommandgroup = interaction.options.getSubcommandGroup();
const subcommand = interaction.options.getSubcommand();
let isMod = false;
if (await db.isMod(interaction.member)) {
isMod = true;
} else {
interaction.member.roles.cache.each(async role => {
if (!isMod && (await db.isMod(role)))
isMod = true;
});
}
let response = {
title: '',
description: ''
};
if (isMod) {
switch (subcommandgroup) {
case 'score': // /mod score ?
switch (subcommand) {
case 'add':
try {
let score = interaction.options.getInteger('score', false);
if (score) {
await db.addScore(interaction.guildId, interaction.options.getUser('user').id, score);
} else {
let user = interaction.options.getUser('user');
let dbScore = await db.getScore(interaction.guildId, user.id);
if (!dbScore)
await db.setScore(interaction.guildId, user.id, 0);
}
response.title = lang.obj['mod_score_add_title_success'];
response.description = lang.obj['mod_score_add_description_success'];
} catch (err) {
response.title = lang.obj['mod_score_add_title_failed'];
response.description = `${lang.obj['mod_score_add_description_failed']}${err}`;
}
break;
case 'remove':
try {
let score = interaction.options.getInteger('score', false);
if (score) {
await db.removeScore(interaction.guildId, interaction.options.getUser('user').id, score);
} else {
await db.unsetScore(interaction.guildId, interaction.options.getUser('user').id);
}
response.title = lang.obj['mod_score_remove_title_success'];
response.description = lang.obj['mod_score_remove_description_success'];
} catch (err) {
response.title = lang.obj['mod_score_remove_title_failed'];
response.description = `${lang.obj['mod_score_remove_description_failed']}${err}`;
}
break;
case 'set':
try {
let score = interaction.options.getInteger('score', false);
if (score) {
await db.setScore(interaction.guildId, interaction.options.getUser('user').id, score);
} else {
let user = interaction.options.getUser('user');
let dbScore = await db.getScore(interaction.guildId, user.id);
if (!dbScore)
await db.setScore(interaction.guildId, user.id, 0);
}
response.title = lang.obj['mod_score_set_title_success'];
response.description = lang.obj['mod_score_set_description_success'];
} catch (err) {
response.title = lang.obj['mod_score_set_title_failed'];
response.description = `${lang.obj['mod_score_set_description_failed']}${err}`;
}
break;
}
break;
case 'delay': // /mod delay ?
response = await delayJS.delay(interaction, subcommand, lang, db);
break;
case 'timeout': // /mod timeout ?
response = await timeoutJS.timeout(interaction, subcommand, lang, db);
break;
case 'championship': // /mod championship ?
response = await championshipJS.championship(interaction, subcommand, lang, db);
break;
}
} else {
response.title = lang.obj['mod_no_mod_title'];
response.description = lang.obj['mod_no_mod_description'];
}
// returnEmbed(title, message, image=null)
interaction.editReply({
embeds: [util.returnEmbed(response.title, response.description)],
ephemeral: true
});
}
function getRegisterObject() {
return {
name: 'mod',
description: 'Manage delay, timeout and score',
type: ApplicationCommandType.ChatInput,
options: [
{
name: 'score',
description: 'Manage the score of someone',
type: ApplicationCommandOptionType.SubcommandGroup,
options: [
{
name: 'add',
description: 'Add a score to a user',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'user',
description: 'The user whose score you want to update',
required: true,
type: ApplicationCommandOptionType.User
},
{
name: 'score',
description: 'The amount of points you want to add to the score',
required: false,
type: ApplicationCommandOptionType.Integer
}
]
},
{
name: 'remove',
description: 'Remove a score from a user',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'user',
description: 'The user whose score you want to update',
required: true,
type: ApplicationCommandOptionType.User
},
{
name: 'score',
description: 'The amount of points you want to remove from the score',
required: false,
type: ApplicationCommandOptionType.Integer
}
]
},
{
name: 'set',
description: 'Set the score of a user',
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: 'user',
description: 'The user whose score you want to update',
required: true,
type: ApplicationCommandOptionType.User
},
{
name: 'score',
description: 'The amount of points you want to set the score',
required: false,
type: ApplicationCommandOptionType.Integer
}
]
}
]
},
delayJS.getRegisterObject(), timeoutJS.getRegisterObject(), championshipJS.getRegisterObject()
]
};
}
// Exports each function separately
module.exports.mod = mod;
module.exports.getRegisterObject = getRegisterObject;