complete migartion add_nick_super

This commit is contained in:
kdh8219 2023-04-26 21:20:12 +09:00
parent 88ddedef97
commit dbf5fed7f2
3 changed files with 90 additions and 107 deletions

View file

@ -2,17 +2,10 @@ import {
SlashCommandBuilder, SlashCommandBuilder,
PermissionFlagsBits, PermissionFlagsBits,
ChatInputCommandInteraction, ChatInputCommandInteraction,
User,
} from "discord.js"; } from "discord.js";
import mojangAPI from "../../wrapper/mojang-api.js"; import mojangAPI from "../../wrapper/mojang-api.js";
import firebase from "../../wrapper/firebase.js"; import firebase from "../../wrapper/firebase.js";
import {
check_user_type_by_discord_id,
check_user_type_by_minecraft_uuid,
EUserType,
TData,
} from "../../functions.js";
export default { export default {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
@ -33,17 +26,7 @@ export default {
.setDefaultMemberPermissions(PermissionFlagsBits.Administrator) .setDefaultMemberPermissions(PermissionFlagsBits.Administrator)
.setDMPermission(false), .setDMPermission(false),
async execute(interaction: ChatInputCommandInteraction) { async execute(interaction: ChatInputCommandInteraction) {
let data = (await firebase.get()).val() as TData;
const discord_id = interaction.options.getUser("discord")?.id as string; const discord_id = interaction.options.getUser("discord")?.id as string;
const dcusertype = check_user_type_by_discord_id(data, discord_id);
if (dcusertype == EUserType.Blacklisted) {
interaction.editReply({
content: "`에러`: 해당 디스코드 아이디는 블랙리스트 되었습니다.",
});
return;
}
const mcid = interaction.options.getString("minecraft_id") as string; const mcid = interaction.options.getString("minecraft_id") as string;
let mcuuid: string; let mcuuid: string;
try { try {
@ -55,35 +38,41 @@ export default {
}); });
return; return;
} }
const mcusertype = check_user_type_by_minecraft_uuid(data, mcuuid);
if (mcusertype == EUserType.Blacklisted) { const members = firebase.collection("members");
const blacklist = firebase.collection("blacklist");
if (!(await blacklist.where("discord_id", "==", discord_id).get()).empty) {
interaction.editReply({ interaction.editReply({
content: "`에러`: 해당 마인크래프트 아이디는 블랙리스트 되었습니다.", content: "`에러`: 해당 디스코드 아이디는 블랙리스트 되었습니다.",
}); });
return; return;
} else if (mcusertype == EUserType.Member) { }
if (!(await blacklist.where("minecraft_uuid", "==", mcuuid).get()).empty) {
interaction.editReply({
content: "`에러`: 해당 마인크래프트 계정은 블랙리스트 되었습니다.",
});
return;
}
if (!(await members.where("minecraft_uuid", "==", mcuuid).get()).empty) {
interaction.editReply({ interaction.editReply({
content: "`에러`: 해당 마인크래프트 아이디는 이미 등록되었습니다.", content: "`에러`: 해당 마인크래프트 아이디는 이미 등록되었습니다.",
}); });
return; return;
} }
if (dcusertype == EUserType.Unregistered) { if (!(await members.where("discord_id", "==", discord_id).get()).empty) {
await interaction.editReply({ await interaction.editReply({
content: `${mcid}(${mcuuid})님 2k2r에 오신것을 환영합니다!`, content: `${mcid}(${mcuuid})님 2k2r에 오신것을 환영합니다!`,
}); });
data["members"].push({ discord: discord_id, minecraft: [mcuuid] });
} else { } else {
for (let member of data.members) { await interaction.editReply({
if (member.discord == discord_id) { content: `${mcid}(${mcuuid})이 성공적으로 부계정으로 등록되었습니다!`,
await interaction.editReply({ });
content: `${mcid}(${mcuuid})이 성공적으로 부계정으로 등록되었습니다!`,
});
member.minecraft.push(mcuuid);
}
} //신규 디코일 경우
} }
await firebase.set(data); await members.add({
discord_id,
minecraft_uuid: mcuuid,
});
}, },
}; };

View file

