/[sudobot]/trunk/src/utils/util.ts
ViewVC logotype

Annotation of /trunk/src/utils/util.ts

Parent Directory Parent Directory | Revision Log Revision Log


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26