|
| 1 | +import { Command, Interaction } from 'detritus-client'; |
| 2 | +import { InteractionCallbackTypes, MessageComponentButtonStyles, Permissions } from 'detritus-client/lib/constants'; |
| 3 | +import { Components, ComponentContext, Embed, Markup } from 'detritus-client/lib/utils'; |
| 4 | + |
| 5 | +import { deleteTag } from '../../../api'; |
| 6 | +import { RestResponsesRaw } from '../../../api/types'; |
| 7 | +import { DateMomentLogFormat, EmbedColors } from '../../../constants'; |
| 8 | +import { |
| 9 | + createTimestampMomentFromGuild, |
| 10 | + createUserEmbed, |
| 11 | + createUserString, |
| 12 | + editOrReply, |
| 13 | + fetchMemberOrUserById, |
| 14 | +} from '../../../utils'; |
| 15 | + |
| 16 | + |
| 17 | +export interface CommandArgsBefore { |
| 18 | + force?: boolean, |
| 19 | + tag: false | null | RestResponsesRaw.Tag, |
| 20 | +} |
| 21 | + |
| 22 | +export interface CommandArgs { |
| 23 | + force?: boolean, |
| 24 | + tag: RestResponsesRaw.Tag, |
| 25 | +} |
| 26 | + |
| 27 | +export async function createMessage( |
| 28 | + context: Command.Context | Interaction.InteractionContext, |
| 29 | + args: CommandArgs, |
| 30 | +) { |
| 31 | + const isFromInteraction = (context instanceof Interaction.InteractionContext); |
| 32 | + |
| 33 | + const { tag } = args; |
| 34 | + if (tag.user.id !== context.userId) { |
| 35 | + const hasPermissionsToForceRemove = context.user.isClientOwner || (!tag.global && (context.member && context.member.canManageGuild)); |
| 36 | + |
| 37 | + if (args.force) { |
| 38 | + if (!hasPermissionsToForceRemove) { |
| 39 | + return editOrReply(context, '⚠ Not enough permissions to force remove this tag!'); |
| 40 | + } |
| 41 | + } else { |
| 42 | + if (!hasPermissionsToForceRemove) { |
| 43 | + return editOrReply(context, '⚠ You\'re not the owner of this tag!'); |
| 44 | + } |
| 45 | + |
| 46 | + const embed = createUserEmbed(context.user); |
| 47 | + embed.setColor(EmbedColors.DEFAULT); |
| 48 | + embed.setTitle(`Force Remove Tag?`); |
| 49 | + embed.setDescription(`Tag: ${Markup.codestring(tag.name)}`); |
| 50 | + |
| 51 | + { |
| 52 | + const description: Array<string> = []; |
| 53 | + |
| 54 | + { |
| 55 | + const timestamp = createTimestampMomentFromGuild(tag.created, context.guildId); |
| 56 | + description.push(`**Created**: ${timestamp.fromNow()}`); |
| 57 | + description.push(`**->** ${Markup.spoiler(timestamp.format(DateMomentLogFormat))}`); |
| 58 | + } |
| 59 | + if (tag.edited) { |
| 60 | + const timestamp = createTimestampMomentFromGuild(tag.edited, context.guildId); |
| 61 | + description.push(`**Edited**: ${timestamp.fromNow()}`); |
| 62 | + description.push(`**->** ${Markup.spoiler(timestamp.format(DateMomentLogFormat))}`); |
| 63 | + } |
| 64 | + |
| 65 | + // look through cache, else use the tag object |
| 66 | + description.push(`**Global**: ${(tag.global) ? 'Yes' : 'No'}`); |
| 67 | + description.push(`**NSFW**: ${(tag.nsfw) ? 'Yes' : 'No'}`); |
| 68 | + |
| 69 | + { |
| 70 | + const owner = await fetchMemberOrUserById(context, tag.user.id); |
| 71 | + description.push(`**Owner**: ${createUserString(tag.user.id, owner, 'Deleted User?')}`); |
| 72 | + } |
| 73 | + |
| 74 | + description.push(`**Uses**: ${tag.uses.toLocaleString()}`); |
| 75 | + |
| 76 | + embed.addField('Information', description.join('\n')); |
| 77 | + } |
| 78 | + |
| 79 | + const components = new Components({timeout: 5 * (60 * 1000)}); |
| 80 | + components.createButton({ |
| 81 | + label: 'Continue', |
| 82 | + run: async (ctx: ComponentContext) => { |
| 83 | + if (ctx.userId !== context.userId) { |
| 84 | + return ctx.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE); |
| 85 | + } |
| 86 | + |
| 87 | + await deleteTag(context, {name: tag.name, serverId: context.guildId || context.channelId}); |
| 88 | + embed.setColor(EmbedColors.LOG_CREATION); |
| 89 | + embed.setTitle('Successfully Force Removed Tag'); |
| 90 | + await ctx.editOrRespond({components: [], embed}); |
| 91 | + }, |
| 92 | + }); |
| 93 | + |
| 94 | + components.createButton({ |
| 95 | + label: 'Cancel', |
| 96 | + style: MessageComponentButtonStyles.DANGER, |
| 97 | + run: async (ctx: ComponentContext) => { |
| 98 | + if (ctx.userId !== context.userId) { |
| 99 | + return ctx.respond(InteractionCallbackTypes.DEFERRED_UPDATE_MESSAGE); |
| 100 | + } |
| 101 | + |
| 102 | + embed.setColor(EmbedColors.ERROR); |
| 103 | + embed.setTitle('Cancelled Force Removal of Tag'); |
| 104 | + await ctx.editOrRespond({components: [], embed}); |
| 105 | + }, |
| 106 | + }); |
| 107 | + |
| 108 | + const message = await editOrReply(context, {components, embed}); |
| 109 | + components.onTimeout = async () => { |
| 110 | + try { |
| 111 | + embed.setColor(EmbedColors.ERROR); |
| 112 | + embed.setFooter('Request expired, press a button next time'); |
| 113 | + if (context instanceof Interaction.InteractionContext) { |
| 114 | + await context.editResponse({components: [], embed}); |
| 115 | + } else { |
| 116 | + if (message && message.canEdit) { |
| 117 | + await message.edit({components: [], embed}); |
| 118 | + } |
| 119 | + } |
| 120 | + } catch(error) { |
| 121 | + |
| 122 | + } |
| 123 | + }; |
| 124 | + return message; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + await deleteTag(context, {name: tag.name, serverId: context.guildId || context.channelId}); |
| 129 | + return editOrReply(context, `Removed tag ${tag.name}`); |
| 130 | +} |
0 commit comments