generated from nhcarrigan/template
wip: commands
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* @copyright NHCarrigan
|
||||
* @license Naomi's Public License
|
||||
* @author Naomi Carrigan
|
||||
*/
|
||||
|
||||
import { MessageFlags } from "discord.js";
|
||||
import { ids } from "../config/ids.js";
|
||||
import { logger } from "../utils/logger.js";
|
||||
import type { Amari } from "../interfaces/amari.js";
|
||||
import type { ChatInputCommandInteraction } from "discord.js";
|
||||
|
||||
/**
|
||||
* @param amari
|
||||
* @param interaction
|
||||
*/
|
||||
export const onboardMentee = async(
|
||||
amari: Amari,
|
||||
interaction: ChatInputCommandInteraction,
|
||||
): Promise<void> => {
|
||||
if (interaction.user.id !== ids.users.naomi) {
|
||||
await interaction.reply({
|
||||
content: "This command is restricted to Naomi.",
|
||||
flags: [ MessageFlags.Ephemeral ],
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const menteeName = interaction.options.getString("mentee_name", true);
|
||||
const githubUsername = interaction.options.getString("github_username", true);
|
||||
const menteeUser = interaction.options.getUser("mentee", true);
|
||||
const discordId = menteeUser.id;
|
||||
|
||||
await interaction.deferReply({ ephemeral: true });
|
||||
|
||||
try {
|
||||
const { data: installation } = await amari.githubApp.octokit.rest.apps.getOrgInstallation({
|
||||
org: "nhcarrigan-mentorship",
|
||||
});
|
||||
const mentorshipOctokit = await amari.githubApp.getInstallationOctokit(installation.id);
|
||||
|
||||
let repoUrl: string;
|
||||
try {
|
||||
const { data: repoData } = await mentorshipOctokit.rest.repos.createInOrg({
|
||||
// eslint-disable-next-line @typescript-eslint/naming-convention -- Octokit API field.
|
||||
auto_init: true,
|
||||
name: githubUsername,
|
||||
org: "nhcarrigan-mentorship",
|
||||
});
|
||||
repoUrl = repoData.html_url;
|
||||
} catch {
|
||||
repoUrl = `https://github.com/nhcarrigan-mentorship/${githubUsername}`;
|
||||
}
|
||||
|
||||
await mentorshipOctokit.rest.repos.addCollaborator({
|
||||
owner: "nhcarrigan-mentorship",
|
||||
permission: "maintain",
|
||||
repo: githubUsername,
|
||||
username: githubUsername,
|
||||
});
|
||||
|
||||
const channel
|
||||
= amari.discord.channels.cache.get(ids.channels.menteeChat)
|
||||
?? await amari.discord.channels.fetch(ids.channels.menteeChat);
|
||||
|
||||
if (channel?.isSendable() !== true) {
|
||||
await interaction.editReply({
|
||||
content: "Repo created but could not send Discord notification.",
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
await channel.send({
|
||||
content: `Hey <@${discordId}>! I've created your mentorship repository: ${repoUrl}\n\nYou should have received an invitation to collaborate - please accept it to get started!`,
|
||||
});
|
||||
|
||||
await logger.metric("onboarded_mentee", 1, { mentee: menteeName });
|
||||
await interaction.editReply({
|
||||
content: `✅ Successfully onboarded **${menteeName}**!\nRepository: ${repoUrl}`,
|
||||
});
|
||||
} catch (error) {
|
||||
await logger.log("error", `Failed to onboard mentee: ${String(error)}`);
|
||||
await interaction.editReply({
|
||||
content: `❌ Failed to onboard mentee: ${String(error)}`,
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user