Skip to content

Commit

Permalink
Promo codes
Browse files Browse the repository at this point in the history
  • Loading branch information
AL1L committed Nov 1, 2023
1 parent 0b88623 commit a98b61b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/BloxAdmin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ importTime("DebugUI");
import Metrics from "modules/Metrics";
importTime("Metrics");
import Moderation from "modules/Moderation";
import PromoCodes from "modules/PromoCodes";
importTime("Moderation");
import RemoteConfig from "modules/RemoteConfig";
importTime("RemoteConfig");
Expand Down Expand Up @@ -141,6 +142,7 @@ export class BloxAdmin extends EventEmitter<{ ready: [] }> {
this.loadModule(() => new Moderation(this));
this.loadModule(() => new Actions(this));
this.loadModule(() => new Metrics(this));
this.loadModule(() => new PromoCodes(this));
this.logger.debug(`Loaded modules in ${os.clock() - loadStart}s`);

this.messenger.on("message", (message) => {
Expand Down
9 changes: 9 additions & 0 deletions src/RemoteMessaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ export default class RemoteMessaging<M extends defined> extends EventEmitter<{
});
}

public put(url: string, body: unknown) {
return HttpService.RequestAsync({
Method: "PUT",
Headers: this.reqHeaders(),
Url: url,
Body: HttpService.JSONEncode(body),
});
}

public async setup(): Promise<void> {
const result = this.get(this.url);

Expand Down
2 changes: 1 addition & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Config } from "types";

export const BLOXADMIN_VERSION = 123;
export const BLOXADMIN_VERSION = 124;

export const DEFAULT_CONFIG: Config = {
api: {
Expand Down
24 changes: 12 additions & 12 deletions src/modules/Moderation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ export default class Moderation extends Module<{

switch (moderationType) {
case ModerationType.Kick:
this.kick(player, reason);
this._kick(player, reason);
break;
case ModerationType.Mute:
this.mute(player, untilTime, reason, realDuration);
this._mute(player, untilTime, reason, realDuration);
break;
case ModerationType.Unmute:
this.unmute(player, reason);
Expand Down Expand Up @@ -161,7 +161,7 @@ export default class Moderation extends Module<{

spawn(() => {
delay(1, () => {
this.unmuteCheck(true)
this._unmuteCheck(true)
});
})
}
Expand All @@ -185,12 +185,12 @@ export default class Moderation extends Module<{
return ChatService;
}

kick(player: Player, reason?: string) {
private _kick(player: Player, reason?: string) {
this.logger.info(`Kicking ${player.Name} for ${reason}`);
player.Kick(reason);
}

private muteLegacy(player: Player, seconds?: number, reason?: string) {
private _muteLegacy(player: Player, seconds?: number, reason?: string) {
const ChatService = this.chatService()
if (!ChatService) return;

Expand All @@ -209,7 +209,7 @@ export default class Moderation extends Module<{

// Chat

unmuteCheck(loop = false) {
private _unmuteCheck(loop = false) {
for (const [playerId, untilTime] of pairs(this.playersMutedUntil)) {
if (untilTime !== -1 && untilTime < os.time()) {
const player = Players.GetPlayerByUserId(playerId);
Expand All @@ -220,11 +220,11 @@ export default class Moderation extends Module<{

if (loop)
delay(1, () => {
this.unmuteCheck(true)
this._unmuteCheck(true)
});
}

isPlayerMuted(player: Player | number) {
private _isPlayerMuted(player: Player | number) {
const playerId = typeIs(player, "number") ? player : player.UserId;
return this.playersMutedUntil[playerId] && (this.playersMutedUntil[playerId] === -1 || this.playersMutedUntil[playerId] > os.time());
}
Expand All @@ -237,15 +237,15 @@ export default class Moderation extends Module<{
const player = Players.GetPlayerByUserId(playerId);
if (!player) return true;

const isMuted = this.isPlayerMuted(player);
const isMuted = this._isPlayerMuted(player);

if (isMuted && (!this.lastNotifiedMuted[playerId] || this.lastNotifiedMuted[playerId] + NOTIFICATION_COOLDOWN < os.time())) {
this.lastNotifiedMuted[playerId] = os.time();
this.systemMessageEvent.FireClient(player, "You are muted and your messages will not be seen by others.");
} else if (!isMuted && message.TextChannel && message.TextChannel.Name.sub(1, 10) === "RBXWhisper") {
const destinationPlayer = Players.GetPlayerByUserId(destination.UserId);
if (destinationPlayer) {
const destinationIsMuted = this.isPlayerMuted(destinationPlayer);
const destinationIsMuted = this._isPlayerMuted(destinationPlayer);

if (destinationIsMuted) {
this.systemMessageEvent.FireClient(player, "The player you are trying to whisper is muted and cannot respond.");
Expand All @@ -257,14 +257,14 @@ export default class Moderation extends Module<{
}
}

mute(player: Player, untilTime?: number, reason?: string, realDuration?: number) {
private _mute(player: Player, untilTime?: number, reason?: string, realDuration?: number) {
this.logger.info(`Muting ${player.Name} for ${reason} until ${untilTime}`);
this.playersMutedUntil[player.UserId] = untilTime || -1;

const seconds = untilTime ? untilTime - os.time() : undefined;

if (TextChatService.ChatVersion === Enum.ChatVersion.LegacyChatService) {
this.muteLegacy(player, seconds, reason);
this._muteLegacy(player, seconds, reason);
return;
}
const r = reason ? ` for ${reason}` : "";
Expand Down
47 changes: 47 additions & 0 deletions src/modules/PromoCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BloxAdmin } from "BloxAdmin";
import { Module } from "Module";

const HttpService = game.GetService("HttpService");

export interface CreatePromoCode {
attributes: Record<string, unknown>;
uses?: number;
active: boolean;
starts?: string;
expires?: string;
}

export interface PromoCode extends CreatePromoCode {
code: string;
used: number;
created: string;
}


export default class PromoCodes extends Module {
constructor(admin: BloxAdmin) {
super("PromoCodes", admin);
}

enable(): void { }

ClaimCode(player: Player | number, code: string): Promise<PromoCode> {
return new Promise((resolve, reject) => {
const playerId = typeIs(player, "number") ? player : player.UserId;

const url = `${this.admin.config.api.base}/games/${game.GameId}/codes/${code}/uses/${playerId}`

this.logger.debug("PUT to " + url);

const response = this.admin.messenger.put(url, {
serverId: this.admin.serverId(),
});

if (!response.Success) {
return reject(response.Body);
}

return resolve(HttpService.JSONDecode(response.Body) as PromoCode);
});
}
}

0 comments on commit a98b61b

Please sign in to comment.