all repos — discord-utils @ 492186c2891cfcdb565eed0850083c9d6f577d4b

src/commands/ban.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
import { Result } from "@sapphire/result";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";

import { parseIncludedGames, parseDuration, createReactive } from "@/utils";
import config, { type Game } from "@/config";
import { ban, getUser } from "@/roblox";
import { kitten } from "@/discord";
import { INTEGRATION_CONTEXT_PRESETS, INTEGRATION_TYPE_PRESETS, option } from "@purrkit/core";

const { games: GAMES } = config;
type State = {
	prefix: string;
	successfulBans: Game[];
	failedBans: Game[];
};

function renderGameButtons(state: State, banningIn: Game[]): ActionRowBuilder<ButtonBuilder> {
	const { successfulBans, failedBans } = state;

	const successfulIds = new Set(successfulBans.map((b) => b.id));
	const failedIds = new Set(failedBans.map((b) => b.id));
	const pendingIds = new Set(banningIn.map((b) => b.id));

	const row = new ActionRowBuilder<ButtonBuilder>();

	for (const game of GAMES) {
		const isSuccessful = successfulIds.has(game.id);
		const isFailed = failedIds.has(game.id);
		const isPending = pendingIds.has(game.id);

		const button = new ButtonBuilder()
			.setLabel(game.name)
			.setDisabled(true)
			.setCustomId(`ban_${game.id}`);

		if (isSuccessful) {
			button.setStyle(ButtonStyle.Success);
			button.setEmoji("✅");
		} else if (isFailed) {
			button.setStyle(ButtonStyle.Danger);
			button.setEmoji("✖️");
		} else if (isPending) {
			button.setStyle(ButtonStyle.Primary);
			button.setEmoji("⏳");
		} else {
			button.setStyle(ButtonStyle.Secondary);
			button.setEmoji("➖");
		}

		row.addComponents(button);
	}

	return row;
}

export const banCommand = kitten.command("ban", {
	description: "ban a user from the specified games",
	contexts: INTEGRATION_CONTEXT_PRESETS.EVERYWHERE,
	integrationTypes: INTEGRATION_TYPE_PRESETS.USER_ONLY,
	options: {
		player: option.string("ID of the player to ban", { required: true }),
		reason: option.string("reason for the ban", { required: true }),
		"private-reason": option.string("private reason for the ban", { required: true }),
		length: option.string("length of the ban; defaults to permanent"),
		"ban-alts": option.boolean("whether to ban alts of the user; defaults to true"),
		games: option.string("games to ban them from; defaults to all"),
	},
	run: async (interaction, options) => {
		await interaction.deferReply({ withResponse: true });

		const gamesResult = Result.from(() => parseIncludedGames(options.games));
		if (gamesResult.isErr()) {
			const err = gamesResult.unwrapErr();
			const message = err instanceof Error ? err.message : "failed to parse included games";

			await interaction.editReply({ content: message });
			return;
		}

		const games = gamesResult.unwrap();
		const user = await getUser(options.player);
		const duration = options.length ? parseDuration(options.length).toString() : undefined;

		const extraButtons = new ActionRowBuilder<ButtonBuilder>().addComponents(
			new ButtonBuilder()
				.setLabel(options.length ? `For ${options.length}` : "Permanently")
				.setStyle(ButtonStyle.Primary)
				.setDisabled(true)
				.setCustomId("duration"),
			new ButtonBuilder()
				.setLabel("View Profile")
				.setStyle(ButtonStyle.Link)
				.setURL(`https://roblox.com/users/${user.id}/profile`),
			new ButtonBuilder()
				.setLabel("Unban")
				.setStyle(ButtonStyle.Secondary)
				.setCustomId(`unban:${user.id}`),
		);

		const details = [
			`> **Reason:** ${options.reason}`,
			`> **Private Reason:** ${options["private-reason"]}`,
		].join("\n");

		const state = createReactive<State>(
			{ successfulBans: [], failedBans: [], prefix: "⏳ Banning" },
			(_newState) => {
				const gameButtons = renderGameButtons(state, games);

				const userParts = [
					`**@${user.name}**`,
					user.displayName ? `(${user.displayName})` : null,
					`id:${user.id}`,
				];
				const userString = userParts.filter(Boolean).join(" ");
				const message = `${state.prefix} ${userString}\n${details}`;

				interaction.editReply({ content: message, components: [gameButtons, extraButtons] });
			},
		);

		const banOptions = {
			duration,
			excludeAltAccounts: !!options["ban-alts"],
			displayReason: options.reason,
			privateReason: options["private-reason"],
		};

		await Promise.all(
			games.map(async (game) => {
				const banResult = await Result.fromAsync(() =>
					ban(user.id.toString(), game.universeId, banOptions),
				);
				if (banResult.isOk()) state.successfulBans.push(game);
				else state.failedBans.push(game);
			}),
		);

		state.prefix = "✅ Banned";
	},
});