all repos — stealth-developers @ 150c6916c5949d48883c1e2f51b092b07fb0669c

apps/bot/src/middleware/index.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
import { HaltExecution } from "@purrkit/router";
import { kitten } from "@/client";
import { upsertGuild } from "@stealth-developers/db";
import { upsertUser } from "@/lib";

const base = kitten.builder();

export const getUser = base
	.use(async (interaction) => {
		if (!interaction.guild) return {};

		const user = await upsertUser(interaction.user, interaction.guild);
		if (!user) throw new HaltExecution("Failed to add you to the database. Please try again.");

		return { user };
	})
	.use(async (interaction) => {
		if (!interaction.guild) return {};

		const actor = await upsertUser(interaction.user, interaction.guild);
		if (!actor) throw new HaltExecution("Failed to add you to the database. Please try again.");

		return { actor };
	})
	.use(async (interaction) => {
		if (!interaction.guild) return {};

		const guild = await upsertGuild({
			discordId: interaction.guild.id,
			icon: interaction.guild.iconURL(),
			name: interaction.guild.name,
		});

		if (!guild)
			throw new HaltExecution("Failed to find your guild in the database. Please try again.");

		return { guild };
	});

export const getBasicData = getUser;