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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
import { Command, container, Result } from "@sapphire/framework";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js";
import { parseIncludedGames, parseDuration, createReactive } from "@/utils";
import config, { type Game } from "@/config";
import { ban, getUser } from "@/roblox";
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 class BanCommand extends Command {
public constructor(context: Command.LoaderContext, options: Command.Options) {
super(context, { ...options });
}
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder
.setName("ban")
.setDescription("ban a player")
.addStringOption((option) =>
option.setName("player").setDescription("the ID of the player to ban").setRequired(true),
)
.addStringOption((option) =>
option.setName("reason").setDescription("the reason for the ban").setRequired(true),
)
.addStringOption((option) =>
option
.setName("private-reason")
.setDescription("the private reason for the ban")
.setRequired(true),
)
.addStringOption((option) =>
option
.setName("length")
.setDescription("length of the ban; defaults to permanent")
.setRequired(false),
)
.addBooleanOption((option) =>
option
.setName("ban-alts")
.setDescription("whether to ban alts of the player")
.setRequired(false),
)
.addStringOption((option) =>
option
.setName("games")
.setDescription("the games to ban them from; defaults to all")
.setRequired(false),
),
);
}
public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
await interaction.deferReply({ withResponse: true });
const options = {
player: interaction.options.getString("player", true),
reason: interaction.options.getString("reason", true),
privateReason: interaction.options.getString("private-reason", true),
length: interaction.options.getString("length", false),
banAlts: interaction.options.getBoolean("ban-alts", false),
includedGames: interaction.options.getString("games", false),
};
const gamesResult = Result.from(() => parseIncludedGames(options.includedGames));
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.privateReason}`,
].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.banAlts,
displayReason: options.reason,
privateReason: options.privateReason,
};
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";
}
}
container.stores.loadPiece({
piece: BanCommand,
name: "ban",
store: "commands",
});
|