Skip to content

Commit

Permalink
Create reusable classes for modal fields
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Jun 3, 2024
1 parent 724f652 commit 6684251
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 240 deletions.
49 changes: 14 additions & 35 deletions src/commands/user/BanCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
ActionRowBuilder,
ModalBuilder,
PermissionFlagsBits,
PermissionsBitField,
TextInputBuilder,
TextInputStyle
} from 'discord.js';
import MemberWrapper from '../../discord/MemberWrapper.js';
import {parseTime} from '../../util/timeutils.js';
Expand All @@ -15,6 +12,15 @@ import Confirmation from '../../database/Confirmation.js';
import UserActionEmbed from '../../embeds/UserActionEmbed.js';
import config from '../../bot/Config.js';
import {deferReplyOnce, replyOrEdit} from '../../util/interaction.js';
import ReasonInput from '../../modals/inputs/ReasonInput.js';
import CommentInput from '../../modals/inputs/CommentInput.js';
import DeleteMessageHistoryInput from '../../modals/inputs/DeleteMessageHistoryInput.js';
import DurationInput from '../../modals/inputs/DurationInput.js';

/**
* @typedef {DurationConfirmationData} BanConfirmationData
* @property {?number} deleteMessageTime
*/

export default class BanCommand extends UserCommand {

Expand Down Expand Up @@ -92,7 +98,7 @@ export default class BanCommand extends UserCommand {
async executeButton(interaction) {
const parts = interaction.customId.split(':');
if (parts[1] === 'confirm') {
/** @type {Confirmation<{reason: ?string, comment: ?string, duration: ?number, deleteMessageTime: ?number, user: import('discord.js').Snowflake}>}*/
/** @type {Confirmation<BanConfirmationData>}*/
const data = await Confirmation.get(parts[2]);
if (!data) {
await interaction.update({content: 'This confirmation has expired.', embeds: [], components: []});
Expand Down Expand Up @@ -132,37 +138,10 @@ export default class BanCommand extends UserCommand {
.setTitle(`Ban ${await member.displayName()}`.substring(0, MODAL_TITLE_LIMIT))
.setCustomId(`ban:${member.user.id}`)
.addComponents(
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Reason')
.setCustomId('reason')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No reason provided')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Comment')
.setCustomId('comment')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No internal comment')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Duration')
.setCustomId('duration')
.setStyle(TextInputStyle.Short)),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Delete message history')
.setCustomId('delete')
.setStyle(TextInputStyle.Short)
.setValue('1 hour')),
new ReasonInput().toActionRow(),
new CommentInput().toActionRow(),
new DurationInput().toActionRow(),
new DeleteMessageHistoryInput().toActionRow(),
));
}

Expand Down
25 changes: 5 additions & 20 deletions src/commands/user/KickCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
ActionRowBuilder,
ModalBuilder,
PermissionFlagsBits,
PermissionsBitField,
TextInputBuilder,
TextInputStyle
} from 'discord.js';
import MemberWrapper from '../../discord/MemberWrapper.js';
import colors from '../../util/colors.js';
Expand All @@ -14,6 +11,8 @@ import Confirmation from '../../database/Confirmation.js';
import UserActionEmbed from '../../embeds/UserActionEmbed.js';
import config from '../../bot/Config.js';
import {deferReplyOnce, replyOrEdit} from '../../util/interaction.js';
import ReasonInput from '../../modals/inputs/ReasonInput.js';
import CommentInput from '../../modals/inputs/CommentInput.js';

