[Commands] /github-mapping remove now correctly hashes the Discord ID. (#28)

This commit is contained in:
2026-11-10 07:48:12 -07:00
committed by GitHub
parent 73f9d32649
commit 6503a6ec11
2 changed files with 70 additions and 34 deletions
+66 -31
View File
@@ -1,6 +1,61 @@
import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js'; import { ApplicationIntegrationType, ChatInputCommandInteraction, Client, InteractionContextType, MessageFlags, PermissionFlagsBits, SlashCommandBuilder } from 'discord.js';
import { Database } from '../shared/Database'; import { Database } from '../shared/Database';
import { getIdFromInput } from '../utils'; import { getIdFromInput, SetSeverity } from '../utils';
const userRegex = new RegExp(/^\d{16,20}$/);
async function addUserMapping(interaction: ChatInputCommandInteraction) {
const githubUsername = interaction.options.getString('github-name', true);
const discordId = getIdFromInput(interaction.options.getString('discord-user', true));
if (!userRegex.test(discordId)) throw new Error('The provided Discord user ID is invalid.');
const existingMappingId = await Database.getGithubFromDiscordId(discordId);
const existingMappingName = await Database.getDiscordIdFromGithub(githubUsername);
if (existingMappingId) throw new Error(`<@${discordId}> (${discordId}) is already mapped to \`${existingMappingId}\``);
if (existingMappingName) throw new Error(`\`${githubUsername}\` is already mapped to <@${existingMappingName}> (${existingMappingName})`);
await Database.putGithubUserMapping(discordId, githubUsername);
await interaction.editReply({
embeds: [{
...SetSeverity('success'),
title: 'GitHub/Discord Mapping | Add',
description: `\`${githubUsername}\` has been linked to <@${discordId}>`,
}],
});
}
async function removeUserMapping(interaction: ChatInputCommandInteraction) {
const discordId = getIdFromInput(interaction.options.getString('discord-user', true));
if (!userRegex.test(discordId)) throw new Error('The provided Discord user ID is invalid.');
if (!await Database.removeGithubUserMapping(discordId)) throw new Error('No existing GitHub mapping was found.');
await interaction.editReply({
embeds: [{
...SetSeverity('success'),
title: 'GitHub/Discord Mapping | Remove',
description: `<@${discordId}> is no longer linked.`,
}],
});
}
async function listUserMapping(interaction: ChatInputCommandInteraction) {
const allMappings = await Database.getAllGithubUserMappings();
if (allMappings.length < 1) throw new Error('No GitHub mappings have been configured.');
await interaction.editReply({
embeds: [{
...SetSeverity('success'),
title: 'GitHub/Discord Mapping | List',
description: allMappings.map(m => `- <@${m.discord_id}> (${m.discord_id}) - ${m.github_username}`).join('\n'),
}]
});
}
export default { export default {
name: 'github-mapping', name: 'github-mapping',
@@ -44,37 +99,17 @@ export default {
.setDescription('List all github-discord mappings.') .setDescription('List all github-discord mappings.')
), ),
handler: async function (client: Client, interaction: ChatInputCommandInteraction) { handler: async function (client: Client, interaction: ChatInputCommandInteraction) {
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] }); if (!interaction.inGuild()) return;
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
const subcommand = await interaction.options.getSubcommand(true); try {
switch (interaction.options.getSubcommand(true)) {
if (subcommand == 'add') { case 'add': return await addUserMapping(interaction);
const discordUserInput = interaction.options.getString('discord-user', true); case 'remove': return await removeUserMapping(interaction);
case 'list': return await listUserMapping(interaction);
const idToUse = getIdFromInput(discordUserInput); }
} catch (error: Error | any) {
const githubName = interaction.options.getString('github-name', true); await interaction.editReply({ content: error.message }).catch(() => {});
const existingMappingId = await Database.getGithubFromDiscordId(idToUse);
const existingMappingName = await Database.getDiscordIdFromGithub(githubName);
if (existingMappingId) return interaction.editReply(`Discord user id is already mapped to ${existingMappingId}`);
if (existingMappingName) return interaction.editReply(`Github username is already mapped to <@${existingMappingName}> (${existingMappingName})`);
Database.putGithubUserMapping(idToUse, githubName);
return interaction.editReply('Mapping added.');
} else if (subcommand == 'remove') {
const discordUserInput = interaction.options.getString('discord-user', true);
const idToUse = getIdFromInput(discordUserInput);
Database.removeGithubUserMapping(idToUse);
return interaction.editReply('Mapping removed.');
} else if (subcommand == 'list') {
const allMappings = await Database.getAllGithubUserMappings();
return interaction.editReply(allMappings.map(m => `- <@${m.discord_id}> (${m.discord_id}) - ${m.github_username}`).join('\n'));
} }
} }
}; };
+3 -2
View File
@@ -364,8 +364,9 @@ export class Database {
}); });
} }
static async removeGithubUserMapping(discordId: string) { static async removeGithubUserMapping(discordId: string): Promise<boolean> {
await Database.db.run('DELETE from github_user_mapping WHERE discord_id_hash = ?', Encrypter.encrypt(discordId)); const result = await Database.db.run('DELETE FROM github_user_mapping WHERE discord_id_hash = ?', Encrypter.hash(discordId));
return (result.changes ?? 0) > 0;
} }
//#endregion //#endregion