Skip to content

Commit 16af225

Browse files
committed
feat(discord): add custom fields
1 parent 0dc57b6 commit 16af225

File tree

1 file changed

+24
-18
lines changed

1 file changed

+24
-18
lines changed

src/integrations/discord.ts

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ import {
4040
import { check } from '~/helpers/permissions/check.js';
4141
import { get as getPermission } from '~/helpers/permissions/get.js';
4242
import * as changelog from '~/helpers/user/changelog.js';
43+
import getBotId from '~/helpers/user/getBotId.js';
44+
import getBotUserName from '~/helpers/user/getBotUserName.js';
4345
import { Types } from '~/plugins/ListenTo.js';
4446
import { getIdFromTwitch } from '~/services/twitch/calls/getIdFromTwitch.js';
4547
import { variables as vars } from '~/watchers.js';
@@ -89,6 +91,9 @@ class Discord extends Integration {
8991
@settings('bot')
9092
fields: string[] = ['$game', '$title', '$tags', '$startedAt', '$uptime', '$viewers', '$followers', '$subscribers'];
9193

94+
@settings('bot')
95+
customFields: { name: string, value: string }[] = [];
96+
9297
@settings('bot')
9398
fieldsDisabled: string[] = [''];
9499

@@ -113,7 +118,7 @@ class Discord extends Integration {
113118
@settings('bot')
114119
deleteMessagesAfterWhile = false;
115120

116-
generateEmbed(isOnline: boolean) {
121+
async generateEmbed(isOnline: boolean) {
117122
const broadcasterUsername = variables.get('services.twitch.broadcasterUsername') as string;
118123
const profileImageUrl = variables.get('services.twitch.profileImageUrl') as string;
119124

@@ -122,10 +127,10 @@ class Discord extends Integration {
122127
? `${broadcasterUsername.charAt(0).toUpperCase() + broadcasterUsername.slice(1)} started stream! Check it out!`
123128
: `${broadcasterUsername.charAt(0).toUpperCase() + broadcasterUsername.slice(1)} is not streaming anymore! Check it next time!`;
124129

125-
const fields = this.fields
130+
const fields = await Promise.all(this.fields
126131
.filter((o) => this.filterFields(o, isOnline))
127-
.map((o) => this.prepareFields(o, isOnline))
128-
.filter((o) => o !== null);
132+
.map((o) => this.prepareFields(o, isOnline)),
133+
);
129134

130135
return new DiscordJs.EmbedBuilder()
131136
.setURL('https://twitch.tv/' + broadcasterUsername)
@@ -160,7 +165,7 @@ class Discord extends Integration {
160165
debug('discord.embed', `Trying to update message ${this.embedMessageId}.`);
161166
if (message) {
162167
debug('discord.embed', `Updating message ${this.embedMessageId}.`);
163-
message.edit({ embeds: [this.generateEmbed(true)] })
168+
message.edit({ embeds: [await this.generateEmbed(true)] })
164169
.then(() => debug('discord.embed', `Message ${this.embedMessageId} was updated.`))
165170
.catch((e) => debug('discord.embed', e));
166171
} else {
@@ -337,7 +342,7 @@ class Discord extends Integration {
337342
debug('discord.embed', `Trying to update message ${this.embedMessageId}.`);
338343
if (message) {
339344
debug('discord.embed', `Updating message ${this.embedMessageId}.`);
340-
message.edit({ embeds: [this.generateEmbed(false)] })
345+
message.edit({ embeds: [await this.generateEmbed(false)] })
341346
.then(() => debug('discord.embed', `Message ${this.embedMessageId} was updated.`))
342347
.catch((e) => debug('discord.embed', e));
343348
} else {
@@ -358,7 +363,7 @@ class Discord extends Integration {
358363
}
359364

360365
if (!isOnline) {
361-
if (['$viewers', '$followers', '$subscribers'].includes(o)) {
366+
if (['$viewers', '$followers', '$subscribers', '$uptime'].includes(o)) {
362367
return false;
363368
}
364369
}
@@ -374,24 +379,25 @@ class Discord extends Integration {
374379
return true;
375380
}
376381

377-
prepareFields(o: string, isOnline: boolean) {
382+
async prepareFields(o: string, isOnline: boolean) {
383+
if (o.startsWith('$custom')) {
384+
if (this.customFields.length > 0) {
385+
const index = Number(o.replace('$custom', ''));
386+
if (this.customFields[index]) {
387+
const value = await new Message(this.customFields[index].value).parse({ sender: getUserSender(getBotId(), getBotUserName()), discord: undefined }) as string;
388+
return { name: this.customFields[index].name, value };
389+
}
390+
}
391+
}
378392
if (o === '$game') {
379393
return { name: prepare('webpanel.responses.variable.game'), value: stats.value.currentGame ?? '' };
380394
}
381395
if (o === '$title') {
382396
return { name: prepare('webpanel.responses.variable.title'), value: stats.value.currentTitle ?? '' };
383397
}
384-
if (o === '$tags') {
385-
return { name: prepare('webpanel.responses.variable.tags'), value: `${(stats.value.currentTags ?? []).map(tag => `${tag}`).join(', ')}` };
386-
}
387398
if (o === '$uptime') {
388399
const uptime = vars.get('services.twitch.uptime') as number;
389-
390-
if (isOnline) {
391-
return { name: capitalize(prepare('webpanel.uptime')), value: getTime(Date.now() - uptime, true), inline: true };
392-
} else {
393-
return null;
394-
}
400+
return { name: capitalize(prepare('webpanel.uptime')), value: getTime(Date.now() - uptime, true), inline: true };
395401
}
396402
if (o === '$startedAt') {
397403
if (isOnline) {
@@ -425,7 +431,7 @@ class Discord extends Integration {
425431
// Send the embed to the same channel as the message
426432
const message = await (channel as DiscordJs.TextChannel).send({
427433
content: this.onlineAnnounceMessage.length > 0 ? this.onlineAnnounceMessage : undefined,
428-
embeds: [this.generateEmbed(true)],
434+
embeds: [await this.generateEmbed(true)],
429435
});
430436
this.embedMessageId = message.id;
431437
chatOut(`#${(channel as DiscordJs.TextChannel).name}: [[online announce embed]] [${this.client.user?.tag}]`);

0 commit comments

Comments
 (0)