feat(platform): add Discord onboarding and Velocity gate
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import { SlashCommandBuilder } from "discord.js";
|
||||
|
||||
export const commands = [
|
||||
new SlashCommandBuilder()
|
||||
.setName("register")
|
||||
.setDescription("Register a Minecraft account through a private login link"),
|
||||
new SlashCommandBuilder()
|
||||
.setName("account")
|
||||
.setDescription("Open your Minecraft account dashboard through a private login link"),
|
||||
].map((command) => command.toJSON());
|
||||
@@ -0,0 +1,7 @@
|
||||
export function requiredEnvironment(name: string) {
|
||||
const value = process.env[name]?.trim();
|
||||
if (!value) throw new Error(`Missing required environment variable: ${name}`);
|
||||
return value;
|
||||
}
|
||||
|
||||
export const commandNames = ["register", "account"] as const;
|
||||
@@ -0,0 +1,11 @@
|
||||
import "dotenv/config";
|
||||
import { REST, Routes } from "discord.js";
|
||||
import { commands } from "./commands";
|
||||
import { requiredEnvironment } from "./config";
|
||||
|
||||
const token = requiredEnvironment("DISCORD_BOT_TOKEN");
|
||||
const applicationId = requiredEnvironment("DISCORD_APPLICATION_ID");
|
||||
const rest = new REST({ version: "10" }).setToken(token);
|
||||
|
||||
await rest.put(Routes.applicationCommands(applicationId), { body: commands });
|
||||
console.log(`Deployed ${commands.length} global Discord commands.`);
|
||||
@@ -1,2 +1,79 @@
|
||||
// Discord command handling is added in Phase 2 after the one-time login service exists.
|
||||
export {};
|
||||
import "dotenv/config";
|
||||
import { createMagicLink, LoginRateLimitedError } from "@minecraft-account-manager/auth";
|
||||
import {
|
||||
createAuthRepository,
|
||||
createDatabase,
|
||||
recordEvent,
|
||||
} from "@minecraft-account-manager/database";
|
||||
import {
|
||||
ActionRowBuilder,
|
||||
ButtonBuilder,
|
||||
ButtonStyle,
|
||||
Client,
|
||||
Events,
|
||||
GatewayIntentBits,
|
||||
} from "discord.js";
|
||||
import { commandNames, requiredEnvironment } from "./config";
|
||||
|
||||
const token = requiredEnvironment("DISCORD_BOT_TOKEN");
|
||||
const appUrl = requiredEnvironment("APP_URL");
|
||||
const databaseUrl = requiredEnvironment("DATABASE_URL");
|
||||
const discordGuildId = requiredEnvironment("DISCORD_GUILD_ID");
|
||||
const { db } = createDatabase(databaseUrl);
|
||||
const authRepository = createAuthRepository(db);
|
||||
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
|
||||
|
||||
client.once(Events.ClientReady, (readyClient) => {
|
||||
console.log(`Discord bot ready as ${readyClient.user.tag}`);
|
||||
});
|
||||
|
||||
client.on(Events.InteractionCreate, async (interaction) => {
|
||||
if (!interaction.isChatInputCommand()) return;
|
||||
if (!commandNames.includes(interaction.commandName as (typeof commandNames)[number])) return;
|
||||
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
|
||||
try {
|
||||
if (interaction.guildId !== discordGuildId) {
|
||||
await interaction.editReply("This command is only available in the configured community server.");
|
||||
return;
|
||||
}
|
||||
|
||||
const magicLink = await createMagicLink(
|
||||
{
|
||||
id: interaction.user.id,
|
||||
username: interaction.user.username,
|
||||
globalName: interaction.user.globalName,
|
||||
},
|
||||
{ repository: authRepository, appUrl },
|
||||
);
|
||||
|
||||
await recordEvent(db, {
|
||||
type: "games.minecraft.account-manager.auth.magic-link.created",
|
||||
source: "/discord-bot",
|
||||
subject: `discord-user/${interaction.user.id}`,
|
||||
data: { command: interaction.commandName, guildId: interaction.guildId },
|
||||
});
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>().addComponents(
|
||||
new ButtonBuilder()
|
||||
.setLabel(interaction.commandName === "register" ? "Start registration" : "Open my account")
|
||||
.setStyle(ButtonStyle.Link)
|
||||
.setURL(magicLink.url),
|
||||
);
|
||||
|
||||
await interaction.editReply({
|
||||
content: "This private link works once and expires in 10 minutes. Do not share it.",
|
||||
components: [row],
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof LoginRateLimitedError) {
|
||||
await interaction.editReply("Please wait 30 seconds before requesting another private account link.");
|
||||
return;
|
||||
}
|
||||
console.error("Failed to create Discord account link", error);
|
||||
await interaction.editReply("I could not create an account link. Please try again shortly.");
|
||||
}
|
||||
});
|
||||
|
||||
await client.login(token);
|
||||
|
||||
Reference in New Issue
Block a user