export default class KickCommand extends UserCommand {

Expand Down Expand Up @@ -65,7 +64,7 @@ export default class KickCommand extends UserCommand {
async executeButton(interaction) {
const parts = interaction.customId.split(':');
if (parts[1] === 'confirm') {
/** @type {Confirmation<{reason: ?string, comment: ?string, user: import('discord.js').Snowflake}>}*/
/** @type {Confirmation<ConfirmationData>}*/
const data = await Confirmation.get(parts[2]);
if (!data) {
await interaction.update({content: 'This confirmation has expired.', embeds: [], components: []});
Expand Down Expand Up @@ -103,22 +102,8 @@ export default class KickCommand extends UserCommand {
.setTitle(`Kick ${await member.displayName()}`.substring(0, MODAL_TITLE_LIMIT))
.setCustomId(`kick:${member.user.id}`)
.addComponents(
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Reason')
.setCustomId('reason')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No reason provided')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Comment')
.setCustomId('comment')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No internal comment')),
new ReasonInput().toActionRow(),
new CommentInput().toActionRow(),
));
}

Expand Down
34 changes: 7 additions & 27 deletions src/commands/user/MuteCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
ActionRowBuilder,
ModalBuilder,
PermissionFlagsBits,
PermissionsBitField,
TextInputBuilder,
TextInputStyle
} from 'discord.js';
import MemberWrapper from '../../discord/MemberWrapper.js';
import {formatTime, parseTime} from '../../util/timeutils.js';
Expand All @@ -16,6 +13,9 @@ import ErrorEmbed from '../../embeds/ErrorEmbed.js';
import UserActionEmbed from '../../embeds/UserActionEmbed.js';
import config from '../../bot/Config.js';
import {deferReplyOnce, replyOrEdit} from '../../util/interaction.js';
import ReasonInput from '../../modals/inputs/ReasonInput.js';
import CommentInput from '../../modals/inputs/CommentInput.js';
import DurationInput from '../../modals/inputs/DurationInput.js';

export default class MuteCommand extends UserCommand {

Expand Down Expand Up @@ -97,7 +97,7 @@ export default class MuteCommand extends UserCommand {
async executeButton(interaction) {
const parts = interaction.customId.split(':');
if (parts[1] === 'confirm') {
/** @type {Confirmation<{reason: ?string, comment: ?string, duration: ?number, user: import('discord.js').Snowflake}>}*/
/** @type {Confirmation<DurationConfirmationData>}*/
const data = await Confirmation.get(parts[2]);
if (!data) {
await interaction.update({content: 'This confirmation has expired.', embeds: [], components: []});
Expand Down Expand Up @@ -137,29 +137,9 @@ export default class MuteCommand extends UserCommand {
.setTitle(`Mute ${await member.displayName()}`.substring(0, MODAL_TITLE_LIMIT))
.setCustomId(`mute:${member.user.id}`)
.addComponents(
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Reason')
.setCustomId('reason')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No reason provided')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Comment')
.setCustomId('comment')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No internal comment')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Duration')
.setCustomId('duration')
.setStyle(TextInputStyle.Short)),
new ReasonInput().toActionRow(),
new CommentInput().toActionRow(),
new DurationInput().toActionRow(),
));
}

Expand Down
36 changes: 8 additions & 28 deletions src/commands/user/PardonCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import {
ActionRowBuilder, bold, escapeMarkdown,
bold, escapeMarkdown,
ModalBuilder,
PermissionFlagsBits,
PermissionsBitField,
TextInputBuilder,
TextInputStyle
PermissionsBitField
} from 'discord.js';
import MemberWrapper from '../../discord/MemberWrapper.js';
import colors from '../../util/colors.js';
Expand All @@ -14,6 +12,9 @@ import EmbedWrapper from '../../embeds/EmbedWrapper.js';
import {formatNumber, inlineEmojiIfExists} from '../../util/format.js';
import {deferReplyOnce, replyOrEdit} from '../../util/interaction.js';
import UserCommand from './UserCommand.js';
import ReasonInput from '../../modals/inputs/ReasonInput.js';
import CommentInput from '../../modals/inputs/CommentInput.js';
import CountInput from '../../modals/inputs/CountInput.js';

export default class PardonCommand extends UserCommand {

Expand Down Expand Up @@ -99,30 +100,9 @@ export default class PardonCommand extends UserCommand {
.setTitle(`Pardon ${await member.displayName()}`.substring(0, MODAL_TITLE_LIMIT))
.setCustomId(`pardon:${member.user.id}`)
.addComponents(
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Reason')
.setCustomId('reason')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No reason provided')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Comment')
.setCustomId('comment')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No internal comment')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Count')
.setCustomId('count')
.setStyle(TextInputStyle.Short)
.setPlaceholder('1')),
new ReasonInput().toActionRow(),
new CommentInput().toActionRow(),
new CountInput().toActionRow(),
));
}

Expand Down
40 changes: 12 additions & 28 deletions src/commands/user/SoftBanCommand.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {
ActionRowBuilder,
ModalBuilder,
PermissionFlagsBits,
PermissionsBitField,
TextInputBuilder,
TextInputStyle
} from 'discord.js';
import MemberWrapper from '../../discord/MemberWrapper.js';
import {parseTime} from '../../util/timeutils.js';
Expand All @@ -15,6 +12,14 @@ import Confirmation from '../../database/Confirmation.js';
import UserActionEmbed from '../../embeds/UserActionEmbed.js';
import config from '../../bot/Config.js';
import {deferReplyOnce, replyOrEdit} from '../../util/interaction.js';
import ReasonInput from '../../modals/inputs/ReasonInput.js';
import CommentInput from '../../modals/inputs/CommentInput.js';
import DeleteMessageHistoryInput from '../../modals/inputs/DeleteMessageHistoryInput.js';

