Skip to content

Commit

Permalink
add option to mention a user when running the article or video command
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Nov 2, 2022
1 parent 0f5ace9 commit 775e269
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
34 changes: 20 additions & 14 deletions src/commands/external/ArticleCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
codeBlock,
EmbedBuilder,
escapeBold,
SelectMenuBuilder,
SelectMenuBuilder, userMention,
} from 'discord.js';
import Turndown from 'turndown';
import icons from '../../util/icons.js';
Expand All @@ -31,12 +31,15 @@ export default class ArticleCommand extends Command {
}

buildOptions(builder) {
builder
.addStringOption(option =>
option.setName('query')
.setDescription('Search query')
.setRequired(true)
.setAutocomplete(true));
builder.addStringOption(option =>
option.setName('query')
.setDescription('Search query')
.setRequired(true)
.setAutocomplete(true));
builder.addUserOption(option =>
option.setName('user')
.setDescription('The user you\'re replying to. This user will be mentioned in the Message.')
.setRequired(false));
return builder;
}

Expand Down Expand Up @@ -71,11 +74,12 @@ export default class ArticleCommand extends Command {
};
}).slice(0, SELECT_MENU_OPTIONS_LIMIT);

await interaction.reply(this.generateMessage(results, data.results[0], interaction.user.id));
await interaction.reply(this.generateMessage(results, data.results[0], interaction.user.id, 0, interaction.options.getUser('user')?.id));
}

async executeSelectMenu(interaction) {
if (interaction.user.id !== interaction.customId.split(':')[1]) {
const [, originalAuthor, mentionedUser] = interaction.customId.split(':');
if (interaction.user.id !== originalAuthor) {
await interaction.reply(ErrorEmbed.message('Only the person who executed this command can select a different result'));
return;
}
Expand All @@ -86,7 +90,7 @@ export default class ArticleCommand extends Command {
.findIndex(option => option.value === interaction.values[0]);
const article = await (await GuildSettings.get(interaction.guildId)).getZendesk()
.getArticle(selectMenu.options[index].value);
await interaction.update(this.generateMessage(selectMenu.options, article, interaction.user.id, index));
await interaction.update(this.generateMessage(selectMenu.options, article, interaction.user.id, index, mentionedUser));
}

async complete(interaction) {
Expand Down Expand Up @@ -116,24 +120,26 @@ export default class ArticleCommand extends Command {
/**
* @param {import('discord.js').APISelectMenuOption[]} results
* @param {ZendeskArticle} article
* @param {import('discord.js').Snowflake} userId
* @param {import('discord.js').Snowflake} userId id of the user that executed the command
* @param {number} [index]
* @return {{embeds: EmbedBuilder[], components: ActionRowBuilder[], fetchReply: boolean}}
* @param {?import('discord.js').Snowflake} mention user to mention in the message
* @return {{embeds: EmbedBuilder[], components: ActionRowBuilder[], fetchReply: boolean, content: ?string}}
*/
generateMessage(results, article, userId, index = 0) {
generateMessage(results, article, userId, index = 0, mention = null) {
for (const result of results) {
result.default = false;
}
results[index].default = true;

return {
content: mention ? `${userMention(mention)} this article from our help-center might help you:` : null,
embeds: [this.createEmbed(results[index], article.body)],
components: [
new ActionRowBuilder()
.addComponents(
/** @type {any} */ new SelectMenuBuilder()
.setOptions(/** @type {any} */ results)
.setCustomId(`article:${userId}`)
.setCustomId(`article:${userId}` + (mention ? `:${mention}` : ''))
),
new ActionRowBuilder()
.addComponents(
Expand Down
30 changes: 22 additions & 8 deletions src/commands/external/VideoCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Command from '../Command.js';
import GuildSettings from '../../settings/GuildSettings.js';
import {SELECT_MENU_TITLE_LIMIT} from '../../util/apiLimits.js';
import icons from '../../util/icons.js';
import {ActionRowBuilder, SelectMenuBuilder} from 'discord.js';
import {ActionRowBuilder, SelectMenuBuilder, userMention} from 'discord.js';
import ErrorEmbed from '../../embeds/ErrorEmbed.js';
import config from '../../bot/Config.js';
import {componentEmojiIfExists} from '../../util/format.js';
Expand All @@ -15,6 +15,10 @@ export default class VideoCommand extends Command {
.setDescription('Search query')
.setRequired(true)
.setAutocomplete(true));
builder.addUserOption(option =>
option.setName('user')
.setDescription('The user you\'re replying to. This user will be mentioned in the Message.')
.setRequired(false));
return super.buildOptions(builder);
}

Expand Down Expand Up @@ -59,12 +63,14 @@ export default class VideoCommand extends Command {
await interaction.reply(this.generateMessage(
results,
interaction.user.id,
0
0,
interaction.options.getUser('user')?.id,
));
}

async executeSelectMenu(interaction) {
if (interaction.user.id !== interaction.customId.split(':')[1]) {
const [, originalAuthor, mentionedUser] = interaction.customId.split(':');
if (interaction.user.id !== originalAuthor) {
await interaction.reply(ErrorEmbed.message('Only the person who executed this command can select a different result'));
return;
}
Expand All @@ -76,30 +82,38 @@ export default class VideoCommand extends Command {
await interaction.update(this.generateMessage(
selectMenu.options,
interaction.user.id,
index
index,
mentionedUser,
));
}

/**
* @param {import('discord.js').APISelectMenuOption[]} videos
* @param {import('discord.js').Snowflake} userId
* @param {import('discord.js').Snowflake} userId id of the user that executed this command
* @param {number} [index]
* @param {?import('discord.js').Snowflake} mention user to mention in the message
* @return {{content: string, components: ActionRowBuilder[], fetchReply: boolean}}
*/
generateMessage(videos, userId, index = 0) {
generateMessage(videos, userId, index = 0, mention = null) {
for (const result of videos) {
result.default = false;
}
videos[index].default = true;

let content = `https://youtu.be/${videos[index].value}`;

if (mention) {
content = `${userMention(mention)} this video might help you:\n` + content;
}

return {
content: `https://youtu.be/${videos[index].value}`,
content,
components: [
new ActionRowBuilder()
.addComponents(
/** @type {any} */ new SelectMenuBuilder()
.setOptions(/** @type {any} */ videos)
.setCustomId(`video:${userId}`)
.setCustomId(`video:${userId}` + (mention ? `:${mention}` : ''))
),
],
fetchReply: true,
Expand Down

0 comments on commit 775e269

Please sign in to comment.