/[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 220 by rakin, Mon Jul 29 17:29:04 2024 UTC revision 393 by rakin, Mon Jul 29 17:29:59 2024 UTC
# Line 1  Line 1 
1    /**
2    * This file is part of SudoBot.
3    *
4    * Copyright (C) 2021-2022 OSN Inc.
5    *
6    * SudoBot is free software; you can redistribute it and/or modify it
7    * under the terms of the GNU Affero General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10    *
11    * SudoBot is distributed in the hope that it will be useful, but
12    * WITHOUT ANY WARRANTY; without even the implied warranty of
13    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14    * GNU Affero General Public License for more details.
15    *
16    * You should have received a copy of the GNU Affero General Public License
17    * along with SudoBot. If not, see <https://www.gnu.org/licenses/>.
18    */
19    
20  import fs from 'fs';  import fs from 'fs';
21  import DiscordClient from '../client/Client';  import DiscordClient from '../client/Client';
22  import { GuildMember, Message, CommandInteraction, MessageEmbed, ContextMenuInteraction, Interaction } from 'discord.js';  import { GuildMember, Message, CommandInteraction, MessageEmbed, ContextMenuInteraction, Interaction } from 'discord.js';
23  import Axios, { AxiosRequestHeaders, HeadersDefaults } from 'axios';  import Axios, { AxiosRequestHeaders } from 'axios';
24    import { formatDistanceToNowStrict, formatDuration, intervalToDuration } from 'date-fns';
25    
26    export function parseEmbedsInString(content: string) {
27        const embedExpressions = content.matchAll(/embed\:(\{[^\n]+\})/g);
28        const newContent = content.replace(/embed\:(\{[^\n]+\})/g, '');
29        let embeds: MessageEmbed[] = [];
30    
31        for (const expr of [...embedExpressions]) {
32            const parsed = JSON.parse(expr[1]);
33    
34            try {
35                embeds.push(new MessageEmbed(parsed).setColor(parsed.color));    
36            }
37            catch (e) {
38                console.log(e);
39            }
40        }
41    
42        return { embeds, content: newContent };
43    }
44    
45    export function splitMessage(message: string, limit: number = 1000, maxIterationCount: number = 100) {
46        const splitted: string[] = [];
47        let content = message;
48        let { length } = content;
49    
50        if (length >= limit) {
51            let i = 0;
52    
53            while (length !== 0 && content !== '') {
54                splitted.push(content.substring(0, limit));
55                content = content.substring(limit);
56                length = content.length;
57                i++;
58    
59                if (i >= maxIterationCount) {
60                    console.log('Break loop');
61                    break;
62                }
63            }
64        }
65        else {
66            splitted.push(message);
67        }
68    
69        return splitted;
70    }
71    
72    export function getHomeGuild(client: DiscordClient) {
73        return client.guilds.cache.get(client.config.props.global.id);
74    }
75    
76  export function shouldNotModerate(client: DiscordClient, member: GuildMember) {  export function shouldNotModerate(client: DiscordClient, member: GuildMember) {
77      if (!client.config.props[member.guild.id].admin) {      if (!client.config.props[member.guild.id].admin) {
# Line 10  export function shouldNotModerate(client Line 80  export function shouldNotModerate(client
80    
81      const role = client.config.props[member.guild.id].admin;      const role = client.config.props[member.guild.id].admin;
82    
83      return member.roles.cache.has(role);      return role && role.trim() !== '' && member.roles.cache.has(role);
84  }  }
85    
86  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") {  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") {
# Line 47  export async function hasPermission(clie Line 117  export async function hasPermission(clie
117      return true;      return true;
118  }  }
119    
120  export function timeProcess(seconds: number) {        export function timeProcess(seconds: number) {
121        return formatDuration(intervalToDuration({ start: 0, end: seconds * 1000 }));
122    }
123    
124    
125    /**
126     * @deprecated
127     */
128    export function timeProcessOld(seconds: number) {      
129      let interval = seconds / (60 * 60 * 24 * 30 * 365);      let interval = seconds / (60 * 60 * 24 * 30 * 365);
130    
131      if (interval >= 1) {      if (interval >= 1) {
# Line 88  export function escapeRegex(string: stri Line 166  export function escapeRegex(string: stri
166  }  }
167    
168  export function timeSince(date: number) {  export function timeSince(date: number) {
169      const seconds = Math.floor((Date.now() - date) / 1000);      // const seconds = Math.floor((Date.now() - date) / 1000);
170      return timeProcess(seconds) + ' ago';      // return timeProcess(seconds) + ' ago';
171        return formatDistanceToNowStrict(new Date(date), { addSuffix: true });
172  }  }
173    
174  export async function download(url: string, path: string, headers?: AxiosRequestHeaders) {    export async function download(url: string, path: string, headers?: AxiosRequestHeaders) {  

Legend:
Removed from v.220  
changed lines
  Added in v.393

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26