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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 153 - (hide annotations)
Mon Jul 29 17:28:48 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 3097 byte(s)
Add Non-moderable role (#34)

* feat(utils): add shouldNotModerate() function

* fix(mod-cmds): no non-moderable role checking
1 rakin 51 import fs from 'fs';
2 rakin 153 import DiscordClient from '../client/Client';
3     import { GuildMember } from 'discord.js';
4 rakin 51 import Axios, { AxiosRequestHeaders, HeadersDefaults } from 'axios';
5    
6 rakin 153 export function shouldNotModerate(client: DiscordClient, member: GuildMember) {
7     if (!client.config.props[member.guild.id].admin) {
8     return false;
9     }
10    
11     const role = client.config.props[member.guild.id].admin;
12    
13     return member.roles.cache.has(role);
14     }
15    
16 rakin 51 export function timeProcess(seconds: number) {
17     let interval = seconds / (60 * 60 * 24 * 30 * 365);
18    
19     if (interval >= 1) {
20     return Math.floor(interval) + " year" + (Math.floor(interval) === 1 ? '' : 's');
21     }
22    
23     interval = seconds / (60 * 60 * 24 * 30);
24    
25     if (interval >= 1) {
26     return Math.floor(interval) + " month" + (Math.floor(interval) === 1 ? '' : 's');
27     }
28    
29     interval = seconds / (60 * 60 * 24);
30    
31     if (interval >= 1) {
32     return Math.floor(interval) + " day" + (Math.floor(interval) === 1 ? '' : 's');
33     }
34    
35     interval = seconds / (60 * 60);
36    
37     if (interval >= 1) {
38     return Math.floor(interval) + " hour" + (Math.floor(interval) === 1 ? '' : 's');
39     }
40    
41     interval = seconds / 60;
42    
43     if (interval >= 1) {
44     return Math.floor(interval) + " minute" + (Math.floor(interval) === 1 ? '' : 's');
45     }
46    
47     interval = seconds;
48    
49     return Math.floor(interval) + " second" + (Math.floor(interval) === 1 ? '' : 's');
50     }
51    
52     export function escapeRegex(string: string) {
53     return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
54     }
55    
56     export function timeSince(date: number) {
57     const seconds = Math.floor((Date.now() - date) / 1000);
58     return timeProcess(seconds) + ' ago';
59     }
60    
61     export async function download(url: string, path: string, headers?: AxiosRequestHeaders) {
62     const writer = fs.createWriteStream(path);
63    
64     const response = await Axios({
65     url,
66     method: 'GET',
67     responseType: 'stream',
68     headers
69     });
70    
71     response.data.pipe(writer);
72    
73     return new Promise((resolve, reject) => {
74     if (response.status !== 200) {
75     reject();
76     }
77    
78     writer.on('finish', resolve);
79     writer.on('error', reject);
80     });
81     }
82    
83     export async function deleteFile(path: string) {
84     fs.rm(path, (err: any) => {
85     if (err) {
86     throw new Error(err);
87     }
88     });
89     }
90    
91     export function random(arr: Array <any>) {
92     let index = Math.floor(Math.random() * arr.length);
93     return arr[index];
94 rakin 58 }
95    
96     export function fill(length: number, string: string, token: string = ' ') {
97     let safe = 0;
98    
99     if (length < string.length)
100     return string;
101    
102     const diff = length - string.length;
103    
104     for (let i = 1; i <= diff; i++, safe++) {
105     if (safe >= 500)
106     break;
107    
108     string += ' ';
109     }
110    
111     return string;
112     }
113    
114     export function green(string: string) {
115     return '\u001b[1;32m' + string + '\u001b[0m';
116     }
117    
118     export function yellow(string: string) {
119     return '\u001b[1;33m' + string + '\u001b[0m';
120     }
121    
122     export function red(string: string) {
123     return '\u001b[1;31m' + string + '\u001b[0m';
124 rakin 86 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26