feat(structures/channel): add getters and handlers to Channel struct (#533)

* export CleanVoiceState interface

* feat(structures): channels getter voiceStates

* feat(structures): channels add connectedMembers

* feat(structures): channel add disconnect

* Update channel.ts

* feat(structures): channels add delete

* idk

* feat(structures): channel add editOverwrite

* feat(structures): channel add deleteOverewrite

* feat(structures): channel add edit

* feat(structures): channel add hasPermission
This commit is contained in:
ITOH
2021-02-21 18:22:52 +01:00
committed by GitHub
parent f481962b9c
commit 15dc027b92
2 changed files with 88 additions and 3 deletions
+87 -2
View File
@@ -1,14 +1,28 @@
import { import {
ChannelCreatePayload, ChannelCreatePayload,
ChannelEditOptions,
ChannelType, ChannelType,
MessageContent, MessageContent,
Overwrite,
Permission,
RawOverwrite, RawOverwrite,
} from "../../types/mod.ts"; } from "../../types/mod.ts";
import { cache } from "../../util/cache.ts"; import { cache } from "../../util/cache.ts";
import { Collection } from "../../util/collection.ts"; import { Collection } from "../../util/collection.ts";
import { createNewProp } from "../../util/utils.ts"; import { createNewProp } from "../../util/utils.ts";
import { sendMessage } from "../handlers/channel.ts"; import {
import { Guild } from "./guild.ts"; channelOverwriteHasPermission,
editChannel,
sendMessage,
} from "../handlers/channel.ts";
import {
deleteChannel,
deleteChannelOverwrite,
editChannelOverwrite,
} from "../handlers/guild.ts";
import { kickFromVoiceChannel } from "../handlers/member.ts";
import { CleanVoiceState, Guild } from "./guild.ts";
import { Member } from "./member.ts";
import { Message } from "./message.ts"; import { Message } from "./message.ts";
const baseChannel: Partial<Channel> = { const baseChannel: Partial<Channel> = {
@@ -21,9 +35,45 @@ const baseChannel: Partial<Channel> = {
get mention() { get mention() {
return `<#${this.id!}>`; return `<#${this.id!}>`;
}, },
get voiceStates() {
return this.guild?.voiceStates.filter((voiceState) =>
voiceState.channelID === this.id
);
},
get connectedMembers() {
const voiceStates = this.voiceStates;
if (!voiceStates) return undefined;
return new Collection(
voiceStates.map((vs, key) => [key, cache.members.get(key)]),
);
},
send(content) { send(content) {
return sendMessage(this.id!, content); return sendMessage(this.id!, content);
}, },
disconnect(memberID) {
return kickFromVoiceChannel(this.guildID!, memberID);
},
delete() {
return deleteChannel(this.guildID!, this.id!);
},
editOverwrite(id, options) {
return editChannelOverwrite(this.guildID!, this.id!, id, options);
},
deleteOverwrite(id) {
return deleteChannelOverwrite(this.guildID!, this.id!, id);
},
hasPermission(overwrites, permissions) {
return channelOverwriteHasPermission(
this.guildID!,
this.id!,
overwrites,
permissions,
);
},
edit(options, reason) {
return editChannel(this.id!, options, reason);
},
}; };
// deno-lint-ignore require-await // deno-lint-ignore require-await
@@ -112,9 +162,44 @@ export interface Channel {
messages: Collection<string, Message>; messages: Collection<string, Message>;
/** The mention of the channel */ /** The mention of the channel */
mention: string; mention: string;
/**
* Gets the voice states for this channel
*
* ⚠️ ADVANCED: If you use the custom cache, these will not work for you. Getters can not be async and custom cache requires async.
*/
voiceStates?: Collection<string, CleanVoiceState>;
/**
* Gets the connected members for this channel undefined if member is not cached
*
* ⚠️ ADVANCED: If you use the custom cache, these will not work for you. Getters can not be async and custom cache requires async.
*/
connectedMembers?: Collection<string, Member | undefined>;
// METHODS // METHODS
/** Send a message to the channel. Requires SEND_MESSAGES permission. */ /** Send a message to the channel. Requires SEND_MESSAGES permission. */
send(content: string | MessageContent): ReturnType<typeof sendMessage>; send(content: string | MessageContent): ReturnType<typeof sendMessage>;
/** Disconnect a member from a voice channel. Requires MOVE_MEMBERS permission. */
disconnect(memberID: string): ReturnType<typeof kickFromVoiceChannel>;
/** Delete the channel */
delete(): ReturnType<typeof deleteChannel>;
/** Edit a channel Overwrite */
editOverwrite(
overwriteID: string,
options: Omit<Overwrite, "id">,
): ReturnType<typeof editChannelOverwrite>;
/** Delete a channel Overwrite */
deleteOverwrite(
overwriteID: string,
): ReturnType<typeof deleteChannelOverwrite>;
/** Checks if a channel overwrite for a user id or a role id has permission in this channel */
hasPermission(
overwrites: RawOverwrite[],
permissions: Permission[],
): ReturnType<typeof channelOverwriteHasPermission>;
/** Edit the channel */
edit(
options: ChannelEditOptions,
reason?: string,
): ReturnType<typeof editChannel>;
} }
+1 -1
View File
@@ -345,7 +345,7 @@ export interface Guild {
invites(): ReturnType<typeof getInvites>; invites(): ReturnType<typeof getInvites>;
} }
interface CleanVoiceState extends VoiceState { export interface CleanVoiceState extends VoiceState {
/** The guild id where this voice state is from */ /** The guild id where this voice state is from */
guildID: string; guildID: string;
/** The channel id where this voice state is from */ /** The channel id where this voice state is from */