Skip to content

Commit

Permalink
Readd switchbind command
Browse files Browse the repository at this point in the history
  • Loading branch information
Rian8337 committed Dec 1, 2024
1 parent 413d1b3 commit 1bd1968
Show file tree
Hide file tree
Showing 12 changed files with 306 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/database/utils/elainaDb/UserBind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,78 @@ export class UserBind extends Manager implements DatabaseUserBind {
);
}

/**
* Moves the bind of a bound osu!droid account in this Discord account to another
* Discord account.
*
* @param uid The uid of the osu!droid account.
* @param to The ID of the Discord account to move to.
* @param language The locale of the user who attempted to move the bind. Defaults to English.
* @returns An object containing information about the operation.
*/
async moveBind(
uid: number,
to: Snowflake,
language: Language = "en",
): Promise<OperationResult> {
const localization = this.getLocalization(language);

if (this.uid !== uid) {
return this.createOperationResult(
false,
localization.getTranslation("uidNotBindedToAccount"),
);
}

if (this.discordid === to) {
return this.createOperationResult(
false,
localization.getTranslation("cannotRebindToSameAccount"),
);
}

const otherBindInfo = await this.bindDb.getFromUser(to, {
projection: {
_id: 0,
uid: 1,
},
});

if (otherBindInfo) {
return this.createOperationResult(
false,
localization.getTranslation("targetAccountAlreadyBound"),
);
}

this.discordid = to;

await DatabaseManager.aliceDb.collections.nameChange.updateOne(
{ discordid: this.discordid },
{ $set: { discordid: to } },
);

// Remove the new Discord account's account transfer information.
await DatabaseManager.aliceDb.collections.accountTransfer.deleteOne({
discordId: to,
});

await DatabaseManager.aliceDb.collections.accountTransfer.updateOne(
{ discordId: this.discordid },
{ $set: { discordId: to } },
);

await this.bindDb.updateOne(
{ discordid: this.discordid },
{ $set: { discordid: to } },
);

return DatabaseManager.aliceDb.collections.playerInfo.updateOne(
{ uid: uid },
{ $set: { discordid: to } },
);
}

