diff --git a/packages/structures/src/channels/Channel.ts b/packages/structures/src/channels/Channel.ts index 54701059c..ab03b138b 100644 --- a/packages/structures/src/channels/Channel.ts +++ b/packages/structures/src/channels/Channel.ts @@ -3,7 +3,7 @@ import type { APIChannel, APIPartialChannel, ChannelType, ChannelFlags } from 'd import { Structure } from '../Structure.js'; import { ChannelFlagsBitField } from '../bitfields/ChannelFlagsBitField.js'; import { kData } from '../utils/symbols.js'; -import { isIdSet } from '../utils/type-guards.js'; +import { isFieldSet, isIdSet } from '../utils/type-guards.js'; import type { Partialize } from '../utils/types.js'; import type { ChannelPermissionMixin } from './mixins/ChannelPermissionMixin.js'; import type { ChannelWebhookMixin } from './mixins/ChannelWebhookMixin.js'; @@ -82,9 +82,9 @@ export class Channel< * to null, respecting Omit behaviors */ public get flags() { - const flags = - 'flags' in this[kData] && typeof this[kData].flags === 'number' ? (this[kData].flags as ChannelFlags) : null; - return flags ? new ChannelFlagsBitField(flags) : null; + return isFieldSet(this[kData], 'flags', 'number') + ? new ChannelFlagsBitField(this[kData].flags as ChannelFlags) + : null; } /** diff --git a/packages/structures/src/messages/Attachment.ts b/packages/structures/src/messages/Attachment.ts index e62d350e7..7bf784615 100644 --- a/packages/structures/src/messages/Attachment.ts +++ b/packages/structures/src/messages/Attachment.ts @@ -2,6 +2,7 @@ import type { APIAttachment, AttachmentFlags } from 'discord-api-types/v10'; import { Structure } from '../Structure.js'; import { AttachmentFlagsBitField } from '../bitfields/AttachmentFlagsBitField.js'; import { kData } from '../utils/symbols.js'; +import { isFieldSet } from '../utils/type-guards.js'; import type { Partialize } from '../utils/types.js'; export class Attachment extends Structure { @@ -112,7 +113,8 @@ export class Attachment extends S * Attachment flags combined as a bitfield */ public get flags() { - const flags = this[kData].flags; - return flags ? new AttachmentFlagsBitField(this[kData].flags as AttachmentFlags) : null; + return isFieldSet(this[kData], 'flags', 'number') + ? new AttachmentFlagsBitField(this[kData].flags as AttachmentFlags) + : null; } } diff --git a/packages/structures/src/messages/Message.ts b/packages/structures/src/messages/Message.ts index 7fa33895d..b30a46e77 100644 --- a/packages/structures/src/messages/Message.ts +++ b/packages/structures/src/messages/Message.ts @@ -4,7 +4,7 @@ import { Structure } from '../Structure.js'; import { MessageFlagsBitField } from '../bitfields/MessageFlagsBitField.js'; import { dateToDiscordISOTimestamp } from '../utils/optimization.js'; import { kData, kEditedTimestamp } from '../utils/symbols.js'; -import { isIdSet } from '../utils/type-guards.js'; +import { isFieldSet, isIdSet } from '../utils/type-guards.js'; import type { Partialize } from '../utils/types.js'; // TODO: missing substructures: application @@ -110,8 +110,9 @@ export class Message( + data: Value, + fieldName: Key, +): data is Record & Value { + return fieldName in data; +} + +export function isFieldSet( + data: Value, + fieldName: Key, + type: Type, +): data is Record & Value { + // eslint-disable-next-line valid-typeof + return hasProperty(data, fieldName) && typeof data[fieldName] === type; +}