mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 11:28:15 +00:00
Merge branch 'master' of https://github.com/Skillz4Killz/Discordeno into next
This commit is contained in:
+130
-37
@@ -1,4 +1,3 @@
|
||||
import { endpoints } from "../constants/discord.ts";
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
import { structures } from "../structures/mod.ts";
|
||||
@@ -17,10 +16,8 @@ import { Errors } from "../types/errors.ts";
|
||||
import { PermissionOverwrite } from "../types/guild.ts";
|
||||
import { MessageCreateOptions } from "../types/message.ts";
|
||||
import { Permissions } from "../types/permission.ts";
|
||||
import {
|
||||
botHasChannelPermissions,
|
||||
calculateBits,
|
||||
} from "../utils/permissions.ts";
|
||||
import { endpoints } from "../utils/constants.ts";
|
||||
import { botHasChannelPermissions } from "../utils/permissions.ts";
|
||||
|
||||
/** Checks if a channel overwrite for a user id or a role id has permission in this channel */
|
||||
export function channelOverwriteHasPermission(
|
||||
@@ -48,16 +45,22 @@ export async function getMessage(
|
||||
channelID: string,
|
||||
id: string,
|
||||
) {
|
||||
const hasViewChannelPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.VIEW_CHANNEL],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.VIEW_CHANNEL])
|
||||
!hasViewChannelPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_VIEW_CHANNEL);
|
||||
}
|
||||
|
||||
const hasReadMessageHistoryPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
)
|
||||
!hasReadMessageHistoryPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
@@ -77,16 +80,22 @@ export async function getMessages(
|
||||
| GetMessagesAround
|
||||
| GetMessages,
|
||||
) {
|
||||
const hasViewChannelPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.VIEW_CHANNEL],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.VIEW_CHANNEL])
|
||||
!hasViewChannelPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_VIEW_CHANNEL);
|
||||
}
|
||||
|
||||
const hasReadMessageHistoryPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
)
|
||||
!hasReadMessageHistoryPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
@@ -114,24 +123,34 @@ export async function sendMessage(
|
||||
content: string | MessageContent,
|
||||
) {
|
||||
if (typeof content === "string") content = { content };
|
||||
const hasSendMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.SEND_MESSAGES],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.SEND_MESSAGES])
|
||||
!hasSendMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_MESSAGES);
|
||||
}
|
||||
|
||||
const hasSendTtsMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.SEND_TTS_MESSAGES],
|
||||
);
|
||||
if (
|
||||
content.tts &&
|
||||
!botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.SEND_TTS_MESSAGES],
|
||||
)
|
||||
!hasSendTtsMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_TTS_MESSAGE);
|
||||
}
|
||||
|
||||
const hasEmbedLinksPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.EMBED_LINKS],
|
||||
);
|
||||
if (
|
||||
content.embed &&
|
||||
!botHasChannelPermissions(channelID, [Permissions.EMBED_LINKS])
|
||||
!hasEmbedLinksPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_EMBED_LINKS);
|
||||
}
|
||||
@@ -165,6 +184,17 @@ export async function sendMessage(
|
||||
content.mentions.roles = content.mentions.roles.slice(0, 100);
|
||||
}
|
||||
}
|
||||
|
||||
if (content.mentions.repliedUser) {
|
||||
if (
|
||||
!(await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.READ_MESSAGE_HISTORY],
|
||||
))
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_MESSAGES);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const channel = await cacheHandlers.get("channels", channelID);
|
||||
@@ -180,7 +210,15 @@ export async function sendMessage(
|
||||
endpoints.CHANNEL_MESSAGES(channelID),
|
||||
{
|
||||
...content,
|
||||
allowed_mentions: content.mentions,
|
||||
allowed_mentions: content.mentions
|
||||
? {
|
||||
...content.mentions,
|
||||
replied_user: content.mentions.repliedUser !== false,
|
||||
}
|
||||
: undefined,
|
||||
message_reference: {
|
||||
message_id: content.replyMessageID,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
@@ -188,13 +226,17 @@ export async function sendMessage(
|
||||
}
|
||||
|
||||
/** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */
|
||||
export function deleteMessages(
|
||||
export async function deleteMessages(
|
||||
channelID: string,
|
||||
ids: string[],
|
||||
reason?: string,
|
||||
) {
|
||||
const hasManageMessages = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.MANAGE_MESSAGES],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.MANAGE_MESSAGES])
|
||||
!hasManageMessages
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
@@ -215,9 +257,13 @@ export function deleteMessages(
|
||||
}
|
||||
|
||||
/** Gets the invites for this channel. Requires MANAGE_CHANNEL */
|
||||
export function getChannelInvites(channelID: string) {
|
||||
export async function getChannelInvites(channelID: string) {
|
||||
const hasManagaChannels = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.MANAGE_CHANNELS],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.MANAGE_CHANNELS])
|
||||
!hasManagaChannels
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
@@ -225,12 +271,16 @@ export function getChannelInvites(channelID: string) {
|
||||
}
|
||||
|
||||
/** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */
|
||||
export function createInvite(channelID: string, options: CreateInviteOptions) {
|
||||
export async function createInvite(
|
||||
channelID: string,
|
||||
options: CreateInviteOptions,
|
||||
) {
|
||||
const hasCreateInstantInvitePerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.CREATE_INSTANT_INVITE],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.CREATE_INSTANT_INVITE],
|
||||
)
|
||||
!hasCreateInstantInvitePerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_CREATE_INSTANT_INVITE);
|
||||
}
|
||||
@@ -238,9 +288,13 @@ export function createInvite(channelID: string, options: CreateInviteOptions) {
|
||||
}
|
||||
|
||||
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
|
||||
export function getChannelWebhooks(channelID: string) {
|
||||
export async function getChannelWebhooks(channelID: string) {
|
||||
const hasManageWebhooksPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.MANAGE_WEBHOOKS],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.MANAGE_WEBHOOKS])
|
||||
!hasManageWebhooksPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS);
|
||||
}
|
||||
@@ -293,12 +347,17 @@ function processEditChannelQueue() {
|
||||
}
|
||||
}
|
||||
|
||||
export function editChannel(
|
||||
export async function editChannel(
|
||||
channelID: string,
|
||||
options: ChannelEditOptions,
|
||||
reason?: string,
|
||||
) {
|
||||
const hasManageChannelsPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
[Permissions.MANAGE_CHANNELS],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(channelID, [Permissions.MANAGE_CHANNELS])
|
||||
!hasManageChannelsPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
@@ -352,7 +411,10 @@ export function editChannel(
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_CHANNEL(channelID),
|
||||
payload,
|
||||
{
|
||||
...payload,
|
||||
reason,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -361,8 +423,12 @@ export async function followChannel(
|
||||
sourceChannelID: string,
|
||||
targetChannelID: string,
|
||||
) {
|
||||
const hasManageWebhooksPerm = await botHasChannelPermissions(
|
||||
targetChannelID,
|
||||
[Permissions.MANAGE_WEBHOOKS],
|
||||
);
|
||||
if (
|
||||
!botHasChannelPermissions(targetChannelID, [Permissions.MANAGE_WEBHOOKS])
|
||||
!hasManageWebhooksPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
@@ -376,3 +442,30 @@ export async function followChannel(
|
||||
|
||||
return data.webhook_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a channel is synchronized with its parent/category channel or not.
|
||||
* @param channelID The ID of the channel to test for synchronization
|
||||
* @return Returns `true` if the channel is synchronized, otherwise `false`. Returns `false` if the channel is not cached.
|
||||
*/
|
||||
export async function isChannelSynced(channelID: string) {
|
||||
const channel = await cacheHandlers.get("channels", channelID);
|
||||
if (!channel?.parentID) return false;
|
||||
|
||||
const parentChannel = await cacheHandlers.get("channels", channel.parentID);
|
||||
if (!parentChannel) return false;
|
||||
|
||||
return channel.permission_overwrites?.every((overwrite) => {
|
||||
const permission = parentChannel.permission_overwrites?.find((ow) =>
|
||||
ow.id === overwrite.id
|
||||
);
|
||||
if (!permission) return false;
|
||||
if (
|
||||
overwrite.allow !== permission.allow || overwrite.deny !== permission.deny
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user