/**
* Binds an osu!droid account to this Discord account.
*
Expand Down
138 changes: 138 additions & 0 deletions src/interactions/commands/Bot Creators/switchbind/switchbind.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import { DatabaseManager } from "@database/DatabaseManager";
import { CommandCategory } from "@enums/core/CommandCategory";
import { SlashCommand } from "structures/core/SlashCommand";
import { Constants } from "@core/Constants";
import { MessageCreator } from "@utils/creators/MessageCreator";
import { ApplicationCommandOptionType } from "discord.js";
import { NumberHelper } from "@utils/helpers/NumberHelper";
import { SwitchbindLocalization } from "@localization/interactions/commands/Bot Creators/switchbind/SwitchbindLocalization";
import { CommandHelper } from "@utils/helpers/CommandHelper";
import { InteractionHelper } from "@utils/helpers/InteractionHelper";
import { ConstantsLocalization } from "@localization/core/constants/ConstantsLocalization";

export const run: SlashCommand["run"] = async (client, interaction) => {
const localization = new SwitchbindLocalization(
CommandHelper.getLocale(interaction),
);

if (
!CommandHelper.isExecutedByBotOwner(interaction) &&
(!interaction.inCachedGuild() ||
!interaction.member.roles.cache.has("803154670380908575"))
) {
interaction.ephemeral = true;

return InteractionHelper.reply(interaction, {
content: MessageCreator.createReject(
new ConstantsLocalization(localization.language).getTranslation(
Constants.noPermissionReject,
),
),
});
}

await InteractionHelper.deferReply(interaction);

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

if (
!NumberHelper.isNumberInRange(
uid,
Constants.uidMinLimit,
Constants.uidMaxLimit,
true,
)
) {
return InteractionHelper.reply(interaction, {
content: localization.getTranslation("invalidUid"),
});
}

const user = await (
await client.guilds.fetch(Constants.mainServer)
).members.fetch(interaction.options.getUser("user", true));

const bindInfo =
await DatabaseManager.elainaDb.collections.userBind.getFromUid(uid);

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

const result = await bindInfo.moveBind(uid, user.id, localization.language);

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

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

export const category: SlashCommand["category"] = CommandCategory.botCreators;

export const config: SlashCommand["config"] = {
name: "switchbind",
description:
"Switches an osu!droid account bind from one Discord account to another.",
options: [
{
name: "uid",
required: true,
type: ApplicationCommandOptionType.Integer,
description: "The uid of the osu!droid account to switch.",
minValue: Constants.uidMinLimit,
},
{
name: "user",
required: true,
type: ApplicationCommandOptionType.User,
description: "The user to switch the bind to.",
},
],
example: [
{
command: "switchbind uid:51076 user:@Rian8337#0001",
arguments: [
{
name: "uid",
value: 51076,
},
{
name: "user",
value: "@Rian8337#0001",
},
],
description:
"will switch the osu!droid account with uid 51076's bind to Rian8337.",
},
{
command: "switchbind uid:5475 user:132783516176875520",
arguments: [
{
name: "uid",
value: 5475,
},
{
name: "user",
value: "132783516176875520",
},
],
description:
"will switch the osu!droid account with uid 5475's bind to the Discord account with ID 132783516176875520.",
},
],
permissions: ["Special"],
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { UserBindKRTranslation } from "./translations/UserBindKRTranslation";

export interface UserBindStrings {
readonly uidNotBindedToAccount: string;
readonly cannotRebindToSameAccount: string;
readonly targetAccountAlreadyBound: string;
readonly playerNotFound: string;
readonly playerWithUidOrUsernameNotFound: string;
readonly unbindClanDisbandNotification: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { UserBindStrings } from "../UserBindLocalization";
export class UserBindENTranslation extends Translation<UserBindStrings> {
override readonly translations: UserBindStrings = {
uidNotBindedToAccount: "uid is not bound to this Discord account",
cannotRebindToSameAccount: "cannot rebind to the same Discord account",
targetAccountAlreadyBound: "other Discord account is already bound",
playerNotFound: "player not found",
playerWithUidOrUsernameNotFound:
"player with such uid or username is not found",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ export class UserBindESTranslation extends Translation<UserBindStrings> {
override readonly translations: UserBindStrings = {
uidNotBindedToAccount:
"Ese UID no esta enlazado a esta cuenta de discord",
cannotRebindToSameAccount:
"No puedes enlazarlo a la misma cuenta de discord",
targetAccountAlreadyBound: "",
playerNotFound: "jugador no encontrado",
playerWithUidOrUsernameNotFound:
"jugador con ese nick / UID no puede ser encontrado",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { UserBindStrings } from "../UserBindLocalization";
export class UserBindIDTranslation extends Translation<UserBindStrings> {
override readonly translations: UserBindStrings = {
uidNotBindedToAccount: "uid tidak terhubung dengan akun Discord ini",
cannotRebindToSameAccount:
"tidak dapat menghubungkan kembali ke akun Discord yang sama",
targetAccountAlreadyBound: "",
playerNotFound: "pemain tidak ditemukan",
playerWithUidOrUsernameNotFound:
"pemain dengan uid atau username tersebut tidak ditemukan",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { UserBindStrings } from "../UserBindLocalization";
export class UserBindKRTranslation extends Translation<UserBindStrings> {
override readonly translations: UserBindStrings = {
uidNotBindedToAccount: "uid가 이 디스코드 계정에 바인딩 되어있지 않음",
cannotRebindToSameAccount:
"같은 디스코드 계정에 다시 바인드 할 수 없음",
targetAccountAlreadyBound: "",
playerNotFound: "플레이어가 발견되지 않음",
playerWithUidOrUsernameNotFound:
"해당 uid나 유저네임의 플레이어가 발견되지 않음",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Localization } from "@localization/base/Localization";
import { Translations } from "@localization/base/Translations";
import { SwitchbindENTranslation } from "./translations/SwitchbindENTranslation";
import { SwitchbindESTranslation } from "./translations/SwitchbindESTranslation";
import { SwitchbindIDTranslation } from "./translations/SwitchbindIDTranslation";
import { SwitchbindKRTranslation } from "./translations/SwitchbindKRTranslation";

export interface SwitchbindStrings {
readonly invalidUid: string;
readonly uidNotBinded: string;
readonly switchFailed: string;
readonly switchSuccessful: string;
}

/**
* Localizations for the `switchbind` command.
*/
export class SwitchbindLocalization extends Localization<SwitchbindStrings> {
protected override readonly localizations: Readonly<
Translations<SwitchbindStrings>
> = {
en: new SwitchbindENTranslation(),
kr: new SwitchbindKRTranslation(),
id: new SwitchbindIDTranslation(),
es: new SwitchbindESTranslation(),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Translation } from "@localization/base/Translation";
import { SwitchbindStrings } from "../SwitchbindLocalization";

/**
* The English translation of the `switchbind` command.
*/
export class SwitchbindENTranslation extends Translation<SwitchbindStrings> {
override readonly translations: SwitchbindStrings = {
invalidUid: "Hey, please enter a valid uid!",
uidNotBinded: "I'm sorry, this uid is not bound to anyone!",
switchFailed: "I'm sorry, I'm unable to switch the bind: %s.",
switchSuccessful: "Successfully switched bind.",
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Translation } from "@localization/base/Translation";
import { SwitchbindStrings } from "../SwitchbindLocalization";

/**
* The Spanish translation of the `switchbind` command.
*/
export class SwitchbindESTranslation extends Translation<SwitchbindStrings> {
override readonly translations: SwitchbindStrings = {
invalidUid: "Hey, por favor ingresa un uid válido!",
uidNotBinded: "Lo siento, ese uid no esta asociado a nadie!",
switchFailed: "Lo siento, no puedo cambiar el enlace: %s.",
switchSuccessful: "Enlace cambiado correctamente.",
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Translation } from "@localization/base/Translation";
import { SwitchbindStrings } from "../SwitchbindLocalization";

/**
* The Indonesian translation of the `switchbind` command.
*/
export class SwitchbindIDTranslation extends Translation<SwitchbindStrings> {
override readonly translations: SwitchbindStrings = {
invalidUid: "Hei, mohon berikan uid yang benar!",
uidNotBinded: "Maaf, uid ini tidak terhubung ke seorang pengguna!",
switchFailed: "Maaf, aku tidak bisa memindahkan uid tersebut: %s.",
switchSuccessful: "Berhasil memindahkan uid.",
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Translation } from "@localization/base/Translation";
import { SwitchbindStrings } from "../SwitchbindLocalization";

/**
* The Korean translation of the `switchbind` command.
*/
export class SwitchbindKRTranslation extends Translation<SwitchbindStrings> {
override readonly translations: SwitchbindStrings = {
invalidUid: "저기, 올바른 uid를 입력해 주세요!",
uidNotBinded: "죄송해요, 이 uid는 누구에게도 바인딩 되어있지 않아요!",
switchFailed: "죄송해요, 바인딩을 변경할 수 없어요: %s.",
switchSuccessful: "성공적으로 바인딩을 변경했어요.",
};
}

0 comments on commit 1bd1968

Please sign in to comment.