Skip to content

Commit

Permalink
Disallow multiple account binding
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Nov 2, 2024
1 parent a54b7ad commit c367cb8
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 385 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { MessageButtonCreator } from "@utils/creators/MessageButtonCreator";
import { SlashSubcommand } from "structures/core/SlashSubcommand";
import { UserbindLocalization } from "@localization/interactions/commands/osu! and osu!droid/userbind/UserbindLocalization";
import { CommandHelper } from "@utils/helpers/CommandHelper";
import { Constants } from "@core/Constants";
import { InteractionHelper } from "@utils/helpers/InteractionHelper";
import { DroidHelper } from "@utils/helpers/DroidHelper";

Expand All @@ -14,7 +13,9 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
);

const uid = interaction.options.getInteger("uid", true);
const email = interaction.options.getString("email");
const email = interaction.options.getString("email", true);

await InteractionHelper.deferReply(interaction);

const dbManager = DatabaseManager.elainaDb.collections.userBind;
const uidBindInfo = await dbManager.getFromUid(uid, {
Expand All @@ -24,21 +25,22 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
if (uidBindInfo && uidBindInfo.discordid !== interaction.user.id) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("accountHasBeenBindedError"),
localization.getTranslation("accountHasBeenBoundError"),
),
});
}

const userBindInfo = await dbManager.getFromUser(interaction.user, {
projection: {
_id: 0,
previous_bind: 1,
},
projection: { _id: 0, discordid: 1 },
});

await InteractionHelper.deferReply(interaction);

// TODO: this is a lot of duplicate codes. should consider moving to a function
if (userBindInfo) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("discordAccountAlreadyBoundError"),
),
});
}

const player = await DroidHelper.getPlayer(uid, [
"id",
Expand All @@ -54,146 +56,52 @@ export const run: SlashSubcommand<true>["run"] = async (_, interaction) => {
});
}

if (userBindInfo) {
const isUidBinded = userBindInfo.isUidBinded(uid);

if (!isUidBinded) {
if (interaction.guild?.id !== Constants.mainServer) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation(
"newAccountBindNotInMainServer",
),
),
});
}

if (!email) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("emailNotSpecified"),
),
});
}

if (email !== player.email) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("incorrectEmail"),
),
});
}

const confirmation = await MessageButtonCreator.createConfirmation(
interaction,
{
content: MessageCreator.createWarn(
localization.getTranslation(
"newAccountUidBindConfirmation",
),
uid.toString(),
),
},
[interaction.user.id],
10,
localization.language,
);

if (!confirmation) {
return;
}
}

const result = await userBindInfo.bind(player, localization.language);

if (!result.success) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("accountUidBindError"),
uid.toString(),
result.reason!,
),
});
}

if (isUidBinded) {
InteractionHelper.reply(interaction, {
content: MessageCreator.createAccept(
localization.getTranslation("oldAccountUidBindSuccessful"),
uid.toString(),
),
});
} else {
await DatabaseManager.aliceDb.collections.accountTransfer.updateOne(
{
discordId: interaction.user.id,
},
{
$push: { transferList: uid },
$setOnInsert: { transferUid: userBindInfo.uid },
},
{ upsert: true },
);

InteractionHelper.reply(interaction, {
content: MessageCreator.createAccept(
localization.getTranslation("newAccountUidBindSuccessful"),
uid.toString(),
(1 - userBindInfo.previous_bind.length).toString(),
),
});
}
} else {
if (interaction.guild?.id !== Constants.mainServer) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation(
"newAccountBindNotInMainServer",
),
),
});
}

if (!email) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("emailNotSpecified"),
),
});
}

if (email !== player.email) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("incorrectEmail"),
),
});
}

const result = await dbManager.insert({
discordid: interaction.user.id,
uid: uid,
username: player.username,
previous_bind: [uid],
if (email !== player.email) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("incorrectEmail"),
),
});
}

const confirmation = await MessageButtonCreator.createConfirmation(
interaction,
{
content: MessageCreator.createWarn(
localization.getTranslation("bindConfirmation"),
player.username,
),
},
[interaction.user.id],
10,
localization.language,
);

if (!confirmation) {
return;
}

const result = await dbManager.insert({
discordid: interaction.user.id,
uid: uid,
username: player.username,
});

if (result.failed()) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("accountUidBindError"),
result.reason,
),
});
}

InteractionHelper.reply(interaction, {
content: MessageCreator.createAccept(
localization.getTranslation("newAccountUidBindSuccessful"),
uid.toString(),
"1",
if (result.failed()) {
return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
localization.getTranslation("bindError"),
result.reason,
),
});
}

InteractionHelper.reply(interaction, {
content: MessageCreator.createAccept(
localization.getTranslation("bindSuccessful"),
player.username,
),
});
};

export const config: SlashSubcommand["config"] = {
Expand Down
Loading

0 comments on commit c367cb8

Please sign in to comment.