@ -6,66 +6,6 @@ export type TCommand = {
data: SlashCommandBuilder; data: SlashCommandBuilder;
execute: Function; execute: Function;
}; };
export type TUser = {
discord: string; // id
minecraft: string[]; //uuid
};
export type TData = {
blacklist: TUser[];
members: TUser[];
};
export enum EUserType {
Unregistered,
Blacklisted,
Member,
}
export function check_user_type_by_discord_id(
data: TData,
discord_id: string
): EUserType {
for (let discord_search_point in data["blacklist"]) {
if (data.blacklist[discord_search_point].discord === discord_id) {
return EUserType.Blacklisted;
}
}
for (let discord_search_point in data["members"]) {
if (data.blacklist[discord_search_point].discord === discord_id) {
return EUserType.Member;
}
}
return EUserType.Unregistered;
}
export function check_user_type_by_minecraft_uuid(
data: TData,
minecraft_uuid: string
): EUserType {
for (let discord_search_point in data.blacklist) {
for (let minecraft_search_point in data.blacklist[discord_search_point]
.minecraft) {
if (
data.blacklist[discord_search_point].minecraft[
minecraft_search_point
] === minecraft_uuid
) {
return EUserType.Blacklisted;
}
}
}
for (let discord_search_point in data.members) {
for (let minecraft_search_point in data.members[discord_search_point]
.minecraft) {
if (
data.members[discord_search_point].minecraft[minecraft_search_point] ===
minecraft_uuid
) {
return EUserType.Member;
}
}
}
return EUserType.Unregistered;
}
export function getCommands(): Collection<string, TCommand> { export function getCommands(): Collection<string, TCommand> {
const commandColection = new Collection<string, TCommand>(); const commandColection = new Collection<string, TCommand>();
for (const command of commands) { for (const command of commands) {
@ -73,3 +13,64 @@ export function getCommands(): Collection<string, TCommand> {
} }
return commandColection; return commandColection;
} }
export type TUser = {
discord: string; // id
minecraft: string[]; //uuid
};
// export type TData = {
// blacklist: TUser[];
// members: TUser[];
// };
// export enum EUserType {
// Unregistered,
// Blacklisted,
// Member,
// }
// export function check_user_type_by_discord_id(
// data: TData,
// discord_id: string
// ): EUserType {
// for (let discord_search_point in data["blacklist"]) {
// if (data.blacklist[discord_search_point].discord === discord_id) {
// return EUserType.Blacklisted;
// }
// }
// for (let discord_search_point in data["members"]) {
// if (data.blacklist[discord_search_point].discord === discord_id) {
// return EUserType.Member;
// }
// }
// return EUserType.Unregistered;
// }
// export function check_user_type_by_minecraft_uuid(
// data: TData,
// minecraft_uuid: string
// ): EUserType {
// for (let discord_search_point in data.blacklist) {
// for (let minecraft_search_point in data.blacklist[discord_search_point]
// .minecraft) {
// if (
// data.blacklist[discord_search_point].minecraft[
// minecraft_search_point
// ] === minecraft_uuid
// ) {
// return EUserType.Blacklisted;
// }
// }
// }
// for (let discord_search_point in data.members) {
// for (let minecraft_search_point in data.members[discord_search_point]
// .minecraft) {
// if (
// data.members[discord_search_point].minecraft[minecraft_search_point] ===
// minecraft_uuid
// ) {
// return EUserType.Member;
// }
// }
// }
// return EUserType.Unregistered;
// }

View file

@ -1,19 +1,12 @@
import firebase_admin from "firebase-admin"; import { initializeApp, cert } from "firebase-admin/app";
import { getDatabase } from "firebase-admin/database"; import { getFirestore } from "firebase-admin/firestore";
import { config } from "dotenv"; import { config } from "dotenv";
config(); config();
if (!firebase_admin.apps.length) { initializeApp({
firebase_admin.initializeApp({ credential: cert(JSON.parse(process.env["firebase_cert"] as string)),
credential: firebase_admin.credential.cert( });
JSON.parse(process.env["firebase_cert"] as string)
),
databaseURL: "https://two-k-two-r-name-bot-default-rtdb.firebaseio.com",
databaseAuthVariableOverride: {
uid: process.env.FIREBASE_UID,
},
});
}
const firebase = getDatabase().ref("/");
export default firebase; const db = getFirestore();
export default db;