mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-04 09:50:07 +00:00
refactor channel functions
This commit is contained in:
@@ -1,9 +1,5 @@
|
||||
import { Permissions } from "../types/permission.ts";
|
||||
import { Channel } from "../structures/channel.ts";
|
||||
import {
|
||||
botHasPermission,
|
||||
botHasChannelPermissions,
|
||||
} from "../utils/permissions.ts";
|
||||
import { botHasChannelPermissions } from "../utils/permissions.ts";
|
||||
import { Errors } from "../types/errors.ts";
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
import { endpoints } from "../constants/discord.ts";
|
||||
@@ -20,81 +16,79 @@ import {
|
||||
} from "../types/channel.ts";
|
||||
import { logYellow } from "../utils/logger.ts";
|
||||
import { structures } from "../structures/mod.ts";
|
||||
import { RawOverwrite } from "../types/guild.ts";
|
||||
|
||||
/** Checks if a user id or a role id has permission in this channel */
|
||||
export function hasChannelPermission(
|
||||
channel: Channel,
|
||||
/** Checks if a channel overwrite for a user id or a role id has permission in this channel */
|
||||
export function channelOverwriteHasPermission(
|
||||
guildID: string,
|
||||
id: string,
|
||||
overwrites: RawOverwrite[],
|
||||
permissions: Permissions[],
|
||||
) {
|
||||
const overwrite =
|
||||
channel.permission_overwrites?.find((perm) => perm.id === id) ||
|
||||
channel.permission_overwrites?.find((perm) => perm.id === channel.guildID);
|
||||
const overwrite = overwrites.find((perm) => perm.id === id) ||
|
||||
overwrites.find((perm) => perm.id === guildID);
|
||||
|
||||
return permissions.every((perm) => {
|
||||
if (overwrite) {
|
||||
if (BigInt(overwrite.deny) & BigInt(perm)) return false;
|
||||
if (BigInt(overwrite.allow) & BigInt(perm)) return true;
|
||||
}
|
||||
if (channel.guildID) {
|
||||
return botHasPermission(channel.guildID, [perm]);
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
|
||||
export async function getMessage(channel: Channel, id: string) {
|
||||
if (channel.guildID) {
|
||||
if (
|
||||
!botHasChannelPermissions(channel.id, [Permissions.VIEW_CHANNEL])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_VIEW_CHANNEL);
|
||||
}
|
||||
if (
|
||||
!botHasChannelPermissions(
|
||||
channel.id,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
export async function getMessage(
|
||||
channelID: string,
|
||||
id: string,
|
||||
) {
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.VIEW_CHANNEL])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_VIEW_CHANNEL);
|
||||
}
|
||||
if (
|
||||
!botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
|
||||
const result = await RequestManager.get(
|
||||
endpoints.CHANNEL_MESSAGE(channel.id, id),
|
||||
endpoints.CHANNEL_MESSAGE(channelID, id),
|
||||
) as MessageCreateOptions;
|
||||
return structures.createMessage(result);
|
||||
}
|
||||
|
||||
/** Fetches between 2-100 messages. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
|
||||
export async function getMessages(
|
||||
channel: Channel,
|
||||
channelID: string,
|
||||
options?:
|
||||
| GetMessagesAfter
|
||||
| GetMessagesBefore
|
||||
| GetMessagesAround
|
||||
| GetMessages,
|
||||
) {
|
||||
if (channel.guildID) {
|
||||
if (
|
||||
!botHasChannelPermissions(channel.id, [Permissions.VIEW_CHANNEL])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_VIEW_CHANNEL);
|
||||
}
|
||||
if (
|
||||
!botHasChannelPermissions(
|
||||
channel.id,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.VIEW_CHANNEL])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_VIEW_CHANNEL);
|
||||
}
|
||||
if (
|
||||
!botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
|
||||
if (options?.limit && options.limit > 100) return;
|
||||
|
||||
const result = (await RequestManager.get(
|
||||
endpoints.CHANNEL_MESSAGES(channel.id),
|
||||
endpoints.CHANNEL_MESSAGES(channelID),
|
||||
options,
|
||||
)) as MessageCreateOptions[];
|
||||
return result.map((res) => structures.createMessage(res));
|
||||
@@ -110,30 +104,28 @@ export async function getPins(channelID: string) {
|
||||
|
||||
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
|
||||
export async function sendMessage(
|
||||
channel: Channel,
|
||||
channelID: string,
|
||||
content: string | MessageContent,
|
||||
) {
|
||||
if (typeof content === "string") content = { content };
|
||||
if (channel.guildID) {
|
||||
if (
|
||||
!botHasChannelPermissions(channel.id, [Permissions.SEND_MESSAGES])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_MESSAGES);
|
||||
}
|
||||
if (
|
||||
content.tts &&
|
||||
!botHasChannelPermissions(
|
||||
channel.id,
|
||||
[Permissions.SEND_TTS_MESSAGES],
|
||||
)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_TTS_MESSAGE);
|
||||
}
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.SEND_MESSAGES])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_MESSAGES);
|
||||
}
|
||||
if (
|
||||
content.tts &&
|
||||
!botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.SEND_TTS_MESSAGES],
|
||||
)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_TTS_MESSAGE);
|
||||
}
|
||||
|
||||
if (
|
||||
content.embed &&
|
||||
!botHasChannelPermissions(channel.id, [Permissions.EMBED_LINKS])
|
||||
!botHasChannelPermissions(channelID, [Permissions.EMBED_LINKS])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_EMBED_LINKS);
|
||||
}
|
||||
@@ -170,7 +162,7 @@ export async function sendMessage(
|
||||
}
|
||||
|
||||
const result = await RequestManager.post(
|
||||
endpoints.CHANNEL_MESSAGES(channel.id),
|
||||
endpoints.CHANNEL_MESSAGES(channelID),
|
||||
{
|
||||
...content,
|
||||
allowed_mentions: content.mentions,
|
||||
@@ -182,13 +174,12 @@ export async function sendMessage(
|
||||
|
||||
/** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */
|
||||
export function deleteMessages(
|
||||
channel: Channel,
|
||||
channelID: string,
|
||||
ids: string[],
|
||||
reason?: string,
|
||||
) {
|
||||
if (
|
||||
channel.guildID &&
|
||||
!botHasChannelPermissions(channel.id, [Permissions.MANAGE_MESSAGES])
|
||||
!botHasChannelPermissions(channelID, [Permissions.MANAGE_MESSAGES])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
@@ -202,45 +193,43 @@ export function deleteMessages(
|
||||
);
|
||||
}
|
||||
|
||||
return RequestManager.post(endpoints.CHANNEL_BULK_DELETE(channel.id), {
|
||||
return RequestManager.post(endpoints.CHANNEL_BULK_DELETE(channelID), {
|
||||
messages: ids.splice(0, 100),
|
||||
reason,
|
||||
});
|
||||
}
|
||||
|
||||
/** Gets the invites for this channel. Requires MANAGE_CHANNEL */
|
||||
export function getChannelInvites(channel: Channel) {
|
||||
export function getChannelInvites(channelID: string) {
|
||||
if (
|
||||
channel.guildID &&
|
||||
!botHasChannelPermissions(channel.id, [Permissions.MANAGE_CHANNELS])
|
||||
!botHasChannelPermissions(channelID, [Permissions.MANAGE_CHANNELS])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
return RequestManager.get(endpoints.CHANNEL_INVITES(channel.id));
|
||||
return RequestManager.get(endpoints.CHANNEL_INVITES(channelID));
|
||||
}
|
||||
|
||||
/** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */
|
||||
export function createInvite(channel: Channel, options: CreateInviteOptions) {
|
||||
export function createInvite(channelID: string, options: CreateInviteOptions) {
|
||||
if (
|
||||
channel.guildID &&
|
||||
!botHasChannelPermissions(
|
||||
channel.id,
|
||||
channelID,
|
||||
[Permissions.CREATE_INSTANT_INVITE],
|
||||
)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_CREATE_INSTANT_INVITE);
|
||||
}
|
||||
return RequestManager.post(endpoints.CHANNEL_INVITES(channel.id), options);
|
||||
return RequestManager.post(endpoints.CHANNEL_INVITES(channelID), options);
|
||||
}
|
||||
|
||||
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
|
||||
export function getChannelWebhooks(channel: Channel) {
|
||||
export function getChannelWebhooks(channelID: string) {
|
||||
if (
|
||||
!botHasChannelPermissions(channel.id, [Permissions.MANAGE_WEBHOOKS])
|
||||
!botHasChannelPermissions(channelID, [Permissions.MANAGE_WEBHOOKS])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS);
|
||||
}
|
||||
return RequestManager.get(endpoints.CHANNEL_WEBHOOKS(channel.id));
|
||||
return RequestManager.get(endpoints.CHANNEL_WEBHOOKS(channelID));
|
||||
}
|
||||
|
||||
interface EditChannelRequest {
|
||||
@@ -248,7 +237,7 @@ interface EditChannelRequest {
|
||||
timestamp: number;
|
||||
channelID: string;
|
||||
items: {
|
||||
channel: Channel;
|
||||
channelID: string;
|
||||
options: ChannelEditOptions;
|
||||
}[];
|
||||
}
|
||||
@@ -272,11 +261,14 @@ function processEditChannelQueue() {
|
||||
|
||||
if (!details) return;
|
||||
|
||||
editChannel(details.channel, details.options);
|
||||
editChannel(details.channelID, details.options);
|
||||
const secondDetails = request.items.shift();
|
||||
if (!secondDetails) return;
|
||||
|
||||
return editChannel(secondDetails.channel, secondDetails.options);
|
||||
return editChannel(
|
||||
secondDetails.channelID,
|
||||
secondDetails.options,
|
||||
);
|
||||
});
|
||||
|
||||
if (editChannelNameTopicQueue.size) {
|
||||
@@ -286,21 +278,22 @@ function processEditChannelQueue() {
|
||||
}
|
||||
}
|
||||
|
||||
export function editChannel(channel: Channel, options: ChannelEditOptions) {
|
||||
if (!channel.guildID) throw new Error(Errors.CHANNEL_NOT_IN_GUILD);
|
||||
|
||||
export function editChannel(
|
||||
channelID: string,
|
||||
options: ChannelEditOptions,
|
||||
) {
|
||||
if (
|
||||
!botHasChannelPermissions(channel.id, [Permissions.MANAGE_CHANNELS])
|
||||
!botHasChannelPermissions(channelID, [Permissions.MANAGE_CHANNELS])
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
|
||||
if (options.name || options.topic) {
|
||||
const request = editChannelNameTopicQueue.get(channel.id);
|
||||
const request = editChannelNameTopicQueue.get(channelID);
|
||||
if (!request) {
|
||||
// If this hasnt been done before simply add 1 for it
|
||||
editChannelNameTopicQueue.set(channel.id, {
|
||||
channelID: channel.id,
|
||||
editChannelNameTopicQueue.set(channelID, {
|
||||
channelID: channelID,
|
||||
amount: 1,
|
||||
// 10 minutes from now
|
||||
timestamp: Date.now() + 600000,
|
||||
@@ -312,7 +305,7 @@ export function editChannel(channel: Channel, options: ChannelEditOptions) {
|
||||
request.timestamp = Date.now() + 600000;
|
||||
} else {
|
||||
// 2 have already been used add to queue
|
||||
request.items.push({ channel, options });
|
||||
request.items.push({ channelID, options });
|
||||
if (editChannelProcessing) return;
|
||||
editChannelProcessing = true;
|
||||
processEditChannelQueue();
|
||||
@@ -322,7 +315,10 @@ export function editChannel(channel: Channel, options: ChannelEditOptions) {
|
||||
|
||||
const payload = {
|
||||
...options,
|
||||
permission_overwrites: options.permission_overwrites?.map(
|
||||
rate_limit_per_user: options.slowmode,
|
||||
parent_id: options.parentID,
|
||||
user_limit: options.userLimit,
|
||||
permission_overwrites: options.overwrites?.map(
|
||||
(overwrite) => {
|
||||
return {
|
||||
...overwrite,
|
||||
@@ -340,7 +336,7 @@ export function editChannel(channel: Channel, options: ChannelEditOptions) {
|
||||
};
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_CHANNEL(channel.id),
|
||||
endpoints.GUILD_CHANNEL(channelID),
|
||||
payload,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -11,15 +11,15 @@ export interface ChannelEditOptions {
|
||||
/** whether the channel is nsfw Text */
|
||||
nsfw?: boolean;
|
||||
/** amount of seconds a user has to wait before sending another message (0-21600); bots, as well as users with the permission manage_messages or manage_channel, are unaffected Text */
|
||||
rate_limit_per_user?: number;
|
||||
slowmode?: number;
|
||||
/** the bitrate (in bits) of the voice channel; 8000 to 96000 (128000 for VIP servers) Voice */
|
||||
bitrate?: number;
|
||||
/** the user limit of the voice channel; 0 refers to no limit, 1 to 99 refers to a user limit Voice */
|
||||
user_limit?: number;
|
||||
userLimit?: number;
|
||||
/** channel or category-specific permissions All */
|
||||
permission_overwrites?: Overwrite[];
|
||||
overwrites?: Overwrite[];
|
||||
/** id of the new parent category for a channel Text, Voice */
|
||||
parent_id?: string;
|
||||
parentID?: string;
|
||||
}
|
||||
|
||||
export interface BaseChannelCreate {
|
||||
|
||||
Reference in New Issue
Block a user