Skip to content

Commit ae0bf3e

Browse files
committed
Fix mentions for articles, change layout a little more
1 parent b3a5c20 commit ae0bf3e

2 files changed

Lines changed: 38 additions & 47 deletions

File tree

src/commands/external/ArticleCommand.js

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import Command from '../Command.js';
22
import GuildSettings from '../../settings/GuildSettings.js';
33
import {
4-
ActionRowBuilder,
54
ButtonBuilder,
65
ButtonStyle,
76
MessageFlags,
@@ -165,54 +164,41 @@ export default class ArticleCommand extends Command {
165164
const message = new MessageBuilder();
166165

167166
if (mention) {
168-
message.content = `${userMention(mention)} this article from our help center might help you:`;
167+
message.text(`${userMention(mention)} this article from our help center might help you:`)
168+
.separator(false);
169169
}
170170

171-
const container = this.appendContent(message, results[index], article.body)
172-
.endComponent()
173-
.addSeparatorComponents()
174-
.addActionRowComponents(
175-
new ActionRowBuilder().addComponents(
176-
new StringSelectMenuBuilder()
177-
.setOptions(results)
178-
.setCustomId(`article:${userId}` + (mention ? `:${mention}` : ''))
179-
),
180-
new ActionRowBuilder().addComponents(
181-
new ButtonBuilder()
182-
.setStyle(ButtonStyle.Link)
183-
.setURL(article.html_url)
184-
.setLabel('View Article')
185-
),
171+
let string = this.#markdownConverter.generate(article.body);
172+
let shorten = string.length > ARTICLE_EMBED_PREVIEW_LENGTH;
173+
if (shorten) {
174+
string = string.substring(0, ARTICLE_EMBED_PREVIEW_LENGTH)
175+
.replace(/\n+[^\n]*$/, '');
176+
}
177+
178+
message
179+
.heading(article.title)
180+
.text(string)
181+
.separator()
182+
.select(new StringSelectMenuBuilder()
183+
.setOptions(results)
184+
.setCustomId(`article:${userId}` + (mention ? `:${mention}` : ''))
186185
)
187-
.toJSON();
186+
.separator();
188187

189-
return {
190-
components: [container],
191-
flags: MessageFlags.IsComponentsV2,
192-
};
193-
}
194188

195-
/**
196-
* get a description from the HTML body of an article
197-
* @param {MessageBuilder} message
198-
* @param {import('discord.js').APISelectMenuOption} result
199-
* @param {string} body website body
200-
* @returns {MessageBuilder}
201-
*/
202-
appendContent(message, result, body) {
203-
message.heading(result.label);
204-
205-
let string = this.#markdownConverter.generate(body);
206-
if (string.length > ARTICLE_EMBED_PREVIEW_LENGTH) {
207-
string = string.substring(0, ARTICLE_EMBED_PREVIEW_LENGTH);
208-
message.text(string.replace(/\.?\n+.*$/, ''))
209-
.newLine()
210-
.newLine()
211-
.subtext('To read more, click \'View Article\' below.');
212-
} else {
213-
message.text(string);
189+
if (shorten) {
190+
message.subtext('This is only a short preview of the article, Click \'View Article\' to read more.');
214191
}
215192

216-
return message;
193+
message.button(new ButtonBuilder()
194+
.setStyle(ButtonStyle.Link)
195+
.setURL(article.html_url)
196+
.setLabel('View Article')
197+
);
198+
199+
return {
200+
components: [message.endComponent()],
201+
flags: MessageFlags.IsComponentsV2,
202+
};
217203
}
218204
}

src/formatting/MessageBuilder.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ export default class MessageBuilder {
211211
*/
212212
endComponent() {
213213
if (this.#content) {
214-
// noinspection JSCheckFunctionSignatures
215214
this.#container.addTextDisplayComponents(new TextDisplayBuilder().setContent(this.#content));
216215
this.#content = '';
217216
}
@@ -224,7 +223,6 @@ export default class MessageBuilder {
224223
* @returns {MessageBuilder}
225224
*/
226225
separator(divider = true, spacing = SeparatorSpacingSize.Small) {
227-
// noinspection JSCheckFunctionSignatures
228226
this.endComponent().addSeparatorComponents(new SeparatorBuilder().setDivider(divider).setSpacing(spacing));
229227
return this;
230228
}
@@ -234,7 +232,15 @@ export default class MessageBuilder {
234232
* @returns {MessageBuilder}
235233
*/
236234
button(...builder) {
237-
// noinspection JSCheckFunctionSignatures
235+
this.endComponent().addActionRowComponents(new ActionRowBuilder().addComponents(...builder));
236+
return this;
237+
}
238+
239+
/**
240+
* @param {import('discord.js').SelectMenuBuilder} builder
241+
* @returns {MessageBuilder}
242+
*/
243+
select(...builder) {
238244
this.endComponent().addActionRowComponents(new ActionRowBuilder().addComponents(...builder));
239245
return this;
240246
}
@@ -244,7 +250,6 @@ export default class MessageBuilder {
244250
* @returns {MessageBuilder}
245251
*/
246252
image(...builder) {
247-
// noinspection JSCheckFunctionSignatures
248253
this.endComponent().addMediaGalleryComponents(new MediaGalleryBuilder().addItems(...builder));
249254
return this;
250255
}

0 commit comments

Comments
 (0)