|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org> |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: BSD-2-Clause |
| 5 | + */ |
| 6 | + |
| 7 | +import { ChatInputCommandInteraction, GuildMember, SlashCommandBuilder } from "discord.js"; |
| 8 | +import Command from "./command"; |
| 9 | + |
| 10 | +export class SlapCommand extends Command { |
| 11 | + private readonly baseReply: string = `*$USER slaps $TARGET around a bit with a large trout*`; |
| 12 | + |
| 13 | + override data() { |
| 14 | + const aliases = ["slap"]; |
| 15 | + const description = "Slap someone around a bit with a large trout"; |
| 16 | + |
| 17 | + const baseCommand = new SlashCommandBuilder() |
| 18 | + .setDescription(description) |
| 19 | + .addUserOption(user => user.setName("target").setDescription("The user to slap")); |
| 20 | + |
| 21 | + return aliases.map(name => baseCommand.setName(name).toJSON()); |
| 22 | + } |
| 23 | + |
| 24 | + override async handleCommand(interaction: ChatInputCommandInteraction): Promise<void> { |
| 25 | + if (!interaction.isCommand()) return; |
| 26 | + |
| 27 | + let content = this.baseReply; |
| 28 | + |
| 29 | + const target = interaction.options.getMember("target"); |
| 30 | + const user = interaction.member; |
| 31 | + let username = ""; |
| 32 | + if (user instanceof GuildMember) { |
| 33 | + username = user.displayName; |
| 34 | + } else { |
| 35 | + username = user?.nick ?? user?.user.username ?? "Someone"; |
| 36 | + } |
| 37 | + |
| 38 | + let targetName = ""; |
| 39 | + if (target instanceof GuildMember) { |
| 40 | + targetName = target.displayName; |
| 41 | + } else { |
| 42 | + targetName = target?.nick ?? "Someone"; |
| 43 | + } |
| 44 | + |
| 45 | + if (user) content = content.replace("$USER", `@${username}`); |
| 46 | + if (target) content = content.replace("$TARGET", `@${targetName}`); |
| 47 | + |
| 48 | + await interaction.reply({ |
| 49 | + content, |
| 50 | + }); |
| 51 | + } |
| 52 | +} |
0 commit comments