-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.js
112 lines (99 loc) · 2.5 KB
/
base.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
const Math = require('mathjs');
const {
Client,
GatewayIntentBits,
Collection,
Interaction,
SlashCommandBuilder,
} = require('discord.js');
class SlashCommand extends SlashCommandBuilder {
constructor() {
super();
this.execute = null;
this.per = null;
}
setCooldown(per) {
this.per = per;
client.cooldowns.set(this.name, new Array());
return this;
}
/**
*
* @param {(interaction: Interaction)} coro
*/
callback(coro) {
this.execute = coro;
client.slashcommands.set(this.name, this);
}
}
/**
*
* @param {number} min
* @param {number | null} max
* @returns
*/
function randint(min, max = null) {
if (max === null) return Math.floor(Math.random() * min);
return Math.floor(Math.random() * (max - min + 1) + min);
}
/**
*
* @param {Array} array
* @returns
*/
function shuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
/**
*
* @param {number} milliseconds
* @returns
*/
function time_convertor(milliseconds) {
const secs_ = Math.floor(milliseconds / 1000);
let secs = secs_;
const days = Math.floor(secs / (24 * 3600));
secs %= 3600 * 24;
const hours = Math.floor(secs / 3600);
secs %= 3600;
const minutes = Math.floor(secs / 60);
secs %= 60;
const seconds = secs;
if (secs_ <= 3600) return `${minutes}:${seconds}`;
if (secs_ < 24 * 3600) return `${hours}:${minutes}:${seconds}`;
return `${days} day(s)`;
}
async function getImage(givenMessage, subredditName) {
sub = subreddits[Math.round(Math.random() * (subreddits.length - 1))];
let subr = await trev.getCustomSubreddit(sub);
if (!subr || !subr.media) {
subr = await trev.getCustomSubreddit(sub);
}
const image = subr.media;
// console.log(image);
if (
image.toString().includes('.png') ||
image.toString().includes('.jpg') ||
image.toString().includes('.jpeg') ||
image.toString().includes('.gif')
) {
const imageEmbed = new EmbedBuilder()
.setColor(Math.floor(Math.random() * 16777215).toString(16))
.setTitle(`Random Image from r/${sub}`)
.setURL(image)
.setImage(image);
givenMessage.reply({ embeds: [imageEmbed] });
} else {
getImage(givenMessage, sub).catch((e) => getImage(givenMessage, sub));
}
// givenMessage.delete({timeout: delay}).catch(console.error);
}
// another idea, we can have a function calculate uptime of the bot and return a string
// we have multiple commands that can utilize this
module.exports = {
SlashCommand,
randint,
shuffle,
time_convertor,
getImage,
};