-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathinfo.ts
197 lines (179 loc) · 6.26 KB
/
info.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import {
type CommandInteraction,
type Guild,
GuildPremiumTier,
type APIEmbed,
ButtonStyle,
SlashCommandBuilder,
ComponentType,
} from "discord.js";
import type { ApplicationCommand, MessageCommand } from "@/commands/command.js";
import type { ProcessableMessage } from "@/service/command.js";
import type { BotContext } from "@/context.js";
import assertNever from "@/utils/assertNever.js";
interface GitHubContributor {
login: string;
id: number;
html_url: string;
type: "User" | "Bot";
contributions: number;
}
const fetchContributions = async (): Promise<GitHubContributor[]> => {
return fetch("https://api.github.com/repos/NullDev/CSZ-Bot/contributors", {
headers: { Accept: "application/vnd.github.v3+json" },
}).then(res => res.json() as Promise<GitHubContributor[]>);
};
const fetchLanguages = async (): Promise<string[]> => {
const res = await fetch("https://api.github.com/repos/NullDev/CSZ-Bot/languages", {
headers: { Accept: "application/vnd.github.v3+json" },
});
return Object.keys((await res.json()) as object);
};
const getContributors = async (): Promise<string> => {
const contributors = await fetchContributions();
return `${contributors
.filter(c => c.type === "User")
.map(c => {
return c.login.replace("-", "‑"); // Replace normal hyphen with no-breaking hypen
})
.join(
", ",
)} | [[Auf GitHub ansehen]](https://github.com/NullDev/CSZ-Bot/graphs/contributors)`;
};
const getTechStackInfo = async (): Promise<string> => {
return (
`**Programmiersprache\n** ${(await fetchLanguages()).join(",")} \n` +
`**NodeJS Version\n** ${process.version} \n`
);
};
const getSystemInfo = (): string => {
return (
`**PID\n** ${process.pid} \n` +
`**Uptime\n** ${Math.floor(process.uptime())}s \n` +
`**Platform\n** ${process.platform} \n` +
`**System CPU usage time\n** ${process.cpuUsage().system} \n` +
`**User CPU usage time\n** ${process.cpuUsage().user} \n` +
`**Architecture\n** ${process.arch}`
);
};
const getServerLevel = (guild: Guild) => {
switch (guild.premiumTier) {
case GuildPremiumTier.None:
return 0;
case GuildPremiumTier.Tier1:
return 1;
case GuildPremiumTier.Tier2:
return 2;
case GuildPremiumTier.Tier3:
return 3;
default:
return assertNever(guild.premiumTier);
}
};
const getServerInfo = (guild: Guild): string => {
const birthday = Intl.DateTimeFormat("de-DE").format(guild.joinedTimestamp);
const level = getServerLevel(guild);
return (
// biome-ignore lint/style/useTemplate: Better readability
`**Mitglieder\n** ${guild.memberCount} / ${guild.maximumMembers} \n` +
`**Oberbabo\n** <@!${guild.ownerId}> \n` +
`**Geburtstag\n** ${birthday} \n` +
`**Boosts\n** ${guild.premiumSubscriptionCount} (Level: ${level}) \n` +
"**Invite\n** https://discord.gg/csz"
);
};
const buildEmbed = async (guild: Guild | null, avatarUrl?: string): Promise<APIEmbed> => {
const now = new Date();
const embed = {
color: 0x1ea188,
footer: {
text: `${now.toDateString()} ${now.toLocaleTimeString()}`,
},
author: {
name: "Shitpost Bot",
url: "https://discordapp.com/users/663146938811547660/",
icon_url: avatarUrl,
},
fields: [
{
name: "🪛 Contributors",
value: await getContributors(),
inline: false,
},
{
name: "🧬 Tech-Stack",
value: await getTechStackInfo(),
inline: true,
},
{
name: "⚙️ System",
value: getSystemInfo(),
inline: true,
},
],
};
if (guild) {
embed.fields.push({
name: "👑 Server",
value: getServerInfo(guild),
inline: true,
});
}
return embed;
};
/**
* Info command. Displays some useless information about the bot.
*
* This command is both - a slash command (application command) and a message command
*/
export default class InfoCommand implements ApplicationCommand, MessageCommand {
name = "info";
description = "Listet Informationen über diesen Bot in einem Embed auf";
// Every Application command would have this structure at minimal. However
// we don't enforce to use the name from the constructor, but highly encourage it
// since the command handler is based on that.
applicationCommand = new SlashCommandBuilder()
.setName(this.name)
.setDescription(this.description);
/**
* Replies to the interaction with the info embed as ephemeral reply
* @param command interaction
* @param client client
* @returns info reply
*/
async handleInteraction(command: CommandInteraction, context: BotContext) {
const embed = await buildEmbed(command.guild, context.client.user.avatarURL() ?? undefined);
await command.reply({
embeds: [embed],
ephemeral: true,
components: [
{
type: ComponentType.ActionRow,
components: [
{
type: ComponentType.Button,
url: "https://github.com/NullDev/CSZ-Bot",
label: "GitHub",
style: ButtonStyle.Link,
disabled: false,
},
],
},
],
});
}
/**
* Replies to the message with the info embed and reacts to the message
* @param message message
* @param client client
* @returns reply and reaction
*/
async handleMessage(message: ProcessableMessage, context: BotContext) {
const embed = await buildEmbed(message.guild, context.client.user.avatarURL() ?? undefined);
const reply = message.reply({
embeds: [embed],
});
const reaction = message.react("⚙️");
await Promise.all([reply, reaction]);
}
}