/[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 86 by rakin, Mon Jul 29 17:28:32 2024 UTC revision 239 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    import { formatDistance } from 'date-fns';
6    
7  export function timeProcess(seconds: number) {        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    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    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        let m = mod;
50        
51        if (!m) {
52            m = msg.member! as GuildMember;
53        }
54        
55        if (member.id !== m.id && member.roles.highest?.position >= m.roles.highest?.position) {
56            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            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    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      let interval = seconds / (60 * 60 * 24 * 30 * 365);      let interval = seconds / (60 * 60 * 24 * 30 * 365);
92    
93      if (interval >= 1) {      if (interval >= 1) {
# Line 42  export function escapeRegex(string: stri Line 128  export function escapeRegex(string: stri
128  }  }
129    
130  export function timeSince(date: number) {  export function timeSince(date: number) {
131      const seconds = Math.floor((Date.now() - date) / 1000);      // const seconds = Math.floor((Date.now() - date) / 1000);
132      return timeProcess(seconds) + ' ago';      // return timeProcess(seconds) + ' ago';
133        return formatDistance(new Date(), new Date(date), { addSuffix: true });
134  }  }
135    
136  export async function download(url: string, path: string, headers?: AxiosRequestHeaders) {    export async function download(url: string, path: string, headers?: AxiosRequestHeaders) {  

Legend:
Removed from v.86  
changed lines
  Added in v.239

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26