/[sudobot]/branches/6.x/src/utils/embed.ts
ViewVC logotype

Annotation of /branches/6.x/src/utils/embed.ts

Parent Directory Parent Directory | Revision Log Revision Log


Revision 577 - (hide annotations)
Mon Jul 29 18:52:37 2024 UTC (8 months ago) by rakinar2
File MIME type: application/typescript
File size: 3652 byte(s)
chore: add old version archive branches (2.x to 9.x-dev)
1 rakinar2 577 /*
2     * This file is part of SudoBot.
3     *
4     * Copyright (C) 2021-2023 OSN Developers.
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 { Channel, ChannelType, ColorResolvable, EmbedBuilder, Guild, Message, User, resolveColor } from "discord.js";
21     import { ChatInputCommandContext } from "../services/CommandManager";
22    
23     export function generateEmbed(options: ChatInputCommandContext["options"]) {
24     const getString = (field: string): string | undefined => {
25     return options.getString(field) ?? undefined;
26     };
27    
28     const author = {
29     name: getString("author_name")!,
30     iconURL: getString("author_iconurl")
31     };
32    
33     const footer = {
34     text: getString("footer_text")!,
35     iconURL: getString("footer_iconurl")
36     };
37    
38     try {
39     if (
40     getString("color") &&
41     (!resolveColor(getString("color") as ColorResolvable) || isNaN(resolveColor(getString("color") as ColorResolvable)))
42     ) {
43     throw new Error();
44     }
45     } catch (e) {
46     return { error: "Invalid color given." };
47     }
48    
49     const embed = new EmbedBuilder({
50     author: author.name ? author : undefined,
51     title: getString("title"),
52     description: getString("description"),
53     thumbnail: getString("thumbnail")
54     ? {
55     url: getString("thumbnail")!
56     }
57     : undefined,
58     image: getString("image")
59     ? {
60     url: getString("image")!
61     }
62     : undefined,
63     video: getString("video")
64     ? {
65     url: getString("video")!
66     }
67     : undefined,
68     footer: footer.text ? footer : undefined,
69     timestamp: getString("timestamp")
70     ? getString("timestamp") === "current"
71     ? new Date()
72     : new Date(getString("timestamp")!)
73     : undefined,
74     fields: getString("fields")
75     ? getString("fields")!
76     .trim()
77     .split(",")
78     .map(fieldData => {
79     const [name, value] = fieldData.trim().split(":");
80    
81     return {
82     name: name.trim(),
83     value: value.trim()
84     };
85     })
86     : [],
87     url: getString("url")
88     });
89    
90     const color = getString("color");
91    
92     if (color) {
93     embed.setColor(color as ColorResolvable);
94     }
95    
96     return { embed };
97     }
98    
99     export function userInfo(user: User) {
100     return `ID: ${user.id}\nUsername: ${user.username}\nMention: ${user.toString()}`;
101     }
102    
103     export function messageInfo(message: Message) {
104     return `ID: ${message.id}\nURL: ${message.url}`;
105     }
106    
107     export function channelInfo(channel: Channel) {
108     return `ID: ${channel.id}\nType: ${ChannelType[channel.type]}\nMention: ${channel.toString()}`;
109     }
110    
111     export function guildInfo(guild: Guild) {
112     return `ID: ${guild.id}\nName: ${guild.name}\nInvite: ${guild.invites.cache.first()?.url ?? "*Unavailable*"}`;
113     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26