1 |
rakin |
51 |
import fs from 'fs'; |
2 |
rakin |
153 |
import DiscordClient from '../client/Client'; |
3 |
rakin |
210 |
import { GuildMember, Message, CommandInteraction, MessageEmbed, ContextMenuInteraction, Interaction } from 'discord.js'; |
4 |
rakin |
51 |
import Axios, { AxiosRequestHeaders, HeadersDefaults } from 'axios'; |
5 |
|
|
|
6 |
rakin |
236 |
export function splitMessage(message: string, limit: number = 1000, maxIterationCount: number = 100) { |
7 |
|
|
const splitted: string[] = []; |
8 |
|
|
let content = message; |
9 |
|
|
let { length } = content; |
10 |
|
|
|
11 |
|
|
if (length >= limit) { |
12 |
|
|
let i = 0; |
13 |
|
|
|
14 |
|
|
while (length !== 0 && content !== '') { |
15 |
|
|
splitted.push(content.substring(0, limit)); |
16 |
|
|
content = content.substring(limit); |
17 |
|
|
length = content.length; |
18 |
|
|
i++; |
19 |
|
|
|
20 |
|
|
if (i >= maxIterationCount) { |
21 |
|
|
console.log('Break loop'); |
22 |
|
|
break; |
23 |
|
|
} |
24 |
|
|
} |
25 |
|
|
} |
26 |
|
|
else { |
27 |
|
|
splitted.push(message); |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
return splitted; |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
export function getHomeGuild(client: DiscordClient) { |
34 |
|
|
return client.guilds.cache.get(client.config.props.global.id); |
35 |
|
|
} |
36 |
|
|
|
37 |
rakin |
153 |
export function shouldNotModerate(client: DiscordClient, member: GuildMember) { |
38 |
|
|
if (!client.config.props[member.guild.id].admin) { |
39 |
|
|
return false; |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
const role = client.config.props[member.guild.id].admin; |
43 |
|
|
|
44 |
|
|
return member.roles.cache.has(role); |
45 |
|
|
} |
46 |
|
|
|
47 |
rakin |
196 |
export async function hasPermission(client: DiscordClient, member: GuildMember, msg: Message | CommandInteraction | ContextMenuInteraction, mod: GuildMember | null, error: string = "You don't have permission to moderate this user") { |
48 |
rakin |
194 |
let m = mod; |
49 |
|
|
|
50 |
|
|
if (!m) { |
51 |
|
|
m = msg.member! as GuildMember; |
52 |
|
|
} |
53 |
|
|
|
54 |
rakin |
220 |
if (member.id !== m.id && member.roles.highest?.position >= m.roles.highest?.position) { |
55 |
rakin |
210 |
if (msg instanceof Interaction && msg.deferred) { |
56 |
|
|
await msg.editReply({ |
57 |
|
|
embeds: [ |
58 |
|
|
new MessageEmbed() |
59 |
|
|
.setColor('#f14a60') |
60 |
|
|
.setDescription(`:x: ${error}`) |
61 |
|
|
] |
62 |
|
|
}); |
63 |
|
|
|
64 |
|
|
return false; |
65 |
|
|
} |
66 |
|
|
|
67 |
rakin |
194 |
await msg.reply({ |
68 |
|
|
embeds: [ |
69 |
|
|
new MessageEmbed() |
70 |
|
|
.setColor('#f14a60') |
71 |
|
|
.setDescription(`:x: ${error}`) |
72 |
|
|
] |
73 |
|
|
}); |
74 |
|
|
|
75 |
|
|
return false; |
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
return true; |
79 |
|
|
} |
80 |
|
|
|
81 |
rakin |
51 |
export function timeProcess(seconds: number) { |
82 |
|
|
let interval = seconds / (60 * 60 * 24 * 30 * 365); |
83 |
|
|
|
84 |
|
|
if (interval >= 1) { |
85 |
|
|
return Math.floor(interval) + " year" + (Math.floor(interval) === 1 ? '' : 's'); |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
interval = seconds / (60 * 60 * 24 * 30); |
89 |
|
|
|
90 |
|
|
if (interval >= 1) { |
91 |
|
|
return Math.floor(interval) + " month" + (Math.floor(interval) === 1 ? '' : 's'); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
interval = seconds / (60 * 60 * 24); |
95 |
|
|
|
96 |
|
|
if (interval >= 1) { |
97 |
|
|
return Math.floor(interval) + " day" + (Math.floor(interval) === 1 ? '' : 's'); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
interval = seconds / (60 * 60); |
101 |
|
|
|
102 |
|
|
if (interval >= 1) { |
103 |
|
|
return Math.floor(interval) + " hour" + (Math.floor(interval) === 1 ? '' : 's'); |
104 |
|
|
} |
105 |
|
|
|
106 |
|
|
interval = seconds / 60; |
107 |
|
|
|
108 |
|
|
if (interval >= 1) { |
109 |
|
|
return Math.floor(interval) + " minute" + (Math.floor(interval) === 1 ? '' : 's'); |
110 |
|
|
} |
111 |
|
|
|
112 |
|
|
interval = seconds; |
113 |
|
|
|
114 |
|
|
return Math.floor(interval) + " second" + (Math.floor(interval) === 1 ? '' : 's'); |
115 |
|
|
} |
116 |
|
|
|
117 |
|
|
export function escapeRegex(string: string) { |
118 |
|
|
return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
export function timeSince(date: number) { |
122 |
|
|
const seconds = Math.floor((Date.now() - date) / 1000); |
123 |
|
|
return timeProcess(seconds) + ' ago'; |
124 |
|
|
} |
125 |
|
|
|
126 |
|
|
export async function download(url: string, path: string, headers?: AxiosRequestHeaders) { |
127 |
|
|
const writer = fs.createWriteStream(path); |
128 |
|
|
|
129 |
|
|
const response = await Axios({ |
130 |
|
|
url, |
131 |
|
|
method: 'GET', |
132 |
|
|
responseType: 'stream', |
133 |
|
|
headers |
134 |
|
|
}); |
135 |
|
|
|
136 |
|
|
response.data.pipe(writer); |
137 |
|
|
|
138 |
|
|
return new Promise((resolve, reject) => { |
139 |
|
|
if (response.status !== 200) { |
140 |
|
|
reject(); |
141 |
|
|
} |
142 |
|
|
|
143 |
|
|
writer.on('finish', resolve); |
144 |
|
|
writer.on('error', reject); |
145 |
|
|
}); |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
export async function deleteFile(path: string) { |
149 |
|
|
fs.rm(path, (err: any) => { |
150 |
|
|
if (err) { |
151 |
|
|
throw new Error(err); |
152 |
|
|
} |
153 |
|
|
}); |
154 |
|
|
} |
155 |
|
|
|
156 |
|
|
export function random(arr: Array <any>) { |
157 |
|
|
let index = Math.floor(Math.random() * arr.length); |
158 |
|
|
return arr[index]; |
159 |
rakin |
58 |
} |
160 |
|
|
|
161 |
|
|
export function fill(length: number, string: string, token: string = ' ') { |
162 |
|
|
let safe = 0; |
163 |
|
|
|
164 |
|
|
if (length < string.length) |
165 |
|
|
return string; |
166 |
|
|
|
167 |
|
|
const diff = length - string.length; |
168 |
|
|
|
169 |
|
|
for (let i = 1; i <= diff; i++, safe++) { |
170 |
|
|
if (safe >= 500) |
171 |
|
|
break; |
172 |
|
|
|
173 |
|
|
string += ' '; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
return string; |
177 |
|
|
} |
178 |
|
|
|
179 |
|
|
export function green(string: string) { |
180 |
|
|
return '\u001b[1;32m' + string + '\u001b[0m'; |
181 |
|
|
} |
182 |
|
|
|
183 |
|
|
export function yellow(string: string) { |
184 |
|
|
return '\u001b[1;33m' + string + '\u001b[0m'; |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
export function red(string: string) { |
188 |
|
|
return '\u001b[1;31m' + string + '\u001b[0m'; |
189 |
rakin |
86 |
} |