mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-04-26 09:20:24 -05:00
* Log in link creation initial * Add global name to update all command * Remove left over log * Login command * Update command * Add todos * TODOs * Migration file fix order
69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import { SlashCommandBuilder } from "@discordjs/builders";
|
|
import { Client } from "discord.js";
|
|
import invariant from "tiny-invariant";
|
|
import type { User } from "../../app/db/types";
|
|
import ids from "../ids";
|
|
import type { BotCommand } from "../types";
|
|
import { sendouInkFetch } from "../utils";
|
|
|
|
const COMMAND_NAME = "updateall";
|
|
|
|
const guildsToCrawlForUpdates = [ids.guilds.plusServer, ids.guilds.sendou];
|
|
|
|
export const updateAllCommand: BotCommand = {
|
|
guilds: [ids.guilds.adminServer],
|
|
name: COMMAND_NAME,
|
|
builder: new SlashCommandBuilder()
|
|
.setName(COMMAND_NAME)
|
|
.setDescription("Update sendou.ink usernames and avatars"),
|
|
// @ts-expect-error TODO: fix. Library doesn't seem to extract API Message type so I could fix this error?
|
|
execute: async ({ interaction, client }) => {
|
|
await interaction.deferReply({ ephemeral: true });
|
|
|
|
const userUpdates = await getUsersToUpdate(client);
|
|
const response = await sendouInkFetch("/users", {
|
|
method: "post",
|
|
body: JSON.stringify(userUpdates),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
return interaction.editReply(
|
|
`Update failed with status code ${response.status}`,
|
|
);
|
|
}
|
|
|
|
return interaction.editReply(`Sent ${userUpdates.length} users`);
|
|
},
|
|
};
|
|
|
|
async function getUsersToUpdate(client: Client<boolean>) {
|
|
const usersSeen = new Set<string>();
|
|
const userUpdates: Array<{
|
|
discordId: string;
|
|
discordAvatar: string | null;
|
|
discordName: string | null;
|
|
discordUniqueName: string | null;
|
|
}> = [];
|
|
for (const guildId of guildsToCrawlForUpdates) {
|
|
const guild = client.guilds.cache.find((g) => g.id === guildId);
|
|
invariant(guild);
|
|
|
|
for (const [, { user }] of await guild.members.fetch()) {
|
|
if (usersSeen.has(user.id)) continue;
|
|
|
|
const hasUniqueUsername = user.discriminator === "0";
|
|
|
|
userUpdates.push({
|
|
discordId: user.id,
|
|
discordAvatar: user.avatar,
|
|
discordName: hasUniqueUsername ? user.globalName : user.username,
|
|
discordUniqueName: hasUniqueUsername ? user.username : null,
|
|
});
|
|
|
|
usersSeen.add(user.id);
|
|
}
|
|
}
|
|
|
|
return userUpdates;
|
|
}
|