Skip to content

Commit e4c05fa

Browse files
committed
show interactive text and use component builder
1 parent a41742c commit e4c05fa

File tree

1 file changed

+47
-72
lines changed

1 file changed

+47
-72
lines changed

app/services/gamebridge/payloads/StatusPayload.ts

Lines changed: 47 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ export default class StatusPayload extends Payload {
8181
} = payload.data;
8282
const { bridge, discord } = server;
8383
const {
84-
config: { port },
84+
config: { port, url },
8585
} = bridge.webApp;
86+
const statusApiUri = `http://0.0.0.0:${port}/server-status/${server.config.id}/${Date.now()}`;
8687
const Steam = await bridge.container.getService("Steam");
8788

8889
const updateStatus = async () => {
@@ -205,76 +206,58 @@ export default class StatusPayload extends Payload {
205206
}
206207
}
207208

208-
const container = {
209-
type: Discord.ComponentType.Container,
210-
accent_color:
211-
current_defcon === 1 || current_countdown
212-
? 0xff0000
213-
: current_gamemode
214-
? (gamemodeExtras?.color ?? null)
215-
: null,
216-
components: [
217-
{
218-
type: Discord.ComponentType.Section,
219-
components: [
220-
{
221-
type: Discord.ComponentType.TextDisplay,
222-
content: desc,
223-
},
224-
],
225-
accessory: {
226-
type: Discord.ComponentType.Thumbnail,
227-
media: {
228-
url: "attachment://map.png",
229-
},
230-
description: current_map,
231-
},
232-
},
233-
],
234-
} as Discord.APIContainerComponent;
209+
const container = new Discord.ContainerBuilder();
210+
211+
container.setAccentColor(
212+
current_defcon === 1 || current_countdown
213+
? 0xff0000
214+
: current_gamemode
215+
? (gamemodeExtras?.color ?? null)
216+
: undefined
217+
);
218+
219+
container.addSectionComponents(section =>
220+
section
221+
.addTextDisplayComponents(text => text.setContent(desc))
222+
.setThumbnailAccessory(accessory =>
223+
accessory.setURL("attachment://map.png").setDescription(current_map)
224+
)
225+
);
235226

236227
if (count > 0) {
237-
container.components.push(
238-
{ type: Discord.ComponentType.Separator },
239-
{
240-
type: Discord.ComponentType.MediaGallery,
241-
items: [
242-
{
243-
media: {
244-
url: "attachment://players.png",
245-
},
246-
},
247-
],
248-
}
228+
container.addSectionComponents(sep => sep);
229+
container.addMediaGalleryComponents(gallery =>
230+
gallery.addItems(item => item.setURL("attachment://players.png"))
231+
);
232+
container.addTextDisplayComponents(text =>
233+
text.setContent(
234+
`[Click here to open an interactive version.](${url}/server-status/${
235+
server.config.id
236+
})`
237+
)
249238
);
250239
}
251240

252-
container.components.push(
253-
{ type: Discord.ComponentType.Separator },
254-
{
255-
type: Discord.ComponentType.ActionRow,
256-
components: [
257-
{
258-
type: Discord.ComponentType.Button,
259-
style: Discord.ButtonStyle.Link,
260-
label: "Connect",
261-
url: `https://metastruct.net/${
241+
container.addSectionComponents(sep => sep);
242+
243+
container.addActionRowComponents(row =>
244+
row.setComponents(
245+
new Discord.ButtonBuilder()
246+
.setStyle(Discord.ButtonStyle.Link)
247+
.setLabel("Connect")
248+
.setURL(
249+
`https://metastruct.net/${
262250
server.config.label ? "join/" + server.config.label : ""
263-
}`,
264-
},
265-
],
266-
}
251+
}`
252+
)
253+
)
267254
);
268255

269256
// footer
270-
container.components.push(
271-
{ type: Discord.ComponentType.Separator },
272-
{
273-
type: Discord.ComponentType.TextDisplay,
274-
content: `-# ${gamemodeName}`,
275-
}
276-
);
257+
container.addSeparatorComponents(sep => sep);
258+
container.addTextDisplayComponents(text => text.setContent(`-# ${gamemodeName}`));
277259

260+
// icons and banners
278261
if (mapThumbnail && server.discordBanner !== mapThumbnail) {
279262
server.changeBanner(mapThumbnail);
280263
}
@@ -293,12 +276,7 @@ export default class StatusPayload extends Payload {
293276
server.mapName = current_map;
294277
server.mapUptime = current_mapUptime;
295278
server.serverUptime = current_serverUptime;
296-
server.status.image =
297-
(
298-
container.components.find(
299-
c => c.type === Discord.ComponentType.MediaGallery
300-
) as Discord.APIMediaGalleryComponent
301-
)?.items[0].media.url ?? null;
279+
server.status.image = statusApiUri;
302280
server.status.mapThumbnail = mapThumbnail;
303281
server.status.players = current_players;
304282
server.workshopMap = current_workshopMap;
@@ -335,16 +313,13 @@ export default class StatusPayload extends Payload {
335313
const message = messages
336314
.filter((msg: Discord.Message) => msg.author.id == discord.user?.id)
337315
.first();
338-
const imageUri = `http://0.0.0.0:${port}/server-status/${
339-
server.config.id
340-
}/${Date.now()}`;
341316

342317
if (message) {
343318
await message
344319
.edit({
345320
components: [container],
346321
files: [
347-
new Discord.AttachmentBuilder(imageUri, {
322+
new Discord.AttachmentBuilder(statusApiUri, {
348323
name: "players.png",
349324
}),
350325
new Discord.AttachmentBuilder(mapThumbnail ?? DEFAULT_THUMBNAIL, {
@@ -359,7 +334,7 @@ export default class StatusPayload extends Payload {
359334
.send({
360335
components: [container],
361336
files: [
362-
new Discord.AttachmentBuilder(imageUri, {
337+
new Discord.AttachmentBuilder(statusApiUri, {
363338
name: "players.png",
364339
}),
365340
new Discord.AttachmentBuilder(mapThumbnail ?? DEFAULT_THUMBNAIL, {

0 commit comments

Comments
 (0)