-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.js
executable file
·322 lines (298 loc) · 10.7 KB
/
functions.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
const { MessageEmbed, Permissions } = require('discord.js');
const Sequelize = require('sequelize');
const { sqluser, sqlpass, sqldb } = require('./config.json');
// Create a new sequelize connection using mariadb
const sequelize = new Sequelize(sqldb, sqluser, sqlpass, {
host: 'localhost',
dialect: 'mariadb',
logging: false,
});
// Define userTables
const userTables = sequelize.define('usertables', {
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false,
},
infractions: {
type: Sequelize.TEXT,
allowNull: true,
},
});
// Define guildTables
const guildTables = sequelize.define('guildtables', {
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false,
},
modrole: {
type: Sequelize.STRING,
allowNull: true,
},
manrole: {
type: Sequelize.STRING,
allowNull: true,
},
maxinfractions: {
type: Sequelize.INTEGER,
defaultValue: 3,
allowNull: false,
},
mutedrole: {
type: Sequelize.STRING,
allowNull: true,
},
disabledcommands: {
type: Sequelize.STRING,
allowNull: true,
},
});
/* Define punTables
TODO: everything */
const punTables = sequelize.define('puntables', {
name: {
type: Sequelize.STRING,
unique: true,
allowNull: false,
},
punishment: {
type: Sequelize.STRING,
allowNull: true,
},
duration: {
type: Sequelize.INTEGER,
allowNull: true,
},
expiration: {
type: Sequelize.STRING,
allowNull: true,
},
});
// Function to easily create a new usertable
async function userTableCreate(name, infractions) {
try {
const usertable = await userTables.create({
name: name,
infractions: infractions,
});
return usertable;
}
catch (error) {
console.log(error);
}
}
// Function to easily create a new guildtable
async function guildTableCreate(name, max, manrole, modrole, mutedrole, disabledcommands) {
try {
const guildtable = await guildTables.create({
name: name,
maxinfractions: max,
manrole: manrole,
modrole: modrole,
mutedrole: mutedrole,
disabledcommands: disabledcommands,
});
return guildtable;
}
catch (error) {
console.log(error);
}
}
/*
Function to generate random numbers:
console.log(getRandomIntInclusive(1, 3));
returns 1, 2 or 3
*/
function getRandomIntInclusive(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
// Function to turn objects to string, used for nekos.life api
function objToString(object) {
let str = '';
for (const k in object) {
// eslint-disable-next-line no-prototype-builtins
if (object.hasOwnProperty(k)) {
str += object[k];
}
}
return str;
}
// Generate a random hex color for use in embeds
const randomColor = () => {
let color = '';
for (let i = 0; i < 6; i++) {
const random = Math.random();
const bit = (random * 16) | 0;
color += (bit).toString(16);
}
return color;
};
// Check messages... Used in say and owoify
function contentcheck(message, filter) {
const len = filter.length;
for (let i = 0; i < len; i++) {
if (message.includes(filter[i])) {
return true;
}
}
return false;
}
// infractionlist function, makes a readable list in string form using an array.
function infractionlist(infractions, limit) {
let str = '';
for (let i = 0; i <= infractions.length - 1; i++) {
const x = i + 1;
if (i == limit) {
return str;
} else if (i == 0) {
str += `${infractions.slice(-1)} \n`;
} else {
str += `${infractions.slice(-x, -i)} \n`;
}
}
return str;
}
// errembed function, makes a standard error embed. Used for all errors in the bot
function errembed({ interaction, author, desc, defer }) {
let emb = new MessageEmbed()
.setColor('#CC0000');
if (author) {
emb = emb.setAuthor({ name: author });
}
if (desc) {
emb = emb.setDescription(desc);
}
if (defer == true) {
return interaction.editReply({ embeds: [emb], ephemeral: true });
} else {
return interaction.reply({ embeds: [emb], ephemeral: true });
}
}
// dmpunembed function, makes an embed to DM to user when they have been punished.
function dmpunembed({ interaction, reason = null, punishmenttext }) {
const title = `${punishmenttext.charAt(0).toUpperCase()}${punishmenttext.slice(1).toLowerCase()}`;
const emb = new MessageEmbed()
.setTitle(title)
.setDescription(`You have been ${punishmenttext} in ${interaction.guild.name}!`);
if (punishmenttext === 'banned' || punishmenttext == 'muted') {
emb.setColor('#CC0000');
emb.addField('Duration:', 'Permanent', true);
}
if (punishmenttext !== 'unbanned' && punishmenttext !== 'unmuted') {
emb.addField('Reason:', reason, true);
} else if (punishmenttext == 'unbanned' || punishmenttext == 'unmuted') {
emb.setColor('GREEN');
}
return emb;
}
// punembed function, makes an embed to send in the server when a user has been punished.
function punembed({ member, reason = null, punishmenttext }) {
const title = `${punishmenttext.charAt(0).toUpperCase()}${punishmenttext.slice(1).toLowerCase()}`;
const emb = new MessageEmbed()
.setTitle(title)
.setColor('#5B92E5')
.setDescription(`${member} has been ${punishmenttext}!`)
.setThumbnail(`${member.user.avatarURL()}`);
if (punishmenttext == 'banned' || punishmenttext == 'muted') {
emb.addField('Duration:', 'Permanent', true);
}
if (punishmenttext !== 'unbanned' && punishmenttext !== 'unmuted') {
emb.addField('Reason:', reason, true);
}
return emb;
}
/*
Permcheck function.
Checks permissions of the given user and has multiple checks to make it usable in most commands.
TODO: Make this less complicated if possible.
*/
async function permcheck({ interaction, member, selfcheck, permflag, manonly, roleposcheck, defer }) {
let brole; let usrole;
let memrole;
if (member) {
brole = interaction.guild.me.roles.highest;
usrole = interaction.member.roles.highest;
memrole = member.roles.highest;
if (member.user.bot) {
return (errembed({ interaction: interaction, author: 'This member is a bot!', defer: defer }), true);
}
}
const owner = await interaction.guild.fetchOwner();
if (interaction.member != owner) {
const guildTableName = String(interaction.guild.id + '-guild');
const guildtable = await guildTables.findOne({ where: { name: guildTableName } });
let modrole; let manrole;
let modroles;
if (guildtable) {
modrole = await guildtable.get('modrole');
manrole = await guildtable.get('manrole');
}
// If command can be used by only Managers or both Moderators and Managers
if (manonly == true) {
modroles = [manrole];
} else {
modroles = [modrole, manrole];
}
// If it should check if user is member or not
if (selfcheck == true && member) {
// Check if member is sender and if so send an error message
if (interaction.member == member) {
return (errembed({ interaction: interaction, author: `Please ping someone other than yourself!`, defer: defer }), true);
}
}
// If permflag exists check for permissions
if (permflag) {
if (!interaction.member.permissions.has(permflag)) {
if (!contentcheck(interaction.member._roles, modroles)) {
return (errembed({ interaction: interaction, author: 'You are missing the required permissions!', defer: defer }), true);
}
// Check for modrole and or manrole and if not check role positions
} else if (!contentcheck(interaction.member._roles, modroles)) {
if (member && usrole.comparePositionTo(memrole) <= memrole.comparePositionTo(usrole)) {
return (errembed({ interaction: interaction, author: `Cannot use this on a member with the same or a higher rank than you`, desc: '||Unless you have set a modrole with /settings modrole||', defer: defer }), true);
}
}
// Else just check for modrole or manrole
} else if (!contentcheck(interaction.member._roles, modroles)) {
return (errembed({ interaction: interaction, author: 'You are missing the required permissions!', defer: defer }), true);
}
// Check that member isnt a moderator
if (member && contentcheck(member._roles, modroles) || member && member.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS)) {
return (errembed({ interaction: interaction, author: 'This member is a Moderator!', defer: defer }), true);
}
}
// Check if bot should check its own role position against the member's
// Check positions
if (roleposcheck != false && member && brole.comparePositionTo(memrole) <= memrole.comparePositionTo(brole)) {
// Error if it cant execute
return (errembed({ interaction: interaction, author: `This member's highest role is higher than my highest role`, defer: defer }), true);
}
return false;
}
// updateroles function, updates user(s) roles when a role like "Muted" has been changed.
async function updateroles({ interaction, previousRole, newRole }) {
for (let member of await interaction.guild.members.fetch()) {
member = member.at(1);
if (previousRole && member._roles.includes(previousRole.id)) {
await member.roles.remove(previousRole);
await member.roles.add(newRole);
} else {
await member.roles.add(newRole);
}
}
}
// Function to remove an item from an array.
function removeItemOnce(arr, value) {
const index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
// Define bot commands, used for disabling commands among other things..
const botCommands = ['help', 'avatar', 'userinfo', 'serverinfo', 'ping', 'settings', 'role', 'ban', 'unban', 'kick', 'mute', 'unmute', 'warn', 'infractions', 'purge', 'nick', 'say', 'cuddle', 'hug', 'pat', 'slap', 'neko', 'coinflip', '8ball', 'owoify', 'gayrate'];
// Export all functions/variables
module.exports = { getRandomIntInclusive, objToString, randomColor, contentcheck, sequelize, userTables, guildTables, punTables, userTableCreate, guildTableCreate, errembed, dmpunembed, punembed, infractionlist, permcheck, updateroles, botCommands, removeItemOnce };