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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 196 - (hide annotations)
Mon Jul 29 17:28:58 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: application/typescript
File size: 3697 byte(s)
fix(typescript): wrong typings
1 rakin 51 import fs from 'fs';
2 rakin 153 import DiscordClient from '../client/Client';
3 rakin 196 import { GuildMember, Message, CommandInteraction, MessageEmbed, ContextMenuInteraction } 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 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") {
17 rakin 194 let m = mod;
18    
19     if (!m) {
20     m = msg.member! as GuildMember;
21     }
22    
23     if (member.roles.highest?.position > m.roles.highest?.position) {
24     await msg.reply({
25     embeds: [
26     new MessageEmbed()
27     .setColor('#f14a60')
28     .setDescription(`:x: ${error}`)
29     ]
30     });
31    
32     return false;
33     }
34    
35     return true;
36     }
37    
38 rakin 51 export function timeProcess(seconds: number) {
39     let interval = seconds / (60 * 60 * 24 * 30 * 365);
40    
41     if (interval >= 1) {
42     return Math.floor(interval) + " year" + (Math.floor(interval) === 1 ? '' : 's');
43     }
44    
45     interval = seconds / (60 * 60 * 24 * 30);
46    
47     if (interval >= 1) {
48     return Math.floor(interval) + " month" + (Math.floor(interval) === 1 ? '' : 's');
49     }
50    
51     interval = seconds / (60 * 60 * 24);
52    
53     if (interval >= 1) {
54     return Math.floor(interval) + " day" + (Math.floor(interval) === 1 ? '' : 's');
55     }
56    
57     interval = seconds / (60 * 60);
58    
59     if (interval >= 1) {
60     return Math.floor(interval) + " hour" + (Math.floor(interval) === 1 ? '' : 's');
61     }
62    
63     interval = seconds / 60;
64    
65     if (interval >= 1) {
66     return Math.floor(interval) + " minute" + (Math.floor(interval) === 1 ? '' : 's');
67     }
68    
69     interval = seconds;
70    
71     return Math.floor(interval) + " second" + (Math.floor(interval) === 1 ? '' : 's');
72     }
73    
74     export function escapeRegex(string: string) {
75     return string.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
76     }
77    
78     export function timeSince(date: number) {
79     const seconds = Math.floor((Date.now() - date) / 1000);
80     return timeProcess(seconds) + ' ago';
81     }
82    
83     export async function download(url: string, path: string, headers?: AxiosRequestHeaders) {
84     const writer = fs.createWriteStream(path);
85    
86     const response = await Axios({
87     url,
88     method: 'GET',
89     responseType: 'stream',
90     headers
91     });
92    
93     response.data.pipe(writer);
94    
95     return new Promise((resolve, reject) => {
96     if (response.status !== 200) {
97     reject();
98     }
99    
100     writer.on('finish', resolve);
101     writer.on('error', reject);
102     });
103     }
104    
105     export async function deleteFile(path: string) {
106     fs.rm(path, (err: any) => {
107     if (err) {
108     throw new Error(err);
109     }
110     });
111     }
112    
113     export function random(arr: Array <any>) {
114     let index = Math.floor(Math.random() * arr.length);
115     return arr[index];
116 rakin 58 }
117    
118     export function fill(length: number, string: string, token: string = ' ') {
119     let safe = 0;
120    
121     if (length < string.length)
122     return string;
123    
124     const diff = length - string.length;
125    
126     for (let i = 1; i <= diff; i++, safe++) {
127     if (safe >= 500)
128     break;
129    
130     string += ' ';
131     }
132    
133     return string;
134     }
135    
136     export function green(string: string) {
137     return '\u001b[1;32m' + string + '\u001b[0m';
138     }
139    
140     export function yellow(string: string) {
141     return '\u001b[1;33m' + string + '\u001b[0m';
142     }
143    
144     export function red(string: string) {
145     return '\u001b[1;31m' + string + '\u001b[0m';
146 rakin 86 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26