From a1a70ba33df54953b411709140578254a15051c7 Mon Sep 17 00:00:00 2001 From: Kukuri <83948007+Mikasuru@users.noreply.github.com> Date: Thu, 17 Oct 2024 10:37:31 +0700 Subject: [PATCH] Add the give role command This command will give roles to the player, No need to re-enter the world the refresh the Name and Flags, Based on Role Object in Constants. - Supports giving roles by name or ID - Can assign roles to self or other online players - Immediately updates country state and player name Example: /giveroles <1|developer|Developer|DEVELOPER> --- src/commands/GiveRoles.ts | 77 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/commands/GiveRoles.ts diff --git a/src/commands/GiveRoles.ts b/src/commands/GiveRoles.ts new file mode 100644 index 0000000..f7502b2 --- /dev/null +++ b/src/commands/GiveRoles.ts @@ -0,0 +1,77 @@ +import { Variant } from "growtopia.js"; +import { Command } from "../abstracts/Command.js"; +import { BaseServer } from "../structures/BaseServer.js"; +import { Peer } from "../structures/Peer.js"; +import type { CommandOptions } from "../types"; +import { Role } from "../utils/Constants.js"; +import { find } from "../utils/Utils.js"; + +export default class extends Command { + public opt: CommandOptions; + + constructor(base: BaseServer) { + super(base); + this.opt = { + name: "giveroles", + description: "Give roles to someone or self", + cooldown: 5, + ratelimit: 5, + category: "Developer", + usage: "/giveroles ", + example: ["/giveroles Developer", "/giveroles 1 JadlionHD", "/giveroles developer JadlionHD"], + permission: [Role.BASIC, Role.DEVELOPER] + }; + } + + // Give role to player, No need to re-enter the world to update the name/flags. + + private getRoleId(roleInput: string): string | null { + const roleEntries = Object.entries(Role); + const role = roleEntries.find(([name, id]) => + name.toLowerCase() === roleInput.toLowerCase() || id === roleInput + ); + return role ? role[1] : null; + } + + // Updates the peer role, saves changes, and refreshes their display + private updatePeerRole(peer: Peer, roleId: string): void { + peer.data.role = roleId; + peer.saveToCache(); + peer.saveToDatabase(); + + peer.countryState(); // Refresh country + + this.updateNameDisplay(peer); + } + + // Updates the player name display for all peers + private updateNameDisplay(peer: Peer): void { + const updatedName = peer.name; + + peer.send(Variant.from({ netID: peer.data.netID }, "OnNameChanged", updatedName)); // Update the name + + peer.everyPeer((otherPeer) => { + if (otherPeer.data.netID !== peer.data.netID && otherPeer.data.world === peer.data.world) { + otherPeer.send(Variant.from({ netID: peer.data.netID }, "OnNameChanged", updatedName)); + } + }); + } + + public async execute(peer: Peer, text: string, args: string[]): Promise { + if (!args[0]) return peer.send(Variant.from("OnConsoleMessage", "Role is required.")); + + const roleId = this.getRoleId(args[0]); + if (!roleId) return peer.send(Variant.from("OnConsoleMessage", "Invalid role.")); + + if (args.length > 1) { + const targetPeer = find(this.base, this.base.cache.users, (user) => (user.data?.tankIDName || "").toLowerCase().includes(args[1].toLowerCase())); + if (!targetPeer) return peer.send(Variant.from("OnConsoleMessage", "Make sure that player is online.")); + + this.updatePeerRole(targetPeer, roleId); + peer.send(Variant.from("OnConsoleMessage", `Successfully gave the \`w${args[0]}\`\` role to ${targetPeer.name}`)); + } else { + this.updatePeerRole(peer, roleId); + peer.send(Variant.from("OnConsoleMessage", `Successfully received \`w${args[0]}\`\` role.`)); + } + } +}