/**
* @typedef {ConfirmationData} SoftBanConfirmationData
* @property {?number} deleteMessageTime
*/

export default class SoftBanCommand extends UserCommand {

Expand Down Expand Up @@ -74,7 +79,7 @@ export default class SoftBanCommand extends UserCommand {
async executeButton(interaction) {
const parts = interaction.customId.split(':');
if (parts[1] === 'confirm') {
/** @type {Confirmation<{reason: ?string, comment: ?string, user: import('discord.js').Snowflake, deleteMessageTime: ?number}>}*/
/** @type {Confirmation<SoftBanConfirmationData>}*/
const data = await Confirmation.get(parts[2]);
if (!data) {
await interaction.update({content: 'This confirmation has expired.', embeds: [], components: []});
Expand Down Expand Up @@ -109,30 +114,9 @@ export default class SoftBanCommand extends UserCommand {
.setTitle(`Soft-ban ${await member.displayName()}`.substring(0, MODAL_TITLE_LIMIT))
.setCustomId(`soft-ban:${member.user.id}`)
.addComponents(
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Reason')
.setCustomId('reason')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No reason provided')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Comment')
.setCustomId('comment')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No internal comment')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Delete message history')
.setCustomId('delete')
.setStyle(TextInputStyle.Short)
.setValue('1 hour')),
new ReasonInput().toActionRow(),
new CommentInput().toActionRow(),
new DeleteMessageHistoryInput().toActionRow(),
));
}

Expand Down
40 changes: 12 additions & 28 deletions src/commands/user/StrikeCommand.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import {
ActionRowBuilder,
bold,
escapeMarkdown,
ModalBuilder,
PermissionFlagsBits,
PermissionsBitField,
TextInputBuilder,
TextInputStyle
} from 'discord.js';
import MemberWrapper from '../../discord/MemberWrapper.js';
import colors from '../../util/colors.js';
Expand All @@ -17,6 +14,14 @@ import {inLimits} from '../../util/util.js';
import EmbedWrapper from '../../embeds/EmbedWrapper.js';
import {formatNumber, inlineEmojiIfExists} from '../../util/format.js';
import {deferReplyOnce, replyOrEdit} from '../../util/interaction.js';
import ReasonInput from '../../modals/inputs/ReasonInput.js';
import CommentInput from '../../modals/inputs/CommentInput.js';
import CountInput from '../../modals/inputs/CountInput.js';

/**
* @typedef {ConfirmationData} StrikeConfirmationData
* @property {?number} count
*/

export default class StrikeCommand extends UserCommand {

Expand Down Expand Up @@ -91,7 +96,7 @@ export default class StrikeCommand extends UserCommand {
async executeButton(interaction) {
const parts = interaction.customId.split(':');
if (parts[1] === 'confirm') {
/** @type {Confirmation<{reason: ?string, comment: ?string, count: number, user: import('discord.js').Snowflake}>}*/
/** @type {Confirmation<StrikeConfirmationData>}*/
const data = await Confirmation.get(parts[2]);
if (!data) {
await interaction.update({content: 'This confirmation has expired.', embeds: [], components: []});
Expand Down Expand Up @@ -131,30 +136,9 @@ export default class StrikeCommand extends UserCommand {
.setTitle(`Strike ${await member.displayName()}`.substring(0, MODAL_TITLE_LIMIT))
.setCustomId(`strike:${member.user.id}`)
.addComponents(
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Reason')
.setCustomId('reason')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No reason provided')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Comment')
.setCustomId('comment')
.setStyle(TextInputStyle.Paragraph)
.setPlaceholder('No internal comment')),
/** @type {*} */
new ActionRowBuilder()
.addComponents(/** @type {*} */ new TextInputBuilder()
.setRequired(false)
.setLabel('Count')
.setCustomId('count')
.setStyle(TextInputStyle.Short)
.setPlaceholder('1')),
new ReasonInput().toActionRow(),
new CommentInput().toActionRow(),
new CountInput().toActionRow(),
));
}

Expand Down
Loading

0 comments on commit 6684251

Please sign in to comment.