import { MessageFlags, SlashCommandBuilder } from "discord.js"; import type { BotCommand } from "./index"; import { botConfig } from "@/bot/config"; import { createDiscordUser, SignupError, validateSignup } from "@/bot/services/signup"; /** * DM-only: the temp password is delivered through the DM. The most common * blocker is Discord's "Allow direct messages from server members" privacy * setting, which blocks DMs in both directions. */ export const DM_ONLY_MESSAGE = [ "This command only works in a direct message with the bot.", "", "To use it:", "1. Open Discord Settings → Privacy & Safety and enable **Allow direct messages from server members**.", "2. Right-click the bot in the member list → **Message** (the bot will then appear in your DMs).", "3. Run /signup in that DM.", ].join("\n"); const errorMessage = (error: unknown): string => error instanceof Error ? error.message : "Unknown error"; const buildWelcomeMessage = (username: string, tempPassword: string): string => [ "Your Polaris Task Force account has been created.", "", `Username: ${username}`, `Temporary password: ${tempPassword}`, "", `Log in at ${botConfig.appUrl} and change your password in Account settings.`, ].join("\n"); export const signupCommand: BotCommand = { builder: new SlashCommandBuilder() .setName("signup") .setDescription("Create a Polaris Task Force account linked to your Discord account") .addStringOption((option) => option.setName("displayname").setDescription("Your in-game name").setRequired(true), ) .addStringOption((option) => option .setName("steamid") .setDescription("Your Steam64 ID (17-digit numeric)") .setRequired(true), ) as SlashCommandBuilder, scope: "global", execute: async (interaction, payload) => { if (interaction.inGuild()) { await interaction.reply({ content: DM_ONLY_MESSAGE, flags: MessageFlags.Ephemeral }); return; } const username = interaction.user.username; const discordId = interaction.user.id; const displayName = interaction.options.getString("displayname", true); const steamId = interaction.options.getString("steamid", true); try { await validateSignup(payload, { username, discordId, steamId, displayName }); } catch (error) { if (error instanceof SignupError) { await interaction.reply({ content: error.message, flags: MessageFlags.Ephemeral }); return; } throw error; } const { user, tempPassword } = await createDiscordUser(payload, { username, discordId, displayName, steamId, }); const welcomeMessage = buildWelcomeMessage(user.username, tempPassword); // Best-effort copy in the DM channel. The ephemeral reply below is the // guaranteed delivery path: the command is DM-only, so the reply IS in the // user's DM and cannot be blocked by privacy settings. If this send fails, // the account is not orphaned — the reply carries the password. let dmSent = false; try { await interaction.user.send(welcomeMessage); dmSent = true; } catch (error) { payload.logger.warn( `[Bot] Could not DM the /signup welcome message to ${user.username}: ${errorMessage(error)}`, ); } await interaction.reply({ content: dmSent ? `Account created for ${user.username}. Your temporary password was sent to this DM.` : [ welcomeMessage, "", "I couldn't send you a separate DM. If this keeps happening, enable “Allow direct messages from server members” in Discord Settings → Privacy & Safety.", ].join("\n"), flags: MessageFlags.Ephemeral, }); }, };