aboutsummaryrefslogtreecommitdiff
path: root/lib/extensions/discord.js/ExtendedGuild.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/extensions/discord.js/ExtendedGuild.ts')
-rw-r--r--lib/extensions/discord.js/ExtendedGuild.ts137
1 files changed, 75 insertions, 62 deletions
diff --git a/lib/extensions/discord.js/ExtendedGuild.ts b/lib/extensions/discord.js/ExtendedGuild.ts
index 84db7d0..2c1fd62 100644
--- a/lib/extensions/discord.js/ExtendedGuild.ts
+++ b/lib/extensions/discord.js/ExtendedGuild.ts
@@ -23,7 +23,6 @@ import {
PermissionFlagsBits,
SnowflakeUtil,
ThreadChannel,
- WebhookCreateMessageOptions,
type APIMessage,
type GuildMember,
type GuildMemberResolvable,
@@ -35,11 +34,12 @@ import {
type User,
type UserResolvable,
type VoiceChannel,
- type Webhook
+ type Webhook,
+ type WebhookCreateMessageOptions
} from 'discord.js';
import { camelCase } from 'lodash-es';
import { TanzaniteClient } from '../discord-akairo/TanzaniteClient.js';
-import { banResponse, BanResponse, dmResponse, permissionsResponse, punishmentEntryRemove } from './ExtendedGuildMember.js';
+import { banResponse, BanResponse, dmResponse, permissionsResponse, punishmentEntryError } from './ExtendedGuildMember.js';
interface Extension {
/**
@@ -113,7 +113,7 @@ interface Extension {
* @param options Options for banning the user.
* @returns A string status message of the ban.
* **Preconditions:**
- * - {@link me} has the `BanMembers` permission
+ * - {@link members.me} has the `BanMembers` permission
* **Warning:**
* - Doesn't emit customBan Event
*/
@@ -230,15 +230,15 @@ export class ExtendedGuild extends Guild implements Extension {
public override async customBan(options: GuildCustomBanOptions): Promise<BanResponse> {
// checks
- if (!this.members.me!.permissions.has(PermissionFlagsBits.BanMembers)) return banResponse.MISSING_PERMISSIONS;
+ if (!this.members.me!.permissions.has(PermissionFlagsBits.BanMembers)) return banResponse.MissingPermissions;
- let caseID: string | undefined = undefined;
- let dmSuccessEvent: boolean | undefined = undefined;
+ let caseID: string | null = null;
+ let dmSuccess: boolean | null = null;
const user = await this.client.utils.resolveNonCachedUser(options.user);
const moderator = this.client.users.resolve(options.moderator ?? this.client.user!);
- if (!user || !moderator) return banResponse.CANNOT_RESOLVE_USER;
+ if (!user || !moderator) return banResponse.CannotResolveUser;
- if ((await this.bans.fetch()).has(user.id)) return banResponse.ALREADY_BANNED;
+ if ((await this.bans.fetch()).has(user.id)) return banResponse.AlreadyBanned;
const ret = await (async () => {
// add modlog entry
@@ -252,20 +252,22 @@ export class ExtendedGuild extends Guild implements Extension {
guild: this,
evidence: options.evidence
});
- if (!modlog) return banResponse.MODLOG_ERROR;
+ if (!modlog) return banResponse.ModlogError;
caseID = modlog.id;
- // dm user
- dmSuccessEvent = await punishDM({
- client: this.client,
- modlog: modlog.id,
- guild: this,
- user: user,
- punishment: Action.Ban,
- duration: options.duration ?? 0,
- reason: options.reason ?? undefined,
- sendFooter: true
- });
+ if (!options.noDM) {
+ // dm user
+ dmSuccess = await punishDM({
+ client: this.client,
+ modlog: modlog.id,
+ guild: this,
+ user: user,
+ punishment: Action.Ban,
+ duration: options.duration ?? 0,
+ reason: options.reason ?? undefined,
+ sendFooter: true
+ });
+ }
// ban
const banSuccess = await this.bans
@@ -274,7 +276,7 @@ export class ExtendedGuild extends Guild implements Extension {
deleteMessageDays: options.deleteDays
})
.catch(() => false);
- if (!banSuccess) return banResponse.ACTION_ERROR;
+ if (!banSuccess) return banResponse.ActionError;
// add punishment entry so they can be unbanned later
const punishmentEntrySuccess = await createPunishmentEntry({
@@ -285,13 +287,16 @@ export class ExtendedGuild extends Guild implements Extension {
duration: options.duration,
modlog: modlog.id
});
- if (!punishmentEntrySuccess) return banResponse.PUNISHMENT_ENTRY_ADD_ERROR;
+ if (!punishmentEntrySuccess) return banResponse.PunishmentEntryError;
- if (!dmSuccessEvent) return banResponse.DM_ERROR;
- return banResponse.SUCCESS;
+ if (!options.noDM && !dmSuccess) {
+ return banResponse.DmError;
+ }
+
+ return banResponse.Success;
})();
- if (!([banResponse.ACTION_ERROR, banResponse.MODLOG_ERROR, banResponse.PUNISHMENT_ENTRY_ADD_ERROR] as const).includes(ret))
+ if (!([banResponse.ActionError, banResponse.ModlogError, banResponse.PunishmentEntryError] as const).includes(ret))
this.client.emit(
TanzaniteEvent.Ban,
user,
@@ -300,14 +305,14 @@ export class ExtendedGuild extends Guild implements Extension {
options.reason ?? undefined,
caseID!,
options.duration ?? 0,
- dmSuccessEvent,
+ dmSuccess,
options.evidence
);
return ret;
}
public override async massBanOne(options: GuildMassBanOneOptions): Promise<BanResponse> {
- if (this.bans.cache.has(options.user)) return banResponse.ALREADY_BANNED;
+ if (this.bans.cache.has(options.user)) return banResponse.AlreadyBanned;
const ret = await (async () => {
// add modlog entry
@@ -320,7 +325,7 @@ export class ExtendedGuild extends Guild implements Extension {
duration: 0,
guild: this.id
});
- if (!modlog) return banResponse.MODLOG_ERROR;
+ if (!modlog) return banResponse.ModlogError;
let dmSuccessEvent: boolean | undefined = undefined;
// dm user
@@ -344,7 +349,7 @@ export class ExtendedGuild extends Guild implements Extension {
deleteMessageDays: options.deleteDays
})
.catch(() => false);
- if (!banSuccess) return banResponse.ACTION_ERROR;
+ if (!banSuccess) return banResponse.ActionError;
// add punishment entry so they can be unbanned later
const punishmentEntrySuccess = await createPunishmentEntry({
@@ -355,29 +360,29 @@ export class ExtendedGuild extends Guild implements Extension {
duration: 0,
modlog: modlog.id
});
- if (!punishmentEntrySuccess) return banResponse.PUNISHMENT_ENTRY_ADD_ERROR;
+ if (!punishmentEntrySuccess) return banResponse.PunishmentEntryError;
- if (!dmSuccessEvent) return banResponse.DM_ERROR;
- return banResponse.SUCCESS;
+ if (!dmSuccessEvent) return banResponse.DmError;
+ return banResponse.Success;
})();
return ret;
}
public override async customUnban(options: GuildCustomUnbanOptions): Promise<UnbanResponse> {
// checks
- if (!this.members.me!.permissions.has(PermissionFlagsBits.BanMembers)) return unbanResponse.MISSING_PERMISSIONS;
+ if (!this.members.me!.permissions.has(PermissionFlagsBits.BanMembers)) return unbanResponse.MissingPermissions;
- let caseID: string | undefined = undefined;
- let dmSuccessEvent: boolean | undefined = undefined;
+ let caseID: string | null = null;
+ let dmSuccess: boolean | null = null;
const user = await this.client.utils.resolveNonCachedUser(options.user);
const moderator = this.client.users.resolve(options.moderator ?? this.client.user!);
- if (!user || !moderator) return unbanResponse.CANNOT_RESOLVE_USER;
+ if (!user || !moderator) return unbanResponse.CannotResolveUser;
const ret = await (async () => {
- const bans = await this.bans.fetch();
+ const ban = await this.bans.fetch(user.id);
let notBanned = false;
- if (!bans.has(user.id)) notBanned = true;
+ if (ban?.user?.id === user.id) notBanned = true;
const unbanSuccess = await this.bans
.remove(user, `${moderator.tag} | ${options.reason ?? 'No reason provided.'}`)
@@ -388,8 +393,8 @@ export class ExtendedGuild extends Guild implements Extension {
} else return false;
});
- if (notBanned) return unbanResponse.NOT_BANNED;
- if (!unbanSuccess) return unbanResponse.ACTION_ERROR;
+ if (notBanned) return unbanResponse.NotBanned;
+ if (!unbanSuccess) return unbanResponse.ActionError;
// add modlog entry
const { log: modlog } = await createModLogEntry({
@@ -401,7 +406,7 @@ export class ExtendedGuild extends Guild implements Extension {
guild: this,
evidence: options.evidence
});
- if (!modlog) return unbanResponse.MODLOG_ERROR;
+ if (!modlog) return unbanResponse.ModlogError;
caseID = modlog.id;
// remove punishment entry
@@ -411,26 +416,24 @@ export class ExtendedGuild extends Guild implements Extension {
user: user.id,
guild: this
});
- if (!removePunishmentEntrySuccess) return unbanResponse.PUNISHMENT_ENTRY_REMOVE_ERROR;
+ if (!removePunishmentEntrySuccess) return unbanResponse.PunishmentEntryError;
- // dm user
- dmSuccessEvent = await punishDM({
- client: this.client,
- guild: this,
- user: user,
- punishment: Action.Unban,
- reason: options.reason ?? undefined,
- sendFooter: false
- });
+ if (!options.noDM) {
+ // dm user
+ dmSuccess = await punishDM({
+ client: this.client,
+ guild: this,
+ user: user,
+ punishment: Action.Unban,
+ reason: options.reason ?? undefined,
+ sendFooter: false
+ });
- if (!dmSuccessEvent) return unbanResponse.DM_ERROR;
- return unbanResponse.SUCCESS;
+ if (dmSuccess === false) return unbanResponse.DmError;
+ }
+ return unbanResponse.Success;
})();
- if (
- !([unbanResponse.ACTION_ERROR, unbanResponse.MODLOG_ERROR, unbanResponse.PUNISHMENT_ENTRY_REMOVE_ERROR] as const).includes(
- ret
- )
- )
+ if (!([unbanResponse.ActionError, unbanResponse.ModlogError, unbanResponse.PunishmentEntryError] as const).includes(ret))
this.client.emit(
TanzaniteEvent.Unban,
user,
@@ -438,7 +441,7 @@ export class ExtendedGuild extends Guild implements Extension {
this,
options.reason ?? undefined,
caseID!,
- dmSuccessEvent!,
+ dmSuccess,
options.evidence
);
return ret;
@@ -756,6 +759,11 @@ export interface GuildCustomUnbanOptions {
* The evidence for the unban
*/
evidence?: string;
+
+ /**
+ * Don't send a dm to the user.
+ */
+ noDM?: boolean;
}
export interface GuildMassBanOneOptions {
@@ -813,6 +821,11 @@ export interface GuildCustomBanOptions {
* The evidence for the ban
*/
evidence?: string;
+
+ /**
+ * Don't send a dm to the user.
+ */
+ noDM?: boolean;
}
type ValueOf<T> = T[keyof T];
@@ -820,8 +833,8 @@ type ValueOf<T> = T[keyof T];
export const unbanResponse = Object.freeze({
...dmResponse,
...permissionsResponse,
- ...punishmentEntryRemove,
- NOT_BANNED: 'user not banned'
+ ...punishmentEntryError,
+ NotBanned: 'user not banned'
} as const);
/**