harmony/src/utils/getChannelByType.ts

95 lines
2.8 KiB
TypeScript
Raw Normal View History

import { Client } from '../models/client.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'
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
)
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)
case ChannelTypes.DM:
return new DMChannel(client, data as DMChannelPayload)
case ChannelTypes.GROUP_DM:
return new GroupDMChannel(client, data as GroupDMChannelPayload)
}
}
export default getChannelByType