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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 51 by rakin, Mon Jul 29 17:28:23 2024 UTC revision 236 by rakin, Mon Jul 29 17:29:09 2024 UTC
# Line 1  Line 1 
1  import fs from 'fs';  import fs from 'fs';
2    import DiscordClient from '../client/Client';
3    import { GuildMember, Message, CommandInteraction, MessageEmbed, ContextMenuInteraction, Interaction } from 'discord.js';
4  import Axios, { AxiosRequestHeaders, HeadersDefaults } from 'axios';  import Axios, { AxiosRequestHeaders, HeadersDefaults } from 'axios';
5    
6    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    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    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        let m = mod;
49        
50        if (!m) {
51            m = msg.member! as GuildMember;
52        }
53        
54        if (member.id !== m.id && member.roles.highest?.position >= m.roles.highest?.position) {
55            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            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  export function timeProcess(seconds: number) {        export function timeProcess(seconds: number) {      
82      let interval = seconds / (60 * 60 * 24 * 30 * 365);      let interval = seconds / (60 * 60 * 24 * 30 * 365);
83    
# Line 79  export async function deleteFile(path: s Line 156  export async function deleteFile(path: s
156  export function random(arr: Array <any>) {  export function random(arr: Array <any>) {
157      let index = Math.floor(Math.random() * arr.length);      let index = Math.floor(Math.random() * arr.length);
158      return arr[index];      return arr[index];
 }  
159    }
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    }

Legend:
Removed from v.51  
changed lines
  Added in v.236

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26