/[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 239 by rakin, Mon Jul 29 17:29:09 2024 UTC revision 422 by rakin, Mon Jul 29 17:30:09 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 { formatDistance } from 'date-fns';  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) {  export function splitMessage(message: string, limit: number = 1000, maxIterationCount: number = 100) {
46      const splitted: string[] = [];      const splitted: string[] = [];
# Line 42  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 52  export async function hasPermission(clie Line 90  export async function hasPermission(clie
90          m = msg.member! as GuildMember;          m = msg.member! as GuildMember;
91      }      }
92            
93      if (member.id !== m.id && member.roles.highest?.position >= m.roles.highest?.position) {      if (member.id === m.id || member.roles.highest?.position >= m.roles.highest?.position) {
94          if (msg instanceof Interaction && msg.deferred) {          if (msg instanceof Interaction && msg.deferred) {
95              await msg.editReply({              await msg.editReply({
96                  embeds: [                  embeds: [
# Line 80  export async function hasPermission(clie Line 118  export async function hasPermission(clie
118  }  }
119    
120  export function timeProcess(seconds: number) {  export function timeProcess(seconds: number) {
121      return formatDistance(new Date(), new Date(seconds));      return formatDuration(intervalToDuration({ start: 0, end: seconds * 1000 }));
122  }  }
123    
124    
# Line 130  export function escapeRegex(string: stri Line 168  export function escapeRegex(string: stri
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 formatDistance(new Date(), new Date(date), { addSuffix: true });      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.239  
changed lines
  Added in v.422

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26