mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 03:59:50 +02:00
feat: link command basic functionaility
This commit is contained in:
1 parent
97d427a6a8
commit
19298b9ad6
19 files changed
+225
-166
No files matched your search
@@ -0,0 +1,23 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { Region } from '../../constants/regions';
|
||||
|
||||
export const discordBlitzDB = new PrismaClient();
|
||||
|
||||
export function linkBlitzAndDiscord(
|
||||
discord: number,
|
||||
region: Region,
|
||||
blitz: number,
|
||||
) {
|
||||
return discordBlitzDB.discord_blitz.upsert({
|
||||
where: { discord },
|
||||
update: { region, blitz },
|
||||
create: { discord, region, blitz },
|
||||
});
|
||||
}
|
||||
|
||||
export function getBlitzFromDiscord(discord: number) {
|
||||
return discordBlitzDB.discord_blitz.findUnique({
|
||||
where: { discord },
|
||||
select: { region: true, blitz: true },
|
||||
}) as Promise<null | { region: Region; blitz: number }>;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
||||
import { Region } from '../../constants/regions';
|
||||
import listPlayers, {
|
||||
usernamePatternWithoutPosition,
|
||||
} from '../blitz/listPlayers';
|
||||
import { getBlitzFromDiscord } from '../cockroackdb/discordBlitz';
|
||||
import throwError from '../node/throwError';
|
||||
|
||||
export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/;
|
||||
@@ -15,38 +13,14 @@ export interface ResolvedPlayer {
|
||||
export default async function resolvePlayerFromCommand(
|
||||
interaction: ChatInputCommandInteraction<CacheType>,
|
||||
) {
|
||||
const commandUsername = interaction.options.getString('username');
|
||||
const displayName = interaction.guild?.members.cache
|
||||
.get(interaction.user.id)
|
||||
?.displayName.match(usernamePatternWithoutPosition)?.[0];
|
||||
const username = commandUsername ?? displayName;
|
||||
const account = await getBlitzFromDiscord(parseInt(interaction.user.id));
|
||||
|
||||
if (username === undefined) {
|
||||
if (account) {
|
||||
return { region: account.region, id: account.blitz };
|
||||
} else {
|
||||
throw throwError(
|
||||
'Username could not be inferred',
|
||||
"You didn't provide me a username in the command nor was I able to infer your Blitz username from your Discord nickname. Try providing a username manually in the command or using the `/verify` command to set your Discord nickname to your Blitz username.",
|
||||
"You're not linked",
|
||||
'Use the `/link` command to link your Discord and Blitz accounts.',
|
||||
);
|
||||
}
|
||||
|
||||
if (serverAndIdPattern.test(username)) {
|
||||
const [server, accountId] = username.split('/');
|
||||
return {
|
||||
region: server as Region,
|
||||
id: Number(accountId),
|
||||
} satisfies ResolvedPlayer;
|
||||
} else {
|
||||
const accounts = await listPlayers(username);
|
||||
|
||||
if (accounts[0]) {
|
||||
return {
|
||||
region: accounts[0].region,
|
||||
id: accounts[0].account_id,
|
||||
} satisfies ResolvedPlayer;
|
||||
} else {
|
||||
throw throwError(
|
||||
'Could not find user',
|
||||
`I couldn't find user \`${username}\`. Try providing a valid username manually or using the \`/verify\` command to set your Discord nickname to your Blitz username.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { discordBlitzDB } from '../cockroackdb/discordBlitz';
|
||||
import { client } from '../discord/client';
|
||||
|
||||
const EXIT_EVENTS = [
|
||||
'beforeExit',
|
||||
'exit',
|
||||
'SIGINT',
|
||||
'SIGTERM',
|
||||
'SIGUSR1',
|
||||
'SIGUSR2',
|
||||
];
|
||||
|
||||
async function cleanup() {
|
||||
console.log('Cleaning up...');
|
||||
|
||||
await Promise.all([discordBlitzDB.$disconnect(), client.destroy()]);
|
||||
|
||||
console.log('Gracefully exiting...');
|
||||
|
||||
EXIT_EVENTS.forEach((event) => process.off(event, cleanup));
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
export function registerProcesses() {
|
||||
process.on('uncaughtException', console.error);
|
||||
client.on('error', console.error);
|
||||
|
||||
EXIT_EVENTS.forEach((event) => process.on(event, cleanup));
|
||||
}
|
||||
@@ -2,7 +2,11 @@ import { config } from 'dotenv';
|
||||
|
||||
config();
|
||||
|
||||
export const SECRET_KEYS = ['DISCORD_TOKEN', 'GH_TOKEN'] as const;
|
||||
export const SECRET_KEYS = [
|
||||
'DISCORD_TOKEN',
|
||||
'GH_TOKEN',
|
||||
'DISCORD_BLITZ_DB',
|
||||
] as const;
|
||||
|
||||
export type SECRET = typeof SECRET_KEYS extends readonly (infer T)[]
|
||||
? T
|
||||
|
||||
Reference in new issue
Block a user