harmony/src/utils/channel.ts

176 lines
5.3 KiB
TypeScript
Raw Normal View History

2021-04-04 05:42:15 +00:00
import { Client } from '../client/mod.ts'
import {
ChannelPayload,
ChannelTypes,
DMChannelPayload,
GroupDMChannelPayload,
2020-12-20 09:11:37 +00:00
GuildCategoryChannelPayload,
GuildNewsChannelPayload,
GuildTextBasedChannelPayload,
GuildTextChannelPayload,
2020-12-02 12:29:52 +00:00
GuildVoiceChannelPayload,
TextChannelPayload
2020-10-31 12:33:34 +00:00
} from '../types/channel.ts'
import { DMChannel } from '../structures/dmChannel.ts'
import { GroupDMChannel } from '../structures/groupChannel.ts'
import { CategoryChannel } from '../structures/guildCategoryChannel.ts'
import {
GuildTextBasedChannel,
GuildTextChannel
} from '../structures/guildTextChannel.ts'
import { NewsChannel } from '../structures/guildNewsChannel.ts'
import { VoiceChannel } from '../structures/guildVoiceChannel.ts'
2020-11-08 07:57:24 +00:00
import { Guild } from '../structures/guild.ts'
import { TextChannel } from '../structures/textChannel.ts'
import { Channel, GuildChannel } from '../structures/channel.ts'
2021-04-04 11:27:02 +00:00
import { StoreChannel } from '../structures/guildStoreChannel.ts'
import { StageVoiceChannel } from '../structures/guildStageVoiceChannel.ts'
2020-12-02 12:29:52 +00:00
export type EveryTextChannelTypes =
| TextChannel
2020-12-02 12:29:52 +00:00
| NewsChannel
| GuildTextChannel
| GuildTextBasedChannel
| DMChannel
| GroupDMChannel
2020-12-02 12:29:52 +00:00
export type EveryTextChannelPayloadTypes =
| TextChannelPayload
| GuildNewsChannelPayload
| GuildTextChannelPayload
| GuildTextBasedChannelPayload
2020-12-02 12:29:52 +00:00
| DMChannelPayload
| GroupDMChannelPayload
export type EveryChannelTypes =
| Channel
| GuildChannel
2020-12-02 12:29:52 +00:00
| CategoryChannel
| VoiceChannel
| EveryTextChannelTypes
export type EveryChannelPayloadTypes =
| ChannelPayload
2020-12-20 09:11:37 +00:00
| GuildCategoryChannelPayload
2020-12-02 12:29:52 +00:00
| GuildVoiceChannelPayload
| EveryTextChannelPayloadTypes
2020-12-03 04:06:41 +00:00
/** Get appropriate Channel structure by its type */
2020-12-02 12:29:52 +00:00
const getChannelByType = (
client: Client,
data: EveryChannelPayloadTypes,
guild?: Guild
): EveryChannelTypes | undefined => {
switch (data.type) {
case ChannelTypes.GUILD_CATEGORY:
2020-12-02 12:29:52 +00:00
if (guild === undefined)
throw new Error('No Guild was provided to construct Channel')
return new CategoryChannel(
client,
2020-12-20 09:11:37 +00:00
data as GuildCategoryChannelPayload,
2020-12-02 12:29:52 +00:00
guild
)
case ChannelTypes.GUILD_NEWS:
2020-12-02 12:29:52 +00:00
if (guild === undefined)
throw new Error('No Guild was provided to construct Channel')
return new NewsChannel(client, data as GuildNewsChannelPayload, guild)
case ChannelTypes.GUILD_TEXT:
2020-12-02 12:29:52 +00:00
if (guild === undefined)
throw new Error('No Guild was provided to construct Channel')
return new GuildTextChannel(
client,
data as GuildTextChannelPayload,
guild
)
2021-04-04 11:27:02 +00:00
case ChannelTypes.GUILD_STORE:
if (guild === undefined)
throw new Error('No Guild was provided to construct Channel')
return new StoreChannel(client, data as GuildTextChannelPayload, guild)
case ChannelTypes.GUILD_VOICE:
2020-12-02 12:29:52 +00:00
if (guild === undefined)
throw new Error('No Guild was provided to construct Channel')
return new VoiceChannel(client, data as GuildVoiceChannelPayload, guild)
2021-04-04 11:27:02 +00:00
case ChannelTypes.GUILD_STAGE_VOICE:
if (guild === undefined)
throw new Error('No Guild was provided to construct Channel')
return new StageVoiceChannel(
client,
data as GuildVoiceChannelPayload,
guild
)
case ChannelTypes.DM:
return new DMChannel(client, data as DMChannelPayload)
case ChannelTypes.GROUP_DM:
return new GroupDMChannel(client, data as GroupDMChannelPayload)
}
}
export default getChannelByType
2021-04-04 11:27:02 +00:00
export function isTextChannel(channel: Channel): channel is TextChannel {
return (
channel.type === ChannelTypes.DM ||
channel.type === ChannelTypes.GROUP_DM ||
channel.type === ChannelTypes.GUILD_TEXT ||
channel.type === ChannelTypes.GUILD_NEWS
)
}
export function isDMChannel(channel: Channel): channel is DMChannel {
return channel.type === ChannelTypes.DM
}
export function isGroupDMChannel(channel: Channel): channel is GroupDMChannel {
return channel.type === ChannelTypes.GROUP_DM
}
export function isGuildTextChannel(
channel: Channel
): channel is GuildTextChannel {
return channel.type === ChannelTypes.GUILD_TEXT
}
export function isGuildBasedTextChannel(
channel: Channel
): channel is GuildTextBasedChannel {
return (
channel.type === ChannelTypes.GUILD_TEXT ||
channel.type === ChannelTypes.GUILD_NEWS
)
}
export function isCategoryChannel(
channel: Channel
): channel is CategoryChannel {
return channel.type === ChannelTypes.GUILD_CATEGORY
}
export function isNewsChannel(channel: Channel): channel is NewsChannel {
return channel.type === ChannelTypes.GUILD_NEWS
}
export function isVoiceChannel(channel: Channel): channel is VoiceChannel {
return channel.type === ChannelTypes.GUILD_VOICE
}
export function isStageVoiceChannel(
channel: Channel
): channel is StageVoiceChannel {
return channel.type === ChannelTypes.GUILD_STAGE_VOICE
}
export function isStoreChannel(channel: Channel): channel is StoreChannel {
return channel.type === ChannelTypes.GUILD_STORE
}
export function isGuildChannel(channel: Channel): channel is GuildChannel {
return (
channel.type === ChannelTypes.GUILD_CATEGORY ||
channel.type === ChannelTypes.GUILD_NEWS ||
channel.type === ChannelTypes.GUILD_STORE ||
channel.type === ChannelTypes.GUILD_TEXT ||
channel.type === ChannelTypes.GUILD_VOICE ||
channel.type === ChannelTypes.GUILD_STAGE_VOICE
)
}