Skip to content

Commit

Permalink
Merge pull request #26 from su-its/hotfix/give-DPRoles
Browse files Browse the repository at this point in the history
[HOTFIX]学科ロールの付与に存在していたバグを修正
  • Loading branch information
KikyoNanakusa authored Apr 23, 2024
2 parents 4425133 + fc99da7 commit 186a7dc
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
34 changes: 22 additions & 12 deletions src/commands/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SlashCommandBuilder, CommandInteraction, Guild, Role, GuildMember } from "discord.js";
import { Command } from "../types/command";
import { adminAuth } from "../infra/firebase";
import { getMemberByDiscordId } from "../controllers/MemberController";
import { getMemberByDiscordId, getMemberByEmail } from "../controllers/MemberController";
import { UserRecord } from "firebase-admin/lib/auth/user-record";
import createRoleIfNotFound from "../utils/createRoleNotFound";
import addRoleToMember from "../utils/addRoleToMember";
Expand All @@ -15,6 +15,7 @@ import csRole from "../roles/departments/cs";
import iaRole from "../roles/departments/ia";
import authorizedRoleProperty from "../roles/authorized";
import unAuthorizedRoleProperty from "../roles/unAuthorized";
import Member from "../entities/member";

const authCommand: Command = {
data: new SlashCommandBuilder().setName("auth").setDescription("認証コマンド"),
Expand All @@ -33,33 +34,32 @@ async function authCommandHandler(interaction: CommandInteraction) {
}

// メール認証が完了しているか確認
const user = await adminAuth.getUserByEmail(member.mail);
const user: UserRecord = await adminAuth.getUserByEmail(member.mail);
if (user.emailVerified) {
try {
await giveRoles(interaction);
await giveRoles(interaction, user);
} catch (error) {
console.error(error);
await interaction.reply("認証に失敗しました");
}
} else {
await interaction.reply("メール認証が完了していません");
}
}

async function giveRoles(interaction: CommandInteraction) {
const user = await adminAuth.getUserByEmail(interaction.user.tag);
async function giveRoles(interaction: CommandInteraction, user: UserRecord) {
const guild: Guild = interaction.guild!;
const guildMember = await guild.members.fetch(interaction.user.id);
const guildMember: GuildMember = await guild.members.fetch(interaction.user.id);

await giveAuthorizedRole(interaction, guild);
await giveAuthorizedRole(interaction, guild, guildMember);
await giveDepartmentRole(interaction, user, guildMember);
}

async function giveAuthorizedRole(interaction: CommandInteraction, guild: Guild) {
async function giveAuthorizedRole(interaction: CommandInteraction, guild: Guild, guildMember: GuildMember) {
try {
const authorizedRole: Role = await createRoleIfNotFound({ guild, customRole: authorizedRoleProperty });
const unAuthorizedRole: Role = await createRoleIfNotFound({ guild, customRole: unAuthorizedRoleProperty });

const guildMember = await guild.members.fetch(interaction.user.id);
await guildMember.roles.add(authorizedRole);
await guildMember.roles.remove(unAuthorizedRole);

Expand All @@ -69,10 +69,18 @@ async function giveAuthorizedRole(interaction: CommandInteraction, guild: Guild)
}
}

async function giveDepartmentRole(interaction: CommandInteraction, user: UserRecord, guildMember: GuildMember) {
async function giveDepartmentRole(interaction: CommandInteraction, userAccount: UserRecord, guildMember: GuildMember) {
const guild: Guild = interaction.guild!;
const department: string = user.customClaims?.department;
switch (department) {

//認証用のアカウントから、メンバー情報を取得
const member: Member | undefined = await getMemberByEmail(userAccount.email!);
if (!member) {
throw new Error("Member not found");
return;
}

//TODO: 要リファクタリング
switch (member.department) {
case Department.CS:
await addRoleToMember(guild, guildMember, csRole);
break;
Expand All @@ -88,6 +96,8 @@ async function giveDepartmentRole(interaction: CommandInteraction, user: UserRec
case Department.OTHERS:
await addRoleToMember(guild, guildMember, othersRole);
break;
default:
throw new Error("Department not found");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/controllers/MemberController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function getAllMembers() {
}

// Emailでメンバーを取得する 見つからなければundefinedを返す
export async function getMemberByEamil(email: string): Promise<Member | undefined> {
export async function getMemberByEmail(email: string): Promise<Member | undefined> {
try {
const members = await getMembers();
const member = members.find((m) => m.mail === email);
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/authController.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AuthData from "../types/authData";
import sendAuthMail from "../usecases/sendAuthMail";
import { getMemberByEamil } from "./MemberController";
import { getMemberByEmail } from "./MemberController";
import setDiscordId from "../usecases/setDiscordId";
import Member from "../entities/member";

Expand All @@ -11,7 +11,7 @@ async function sendAuthMailController(userInfo: AuthData) {
}
await sendAuthMail(userInfo.mail!, userInfo.student_number!, userInfo.department!);

const member = await getMemberByEamil(userInfo.mail!);
const member = await getMemberByEmail(userInfo.mail!);
checkMember(member);

await setDiscordId(member!.id!, userInfo.discordId!);
Expand Down
2 changes: 1 addition & 1 deletion src/usecases/sendAuthMail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMemberByEamil } from "../controllers/MemberController";
import { getMemberByEmail } from "../controllers/MemberController";
import { auth } from "../infra/firebase";
import {
ActionCodeSettings,
Expand Down

0 comments on commit 186a7dc

Please sign in to comment.