feat(Channel): improve typeguards (#6957)

Co-authored-by: Papageorgiadis Savvas <50584606+papsavas@users.noreply.github.com>
Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: Almeida <almeidx@pm.me>
This commit is contained in:
Rodry
2022-01-12 19:30:06 +00:00
committed by GitHub
parent f753882592
commit 37a22e04c2
12 changed files with 91 additions and 19 deletions

View File

@@ -8,7 +8,7 @@ class MessageCreateAction extends Action {
const client = this.client; const client = this.client;
const channel = this.getChannel(data); const channel = this.getChannel(data);
if (channel) { if (channel) {
if (!channel.isText()) return {}; if (!channel.isTextBased()) return {};
const existing = channel.messages.cache.get(data.id); const existing = channel.messages.cache.get(data.id);
if (existing) return { message: existing }; if (existing) return { message: existing };

View File

@@ -9,7 +9,7 @@ class MessageDeleteAction extends Action {
const channel = this.getChannel(data); const channel = this.getChannel(data);
let message; let message;
if (channel) { if (channel) {
if (!channel.isText()) return {}; if (!channel.isTextBased()) return {};
message = this.getMessage(data, channel); message = this.getMessage(data, channel);
if (message) { if (message) {

View File

@@ -10,7 +10,7 @@ class MessageDeleteBulkAction extends Action {
const channel = client.channels.cache.get(data.channel_id); const channel = client.channels.cache.get(data.channel_id);
if (channel) { if (channel) {
if (!channel.isText()) return {}; if (!channel.isTextBased()) return {};
const ids = data.ids; const ids = data.ids;
const messages = new Collection(); const messages = new Collection();

View File

@@ -23,7 +23,7 @@ class MessageReactionAdd extends Action {
// Verify channel // Verify channel
const channel = this.getChannel(data); const channel = this.getChannel(data);
if (!channel || !channel.isText()) return false; if (!channel || !channel.isTextBased()) return false;
// Verify message // Verify message
const message = this.getMessage(data, channel); const message = this.getMessage(data, channel);

View File

@@ -20,7 +20,7 @@ class MessageReactionRemove extends Action {
// Verify channel // Verify channel
const channel = this.getChannel(data); const channel = this.getChannel(data);
if (!channel || !channel.isText()) return false; if (!channel || !channel.isTextBased()) return false;
// Verify message // Verify message
const message = this.getMessage(data, channel); const message = this.getMessage(data, channel);

View File

@@ -7,7 +7,7 @@ class MessageReactionRemoveAll extends Action {
handle(data) { handle(data) {
// Verify channel // Verify channel
const channel = this.getChannel(data); const channel = this.getChannel(data);
if (!channel || !channel.isText()) return false; if (!channel || !channel.isTextBased()) return false;
// Verify message // Verify message
const message = this.getMessage(data, channel); const message = this.getMessage(data, channel);

View File

@@ -6,7 +6,7 @@ const { Events } = require('../../util/Constants');
class MessageReactionRemoveEmoji extends Action { class MessageReactionRemoveEmoji extends Action {
handle(data) { handle(data) {
const channel = this.getChannel(data); const channel = this.getChannel(data);
if (!channel || !channel.isText()) return false; if (!channel || !channel.isTextBased()) return false;
const message = this.getMessage(data, channel); const message = this.getMessage(data, channel);
if (!message) return false; if (!message) return false;

View File

@@ -6,7 +6,7 @@ class MessageUpdateAction extends Action {
handle(data) { handle(data) {
const channel = this.getChannel(data); const channel = this.getChannel(data);
if (channel) { if (channel) {
if (!channel.isText()) return {}; if (!channel.isTextBased()) return {};
const { id, channel_id, guild_id, author, timestamp, type } = data; const { id, channel_id, guild_id, author, timestamp, type } = data;
const message = this.getMessage({ id, channel_id, guild_id, author, timestamp, type }, channel); const message = this.getMessage({ id, channel_id, guild_id, author, timestamp, type }, channel);

View File

@@ -9,7 +9,7 @@ class TypingStart extends Action {
const channel = this.getChannel(data); const channel = this.getChannel(data);
if (!channel) return; if (!channel) return;
if (!channel.isText()) { if (!channel.isTextBased()) {
this.client.emit(Events.WARN, `Discord sent a typing packet to a ${channel.type} channel ${channel.id}`); this.client.emit(Events.WARN, `Discord sent a typing packet to a ${channel.type} channel ${channel.id}`);
return; return;
} }

View File

@@ -3,6 +3,7 @@
const { DiscordSnowflake } = require('@sapphire/snowflake'); const { DiscordSnowflake } = require('@sapphire/snowflake');
const { ChannelType } = require('discord-api-types/v9'); const { ChannelType } = require('discord-api-types/v9');
const Base = require('./Base'); const Base = require('./Base');
const { ThreadChannelTypes } = require('../util/Constants');
let CategoryChannel; let CategoryChannel;
let DMChannel; let DMChannel;
let NewsChannel; let NewsChannel;
@@ -11,7 +12,6 @@ let StoreChannel;
let TextChannel; let TextChannel;
let ThreadChannel; let ThreadChannel;
let VoiceChannel; let VoiceChannel;
const { ThreadChannelTypes, VoiceBasedChannelTypes } = require('../util/Constants');
/** /**
* Represents any channel on Discord. * Represents any channel on Discord.
@@ -103,19 +103,59 @@ class Channel extends Base {
} }
/** /**
* Indicates whether this channel is {@link TextBasedChannels text-based}. * Indicates whether this channel is a {@link TextChannel}.
* @returns {boolean} * @returns {boolean}
*/ */
isText() { isText() {
return 'messages' in this; return this.type === ChannelType[ChannelType.GuildText];
} }
/** /**
* Indicates whether this channel is {@link BaseGuildVoiceChannel voice-based}. * Indicates whether this channel is a {@link DMChannel}.
* @returns {boolean}
*/
isDM() {
return this.type === ChannelType[ChannelType.DM];
}
/**
* Indicates whether this channel is a {@link VoiceChannel}.
* @returns {boolean} * @returns {boolean}
*/ */
isVoice() { isVoice() {
return VoiceBasedChannelTypes.includes(this.type); return this.type === ChannelType[ChannelType.GuildVoice];
}
/**
* Indicates whether this channel is a {@link PartialGroupDMChannel}.
* @returns {boolean}
*/
isGroupDM() {
return this.type === ChannelType[ChannelType.GroupDM];
}
/**
* Indicates whether this channel is a {@link CategoryChannel}.
* @returns {boolean}
*/
isCategory() {
return this.type === ChannelType[ChannelType.GuildCategory];
}
/**
* Indicates whether this channel is a {@link NewsChannel}.
* @returns {boolean}
*/
isNews() {
return this.type === ChannelType[ChannelType.GuildNews];
}
/**
* Indicates whether this channel is a {@link StoreChannel}.
* @returns {boolean}
*/
isStore() {
return this.type === ChannelType[ChannelType.GuildStore];
} }
/** /**
@@ -126,6 +166,30 @@ class Channel extends Base {
return ThreadChannelTypes.includes(this.type); return ThreadChannelTypes.includes(this.type);
} }
/**
* Indicates whether this channel is a {@link StageChannel}.
* @returns {boolean}
*/
isStage() {
return this.type === ChannelType[ChannelType.GuildStageVoice];
}
/**
* Indicates whether this channel is {@link TextBasedChannels text-based}.
* @returns {boolean}
*/
isTextBased() {
return 'messages' in this;
}
/**
* Indicates whether this channel is {@link BaseGuildVoiceChannel voice-based}.
* @returns {boolean}
*/
isVoiceBased() {
return 'bitrate' in this;
}
static create(client, data, guild, { allowUnknownGuild, fromInteraction } = {}) { static create(client, data, guild, { allowUnknownGuild, fromInteraction } = {}) {
CategoryChannel ??= require('./CategoryChannel'); CategoryChannel ??= require('./CategoryChannel');
DMChannel ??= require('./DMChannel'); DMChannel ??= require('./DMChannel');

View File

@@ -136,7 +136,7 @@ class Sweepers {
let messages = 0; let messages = 0;
for (const channel of this.client.channels.cache.values()) { for (const channel of this.client.channels.cache.values()) {
if (!channel.isText()) continue; if (!channel.isTextBased()) continue;
channels++; channels++;
messages += channel.messages.cache.sweep(filter); messages += channel.messages.cache.sweep(filter);
@@ -168,7 +168,7 @@ class Sweepers {
let reactions = 0; let reactions = 0;
for (const channel of this.client.channels.cache.values()) { for (const channel of this.client.channels.cache.values()) {
if (!channel.isText()) continue; if (!channel.isTextBased()) continue;
channels++; channels++;
for (const message of channel.messages.cache.values()) { for (const message of channel.messages.cache.values()) {

View File

@@ -520,9 +520,17 @@ export abstract class Channel extends Base {
public type: keyof typeof ChannelType; public type: keyof typeof ChannelType;
public delete(): Promise<this>; public delete(): Promise<this>;
public fetch(force?: boolean): Promise<this>; public fetch(force?: boolean): Promise<this>;
public isText(): this is TextBasedChannel; public isText(): this is TextChannel;
public isVoice(): this is BaseGuildVoiceChannel; public isDM(): this is DMChannel;
public isVoice(): this is VoiceChannel;
public isGroupDM(): this is PartialGroupDMChannel;
public isCategory(): this is CategoryChannel;
public isNews(): this is NewsChannel;
public isStore(): this is StoreChannel;
public isThread(): this is ThreadChannel; public isThread(): this is ThreadChannel;
public isStage(): this is StageChannel;
public isTextBased(): this is TextBasedChannel;
public isVoiceBased(): this is VoiceBasedChannel;
public toString(): ChannelMention; public toString(): ChannelMention;
} }
@@ -1051,7 +1059,7 @@ export abstract class GuildChannel extends Channel {
public setName(name: string, reason?: string): Promise<this>; public setName(name: string, reason?: string): Promise<this>;
public setParent(channel: CategoryChannelResolvable | null, options?: SetParentOptions): Promise<this>; public setParent(channel: CategoryChannelResolvable | null, options?: SetParentOptions): Promise<this>;
public setPosition(position: number, options?: SetChannelPositionOptions): Promise<this>; public setPosition(position: number, options?: SetChannelPositionOptions): Promise<this>;
public isText(): this is TextChannel | NewsChannel; public isTextBased(): this is GuildBasedChannel & TextBasedChannel;
} }
export class GuildEmoji extends BaseGuildEmoji { export class GuildEmoji extends BaseGuildEmoji {