diff options
Diffstat (limited to 'src/lib/extensions/discord.js')
-rw-r--r-- | src/lib/extensions/discord.js/BushBaseGuildTextChannel.ts | 10 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuild.ts | 63 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushGuildMember.ts | 17 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushNewsChannel.ts | 4 | ||||
-rw-r--r-- | src/lib/extensions/discord.js/BushTextChannel.ts | 4 |
5 files changed, 77 insertions, 21 deletions
diff --git a/src/lib/extensions/discord.js/BushBaseGuildTextChannel.ts b/src/lib/extensions/discord.js/BushBaseGuildTextChannel.ts index 734642e..78cfada 100644 --- a/src/lib/extensions/discord.js/BushBaseGuildTextChannel.ts +++ b/src/lib/extensions/discord.js/BushBaseGuildTextChannel.ts @@ -1,4 +1,10 @@ -import { AllowedThreadTypeForTextChannel, BaseGuildTextChannel, Collection, Snowflake } from 'discord.js'; +import { + AllowedThreadTypeForNewsChannel, + AllowedThreadTypeForTextChannel, + BaseGuildTextChannel, + Collection, + Snowflake +} from 'discord.js'; import { RawGuildChannelData } from 'discord.js/typings/rawDataTypes'; import { BushCategoryChannel, BushClient, BushGuildMember } from '../..'; import { BushGuild } from './BushGuild'; @@ -10,7 +16,7 @@ export class BushBaseGuildTextChannel extends BaseGuildTextChannel { super(guild, data, client, immediatePatch); } public declare messages: BushMessageManager; - public declare threads: BushThreadManager<AllowedThreadTypeForTextChannel>; + public declare threads: BushThreadManager<AllowedThreadTypeForTextChannel | AllowedThreadTypeForNewsChannel>; public declare readonly client: BushClient; public declare guild: BushGuild; public declare readonly members: Collection<Snowflake, BushGuildMember>; diff --git a/src/lib/extensions/discord.js/BushGuild.ts b/src/lib/extensions/discord.js/BushGuild.ts index 09e355c..12db49a 100644 --- a/src/lib/extensions/discord.js/BushGuild.ts +++ b/src/lib/extensions/discord.js/BushGuild.ts @@ -1,4 +1,4 @@ -import { Guild } from 'discord.js'; +import { Guild, UserResolvable } from 'discord.js'; import { RawGuildData } from 'discord.js/typings/rawDataTypes'; import { Guild as GuildDB, GuildFeatures, GuildModel } from '../../models/Guild'; import { ModLogType } from '../../models/ModLog'; @@ -52,9 +52,56 @@ export class BushGuild extends Guild { return await row.save(); } + public async ban(options: { + user: BushUserResolvable | UserResolvable; + reason?: string | null; + moderator?: BushUserResolvable; + duration?: number; + deleteDays?: number; + }): Promise< + 'success' | 'missing permissions' | 'error banning' | 'error creating modlog entry' | 'error creating ban entry' + > { + // checks + if (!this.me!.permissions.has('BAN_MEMBERS')) return 'missing permissions'; + + const moderator = (await util.resolveNonCachedUser(options.moderator!)) ?? client.user!; + + // ban + const banSuccess = await this.bans + .create(options.user, { + reason: `${moderator.tag} | ${options.reason ?? 'No reason provided.'}`, + days: options.deleteDays + }) + .catch(() => false); + if (!banSuccess) return 'error banning'; + + // add modlog entry + const { log: modlog } = await util.createModLogEntry({ + type: options.duration ? ModLogType.TEMP_BAN : ModLogType.PERM_BAN, + user: options.user as BushUserResolvable, + moderator: moderator.id, + reason: options.reason, + duration: options.duration, + guild: this + }); + if (!modlog) return 'error creating modlog entry'; + + // add punishment entry so they can be unbanned later + const punishmentEntrySuccess = await util.createPunishmentEntry({ + type: 'ban', + user: options.user as BushUserResolvable, + guild: this, + duration: options.duration, + modlog: modlog.id + }); + if (!punishmentEntrySuccess) return 'error creating ban entry'; + + return 'success'; + } + public async unban(options: { user: BushUserResolvable | BushUser; - reason?: string; + reason?: string | null; moderator?: BushUserResolvable; }): Promise< | 'success' @@ -64,13 +111,13 @@ export class BushGuild extends Guild { | 'error creating modlog entry' | 'error removing ban entry' > { - const user = client.users.resolveId(options.user)!; - const moderator = client.users.cache.get(client.users.resolveId(options.moderator!)!)!; + const user = (await util.resolveNonCachedUser(options.user))!; + const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.me))!; const bans = await this.bans.fetch(); let notBanned = false; - if (!bans.has(user)) notBanned = true; + if (!bans.has(user.id)) notBanned = true; const unbanSuccess = await this.bans .remove(user, `${moderator.tag} | ${options.reason ?? 'No reason provided.'}`) @@ -86,7 +133,7 @@ export class BushGuild extends Guild { // add modlog entry const modlog = await util.createModLogEntry({ type: ModLogType.UNBAN, - user, + user: user.id, moderator: moderator.id, reason: options.reason, guild: this @@ -96,12 +143,12 @@ export class BushGuild extends Guild { // remove punishment entry const removePunishmentEntrySuccess = await util.removePunishmentEntry({ type: 'ban', - user, + user: user.id, guild: this }); if (!removePunishmentEntrySuccess) return 'error removing ban entry'; - const userObject = client.users.cache.get(user); + const userObject = client.users.cache.get(user.id); userObject?.send(`You have been unbanned from **${this}** for **${options.reason ?? 'No reason provided'}**.`); diff --git a/src/lib/extensions/discord.js/BushGuildMember.ts b/src/lib/extensions/discord.js/BushGuildMember.ts index f71a435..67fa2fa 100644 --- a/src/lib/extensions/discord.js/BushGuildMember.ts +++ b/src/lib/extensions/discord.js/BushGuildMember.ts @@ -8,7 +8,7 @@ import { BushRole } from './BushRole'; import { BushUser } from './BushUser'; interface BushPunishmentOptions { - reason?: string; + reason?: string | null; moderator?: BushUserResolvable; } @@ -87,7 +87,8 @@ export class BushGuildMember extends GuildMember { } public async warn(options: BushPunishmentOptions): Promise<{ result: WarnResponse | null; caseNum: number | null }> { - const moderator = client.users.cache.get(client.users.resolveId(options.moderator!)!) ?? client.user!; + const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!; + // add modlog entry const result = await util.createModLogEntry( { @@ -119,7 +120,7 @@ export class BushGuildMember extends GuildMember { const ifShouldAddRole = this.#checkIfShouldAddRole(options.role); if (ifShouldAddRole !== true) return ifShouldAddRole; - const moderator = client.users.cache.get(client.users.resolveId(options.moderator!)!) ?? client.user!; + const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!; if (options.addToModlog || options.duration) { const { log: modlog } = options.addToModlog @@ -159,7 +160,7 @@ export class BushGuildMember extends GuildMember { const ifShouldAddRole = this.#checkIfShouldAddRole(options.role); if (ifShouldAddRole !== true) return ifShouldAddRole; - const moderator = client.users.cache.get(client.users.resolveId(options.moderator!)!) ?? client.user!; + const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!; if (options.addToModlog) { const { log: modlog } = await util.createModLogEntry({ @@ -207,7 +208,7 @@ export class BushGuildMember extends GuildMember { if (!muteRole) return 'invalid mute role'; if (muteRole.position >= this.guild.me!.roles.highest.position || muteRole.managed) return 'mute role not manageable'; - const moderator = client.users.cache.get(client.users.resolveId(options.moderator!)!) ?? client.user!; + const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!; // add role const muteSuccess = await this.roles @@ -264,7 +265,7 @@ export class BushGuildMember extends GuildMember { if (!muteRole) return 'invalid mute role'; if (muteRole.position >= this.guild.me!.roles.highest.position || muteRole.managed) return 'mute role not manageable'; - const moderator = client.users.cache.get(client.users.resolveId(options.moderator!)!) ?? client.user!; + const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!; //remove role const muteSuccess = await this.roles @@ -309,7 +310,7 @@ export class BushGuildMember extends GuildMember { // checks if (!this.guild.me?.permissions.has('KICK_MEMBERS') || !this.kickable) return 'missing permissions'; - const moderator = client.users.cache.get(client.users.resolveId(options.moderator!)!) ?? client.user!; + const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!; // dm user const ending = await this.guild.getSetting('punishmentEnding'); @@ -340,7 +341,7 @@ export class BushGuildMember extends GuildMember { // checks if (!this.guild.me!.permissions.has('BAN_MEMBERS') || !this.bannable) return 'missing permissions'; - const moderator = client.users.cache.get(client.users.resolveId(options.moderator!)!) ?? client.user!; + const moderator = (await util.resolveNonCachedUser(options.moderator ?? this.guild.me))!; // dm user const ending = await this.guild.getSetting('punishmentEnding'); diff --git a/src/lib/extensions/discord.js/BushNewsChannel.ts b/src/lib/extensions/discord.js/BushNewsChannel.ts index f44ff4a..d770132 100644 --- a/src/lib/extensions/discord.js/BushNewsChannel.ts +++ b/src/lib/extensions/discord.js/BushNewsChannel.ts @@ -1,4 +1,4 @@ -import { AllowedThreadTypeForTextChannel, Collection, NewsChannel, Snowflake } from 'discord.js'; +import { AllowedThreadTypeForNewsChannel, Collection, NewsChannel, Snowflake } from 'discord.js'; import { BushClient } from '../discord-akairo/BushClient'; import { BushGuild } from './BushGuild'; import { BushGuildMember } from './BushGuildMember'; @@ -7,8 +7,8 @@ import { BushThreadManager } from './BushThreadManager'; export class BushNewsChannel extends NewsChannel { public declare readonly client: BushClient; + public declare threads: BushThreadManager<AllowedThreadTypeForNewsChannel>; public declare guild: BushGuild; public declare messages: BushMessageManager; public declare members: Collection<Snowflake, BushGuildMember>; - public declare threads: BushThreadManager<AllowedThreadTypeForTextChannel>; } diff --git a/src/lib/extensions/discord.js/BushTextChannel.ts b/src/lib/extensions/discord.js/BushTextChannel.ts index db3ad8f..1d4d7fe 100644 --- a/src/lib/extensions/discord.js/BushTextChannel.ts +++ b/src/lib/extensions/discord.js/BushTextChannel.ts @@ -1,13 +1,15 @@ -import { TextChannel } from 'discord.js'; +import { AllowedThreadTypeForTextChannel, TextChannel } from 'discord.js'; import { RawGuildChannelData } from 'discord.js/typings/rawDataTypes'; import { BushClient } from '../discord-akairo/BushClient'; import { BushGuild } from './BushGuild'; import { BushMessageManager } from './BushMessageManager'; +import { BushThreadManager } from './BushThreadManager'; export class BushTextChannel extends TextChannel { public declare readonly client: BushClient; public declare guild: BushGuild; public declare messages: BushMessageManager; + public declare threads: BushThreadManager<AllowedThreadTypeForTextChannel>; public constructor(guild: BushGuild, data?: RawGuildChannelData) { super(guild, data); } |