mirror of
https://github.com/discordeno/discordeno.git
synced 2026-06-16 03:18:17 +00:00
refactor!: organize directories and files (#268)
* refactor!: organize directory structure * fix: avoid stack overflow err * chore: swtch back to std/encoding for base64 * style: format source files
This commit is contained in:
@@ -1,472 +0,0 @@
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
import { structures } from "../structures/structures.ts";
|
||||
import {
|
||||
ChannelEditOptions,
|
||||
ChannelTypes,
|
||||
CreateInviteOptions,
|
||||
Errors,
|
||||
FollowedChannelPayload,
|
||||
GetMessages,
|
||||
GetMessagesAfter,
|
||||
GetMessagesAround,
|
||||
GetMessagesBefore,
|
||||
MessageContent,
|
||||
MessageCreateOptions,
|
||||
Permission,
|
||||
Permissions,
|
||||
RawOverwrite,
|
||||
WebhookPayload,
|
||||
} from "../types/types.ts";
|
||||
import { endpoints } from "../utils/constants.ts";
|
||||
import {
|
||||
botHasChannelPermissions,
|
||||
calculateBits,
|
||||
} 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(
|
||||
guildID: string,
|
||||
id: string,
|
||||
overwrites: RawOverwrite[],
|
||||
permissions: Permission[],
|
||||
) {
|
||||
const overwrite = overwrites.find((perm) => perm.id === id) ||
|
||||
overwrites.find((perm) => perm.id === guildID);
|
||||
|
||||
return permissions.every((perm) => {
|
||||
if (overwrite) {
|
||||
const allowBits = overwrite.allow;
|
||||
const denyBits = overwrite.deny;
|
||||
if (BigInt(denyBits) & BigInt(Permissions[perm])) return false;
|
||||
if (BigInt(allowBits) & BigInt(Permissions[perm])) return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
}
|
||||
|
||||
/** Fetch a single message from the server. Requires VIEW_CHANNEL and READ_MESSAGE_HISTORY */
|
||||
export async function getMessage(
|
||||
channelID: string,
|
||||
id: string,
|
||||
) {
|
||||
const hasViewChannelPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["VIEW_CHANNEL"],
|
||||
);
|
||||
if (
|
||||
!hasViewChannelPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_VIEW_CHANNEL);
|
||||
}
|
||||
|
||||
const hasReadMessageHistoryPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["READ_MESSAGE_HISTORY"],
|
||||
);
|
||||
if (
|
||||
!hasReadMessageHistoryPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
|
||||
const result = await RequestManager.get(
|
||||
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(
|
||||
channelID: string,
|
||||
options?:
|
||||
| GetMessagesAfter
|
||||
| GetMessagesBefore
|
||||
| GetMessagesAround
|
||||
| GetMessages,
|
||||
) {
|
||||
const hasViewChannelPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["VIEW_CHANNEL"],
|
||||
);
|
||||
if (
|
||||
!hasViewChannelPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_VIEW_CHANNEL);
|
||||
}
|
||||
|
||||
const hasReadMessageHistoryPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["READ_MESSAGE_HISTORY"],
|
||||
);
|
||||
if (
|
||||
!hasReadMessageHistoryPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
|
||||
if (options?.limit && options.limit > 100) return;
|
||||
|
||||
const result = (await RequestManager.get(
|
||||
endpoints.CHANNEL_MESSAGES(channelID),
|
||||
options,
|
||||
)) as MessageCreateOptions[];
|
||||
return Promise.all(result.map((res) => structures.createMessage(res)));
|
||||
}
|
||||
|
||||
/** Get pinned messages in this channel. */
|
||||
export async function getPins(channelID: string) {
|
||||
const result = (await RequestManager.get(
|
||||
endpoints.CHANNEL_PINS(channelID),
|
||||
)) as MessageCreateOptions[];
|
||||
return Promise.all(result.map((res) => structures.createMessage(res)));
|
||||
}
|
||||
|
||||
/** Send a message to the channel. Requires SEND_MESSAGES permission. */
|
||||
export async function sendMessage(
|
||||
channelID: string,
|
||||
content: string | MessageContent,
|
||||
) {
|
||||
if (typeof content === "string") content = { content };
|
||||
const hasSendMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["SEND_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
!hasSendMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_MESSAGES);
|
||||
}
|
||||
|
||||
const hasSendTtsMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["SEND_TTS_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
content.tts &&
|
||||
!hasSendTtsMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_TTS_MESSAGE);
|
||||
}
|
||||
|
||||
const hasEmbedLinksPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["EMBED_LINKS"],
|
||||
);
|
||||
if (
|
||||
content.embed &&
|
||||
!hasEmbedLinksPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_EMBED_LINKS);
|
||||
}
|
||||
|
||||
// Use ... for content length due to unicode characters and js .length handling
|
||||
if (content.content && [...content.content].length > 2000) {
|
||||
throw new Error(Errors.MESSAGE_MAX_LENGTH);
|
||||
}
|
||||
|
||||
if (content.mentions) {
|
||||
if (content.mentions.users?.length) {
|
||||
if (content.mentions.parse?.includes("users")) {
|
||||
content.mentions.parse = content.mentions.parse.filter((p) =>
|
||||
p !== "users"
|
||||
);
|
||||
}
|
||||
|
||||
if (content.mentions.users.length > 100) {
|
||||
content.mentions.users = content.mentions.users.slice(0, 100);
|
||||
}
|
||||
}
|
||||
|
||||
if (content.mentions.roles?.length) {
|
||||
if (content.mentions.parse?.includes("roles")) {
|
||||
content.mentions.parse = content.mentions.parse.filter((p) =>
|
||||
p !== "roles"
|
||||
);
|
||||
}
|
||||
|
||||
if (content.mentions.roles.length > 100) {
|
||||
content.mentions.roles = content.mentions.roles.slice(0, 100);
|
||||
}
|
||||
}
|
||||
|
||||
if (content.mentions.repliedUser) {
|
||||
if (
|
||||
!(await botHasChannelPermissions(
|
||||
channelID,
|
||||
["READ_MESSAGE_HISTORY"],
|
||||
))
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const channel = await cacheHandlers.get("channels", channelID);
|
||||
if (!channel) throw new Error(Errors.CHANNEL_NOT_FOUND);
|
||||
if (
|
||||
![ChannelTypes.DM, ChannelTypes.GUILD_NEWS, ChannelTypes.GUILD_TEXT]
|
||||
.includes(channel.type)
|
||||
) {
|
||||
throw new Error(Errors.CHANNEL_NOT_TEXT_BASED);
|
||||
}
|
||||
|
||||
const result = await RequestManager.post(
|
||||
endpoints.CHANNEL_MESSAGES(channelID),
|
||||
{
|
||||
...content,
|
||||
allowed_mentions: content.mentions
|
||||
? {
|
||||
...content.mentions,
|
||||
replied_user: content.mentions.repliedUser,
|
||||
}
|
||||
: undefined,
|
||||
message_reference: {
|
||||
message_id: content.replyMessageID,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return structures.createMessage(result as MessageCreateOptions);
|
||||
}
|
||||
|
||||
/** Delete messages from the channel. 2-100. Requires the MANAGE_MESSAGES permission */
|
||||
export async function deleteMessages(
|
||||
channelID: string,
|
||||
ids: string[],
|
||||
reason?: string,
|
||||
) {
|
||||
const hasManageMessages = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
!hasManageMessages
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
if (ids.length < 2) {
|
||||
throw new Error(Errors.DELETE_MESSAGES_MIN);
|
||||
}
|
||||
|
||||
if (ids.length > 100) {
|
||||
console.warn(
|
||||
`This endpoint only accepts a maximum of 100 messages. Deleting the first 100 message ids provided.`,
|
||||
);
|
||||
}
|
||||
|
||||
return RequestManager.post(endpoints.CHANNEL_BULK_DELETE(channelID), {
|
||||
messages: ids.splice(0, 100),
|
||||
reason,
|
||||
});
|
||||
}
|
||||
|
||||
/** Gets the invites for this channel. Requires MANAGE_CHANNEL */
|
||||
export async function getChannelInvites(channelID: string) {
|
||||
const hasManagaChannels = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_CHANNELS"],
|
||||
);
|
||||
if (
|
||||
!hasManagaChannels
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
return RequestManager.get(endpoints.CHANNEL_INVITES(channelID));
|
||||
}
|
||||
|
||||
/** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */
|
||||
export async function createInvite(
|
||||
channelID: string,
|
||||
options: CreateInviteOptions,
|
||||
) {
|
||||
const hasCreateInstantInvitePerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["CREATE_INSTANT_INVITE"],
|
||||
);
|
||||
if (
|
||||
!hasCreateInstantInvitePerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_CREATE_INSTANT_INVITE);
|
||||
}
|
||||
return RequestManager.post(endpoints.CHANNEL_INVITES(channelID), options);
|
||||
}
|
||||
|
||||
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
|
||||
export async function getChannelWebhooks(channelID: string) {
|
||||
const hasManageWebhooksPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_WEBHOOKS"],
|
||||
);
|
||||
if (
|
||||
!hasManageWebhooksPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS);
|
||||
}
|
||||
return RequestManager.get(endpoints.CHANNEL_WEBHOOKS(channelID)) as Promise<
|
||||
WebhookPayload[]
|
||||
>;
|
||||
}
|
||||
|
||||
interface EditChannelRequest {
|
||||
amount: number;
|
||||
timestamp: number;
|
||||
channelID: string;
|
||||
items: {
|
||||
channelID: string;
|
||||
options: ChannelEditOptions;
|
||||
}[];
|
||||
}
|
||||
|
||||
const editChannelNameTopicQueue = new Map<string, EditChannelRequest>();
|
||||
let editChannelProcessing = false;
|
||||
|
||||
function processEditChannelQueue() {
|
||||
if (!editChannelProcessing) return;
|
||||
|
||||
const now = Date.now();
|
||||
editChannelNameTopicQueue.forEach((request) => {
|
||||
if (now > request.timestamp) return;
|
||||
// 10 minutes have passed so we can reset this channel again
|
||||
if (!request.items.length) {
|
||||
return editChannelNameTopicQueue.delete(request.channelID);
|
||||
}
|
||||
request.amount = 0;
|
||||
// There are items to process for this request
|
||||
const details = request.items.shift();
|
||||
|
||||
if (!details) return;
|
||||
|
||||
editChannel(details.channelID, details.options);
|
||||
const secondDetails = request.items.shift();
|
||||
if (!secondDetails) return;
|
||||
|
||||
return editChannel(
|
||||
secondDetails.channelID,
|
||||
secondDetails.options,
|
||||
);
|
||||
});
|
||||
|
||||
if (editChannelNameTopicQueue.size) {
|
||||
setTimeout(() => processEditChannelQueue(), 600000);
|
||||
} else {
|
||||
editChannelProcessing = false;
|
||||
}
|
||||
}
|
||||
|
||||
export async function editChannel(
|
||||
channelID: string,
|
||||
options: ChannelEditOptions,
|
||||
reason?: string,
|
||||
) {
|
||||
const hasManageChannelsPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_CHANNELS"],
|
||||
);
|
||||
if (
|
||||
!hasManageChannelsPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
|
||||
if (options.name || options.topic) {
|
||||
const request = editChannelNameTopicQueue.get(channelID);
|
||||
if (!request) {
|
||||
// If this hasnt been done before simply add 1 for it
|
||||
editChannelNameTopicQueue.set(channelID, {
|
||||
channelID: channelID,
|
||||
amount: 1,
|
||||
// 10 minutes from now
|
||||
timestamp: Date.now() + 600000,
|
||||
items: [],
|
||||
});
|
||||
} else if (request.amount === 1) {
|
||||
// Start queuing future requests to this channel
|
||||
request.amount = 2;
|
||||
request.timestamp = Date.now() + 600000;
|
||||
} else {
|
||||
// 2 have already been used add to queue
|
||||
request.items.push({ channelID, options });
|
||||
if (editChannelProcessing) return;
|
||||
editChannelProcessing = true;
|
||||
processEditChannelQueue();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const payload = {
|
||||
...options,
|
||||
rate_limit_per_user: options.slowmode,
|
||||
parent_id: options.parentID,
|
||||
user_limit: options.userLimit,
|
||||
permission_overwrites: options.overwrites?.map(
|
||||
(overwrite) => {
|
||||
return {
|
||||
...overwrite,
|
||||
allow: calculateBits(overwrite.allow),
|
||||
deny: calculateBits(overwrite.deny),
|
||||
};
|
||||
},
|
||||
),
|
||||
};
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_CHANNEL(channelID),
|
||||
{
|
||||
...payload,
|
||||
reason,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/** Follow a News Channel to send messages to a target channel. Requires the `MANAGE_WEBHOOKS` permission in the target channel. Returns the webhook id. */
|
||||
export async function followChannel(
|
||||
sourceChannelID: string,
|
||||
targetChannelID: string,
|
||||
) {
|
||||
const hasManageWebhooksPerm = await botHasChannelPermissions(
|
||||
targetChannelID,
|
||||
["MANAGE_WEBHOOKS"],
|
||||
);
|
||||
if (
|
||||
!hasManageWebhooksPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
|
||||
const data = await RequestManager.post(
|
||||
endpoints.CHANNEL_FOLLOW(sourceChannelID),
|
||||
{
|
||||
webhook_channel_id: targetChannelID,
|
||||
},
|
||||
) as FollowedChannelPayload;
|
||||
|
||||
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.permissionOverwrites?.every((overwrite) => {
|
||||
const permission = parentChannel.permissionOverwrites?.find((ow) =>
|
||||
ow.id === overwrite.id
|
||||
);
|
||||
if (!permission) return false;
|
||||
if (
|
||||
overwrite.allow !== permission.allow || overwrite.deny !== permission.deny
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
@@ -1,809 +0,0 @@
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
import { identifyPayload } from "../module/client.ts";
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
import { requestAllMembers } from "../module/shardingManager.ts";
|
||||
import {
|
||||
Guild,
|
||||
Member,
|
||||
structures,
|
||||
Template,
|
||||
} from "../structures/structures.ts";
|
||||
import {
|
||||
AuditLogs,
|
||||
BannedUser,
|
||||
BanOptions,
|
||||
ChannelCreateOptions,
|
||||
ChannelCreatePayload,
|
||||
ChannelTypes,
|
||||
CreateEmojisOptions,
|
||||
CreateGuildFromTemplate,
|
||||
CreateGuildPayload,
|
||||
CreateGuildTemplate,
|
||||
CreateRoleOptions,
|
||||
CreateServerOptions,
|
||||
EditEmojisOptions,
|
||||
EditGuildTemplate,
|
||||
EditIntegrationOptions,
|
||||
Errors,
|
||||
FetchMembersOptions,
|
||||
GetAuditLogsOptions,
|
||||
GuildEditOptions,
|
||||
GuildTemplate,
|
||||
ImageFormats,
|
||||
ImageSize,
|
||||
Intents,
|
||||
MemberCreatePayload,
|
||||
PositionSwap,
|
||||
PruneOptions,
|
||||
PrunePayload,
|
||||
RoleData,
|
||||
UpdateGuildPayload,
|
||||
UserPayload,
|
||||
} from "../types/types.ts";
|
||||
import { formatImageURL } from "../utils/cdn.ts";
|
||||
import { Collection } from "../utils/collection.ts";
|
||||
import { endpoints } from "../utils/constants.ts";
|
||||
import { botHasPermission, calculateBits } from "../utils/permissions.ts";
|
||||
import { urlToBase64 } from "../utils/utils.ts";
|
||||
|
||||
/** Create a new guild. Returns a guild object on success. Fires a Guild Create Gateway event. This endpoint can be used only by bots in less than 10 guilds. */
|
||||
export async function createServer(options: CreateServerOptions) {
|
||||
const guild = await RequestManager.post(
|
||||
endpoints.GUILDS,
|
||||
options,
|
||||
) as CreateGuildPayload;
|
||||
return structures.createGuild(guild, 0);
|
||||
}
|
||||
|
||||
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event.
|
||||
*/
|
||||
export function deleteServer(guildID: string) {
|
||||
return RequestManager.delete(endpoints.GUILD(guildID));
|
||||
}
|
||||
|
||||
/** Gets an array of all the channels ids that are the children of this category. */
|
||||
export function categoryChildrenIDs(guildID: string, id: string) {
|
||||
return cacheHandlers.filter(
|
||||
"channels",
|
||||
(channel) => channel.parentID === id && channel.guildID === guildID,
|
||||
);
|
||||
}
|
||||
|
||||
/** The full URL of the icon from Discords CDN. Undefined when no icon is set. */
|
||||
export function guildIconURL(
|
||||
guild: Guild,
|
||||
size: ImageSize = 128,
|
||||
format?: ImageFormats,
|
||||
) {
|
||||
return guild.icon
|
||||
? formatImageURL(endpoints.GUILD_ICON(guild.id, guild.icon), size, format)
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/** The full URL of the splash from Discords CDN. Undefined if no splash is set. */
|
||||
export function guildSplashURL(
|
||||
guild: Guild,
|
||||
size: ImageSize = 128,
|
||||
format?: ImageFormats,
|
||||
) {
|
||||
return guild.splash
|
||||
? formatImageURL(
|
||||
endpoints.GUILD_SPLASH(guild.id, guild.splash),
|
||||
size,
|
||||
format,
|
||||
)
|
||||
: undefined;
|
||||
}
|
||||
/** The full URL of the banner from Discords CDN. Undefined if no banner is set. */
|
||||
export function guildBannerURL(
|
||||
guild: Guild,
|
||||
size: ImageSize = 128,
|
||||
format?: ImageFormats,
|
||||
) {
|
||||
return guild.banner
|
||||
? formatImageURL(
|
||||
endpoints.GUILD_BANNER(guild.id, guild.banner),
|
||||
size,
|
||||
format,
|
||||
)
|
||||
: undefined;
|
||||
}
|
||||
|
||||
/** Create a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
|
||||
export async function createGuildChannel(
|
||||
guild: Guild,
|
||||
name: string,
|
||||
options?: ChannelCreateOptions,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(
|
||||
guild.id,
|
||||
["MANAGE_CHANNELS"],
|
||||
);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
|
||||
const result =
|
||||
(await RequestManager.post(endpoints.GUILD_CHANNELS(guild.id), {
|
||||
...options,
|
||||
name,
|
||||
permission_overwrites: options?.permissionOverwrites?.map((perm) => ({
|
||||
...perm,
|
||||
|
||||
allow: calculateBits(perm.allow),
|
||||
deny: calculateBits(perm.deny),
|
||||
})),
|
||||
type: options?.type || ChannelTypes.GUILD_TEXT,
|
||||
})) as ChannelCreatePayload;
|
||||
|
||||
const channel = await structures.createChannel(result);
|
||||
return channel;
|
||||
}
|
||||
|
||||
/** Delete a channel in your server. Bot needs MANAGE_CHANNEL permissions in the server. */
|
||||
export async function deleteChannel(
|
||||
guildID: string,
|
||||
channelID: string,
|
||||
reason?: string,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(
|
||||
guildID,
|
||||
["MANAGE_CHANNELS"],
|
||||
);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_CHANNELS);
|
||||
}
|
||||
|
||||
return RequestManager.delete(endpoints.CHANNEL(channelID), { reason });
|
||||
}
|
||||
|
||||
/** Returns a list of guild channel objects.
|
||||
*
|
||||
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
|
||||
*/
|
||||
export async function getChannels(guildID: string, addToCache = true) {
|
||||
const result = await RequestManager.get(
|
||||
endpoints.GUILD_CHANNELS(guildID),
|
||||
) as ChannelCreatePayload[];
|
||||
return Promise.all(result.map(async (res) => {
|
||||
const channel = await structures.createChannel(res, guildID);
|
||||
if (addToCache) {
|
||||
cacheHandlers.set("channels", channel.id, channel);
|
||||
}
|
||||
return channel;
|
||||
}));
|
||||
}
|
||||
|
||||
/** Fetches a single channel object from the api.
|
||||
*
|
||||
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your channels will be cached in your guild.**
|
||||
*/
|
||||
export async function getChannel(channelID: string, addToCache = true) {
|
||||
const result = await RequestManager.get(
|
||||
endpoints.GUILD_CHANNEL(channelID),
|
||||
) as ChannelCreatePayload;
|
||||
const channel = await structures.createChannel(result, result.guild_id);
|
||||
if (addToCache) cacheHandlers.set("channels", channel.id, channel);
|
||||
return channel;
|
||||
}
|
||||
|
||||
/** Modify the positions of channels on the guild. Requires MANAGE_CHANNELS permisison. */
|
||||
export function swapChannels(
|
||||
guildID: string,
|
||||
channelPositions: PositionSwap[],
|
||||
) {
|
||||
if (channelPositions.length < 2) {
|
||||
throw "You must provide atleast two channels to be swapped.";
|
||||
}
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_CHANNELS(guildID),
|
||||
channelPositions,
|
||||
);
|
||||
}
|
||||
|
||||
/** Returns a guild member object for the specified user.
|
||||
*
|
||||
* ⚠️ **ADVANCED USE ONLY: Your members will be cached in your guild most likely. Only use this when you are absolutely sure the member is not cached.**
|
||||
*/
|
||||
export async function getMember(
|
||||
guildID: string,
|
||||
id: string,
|
||||
options?: { force?: boolean },
|
||||
) {
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild && !options?.force) return;
|
||||
|
||||
const data = await RequestManager.get(
|
||||
endpoints.GUILD_MEMBER(guildID, id),
|
||||
) as MemberCreatePayload;
|
||||
|
||||
const member = await structures.createMember(data, guildID);
|
||||
return member;
|
||||
}
|
||||
|
||||
/** Returns guild member objects for the specified user by their nickname/username.
|
||||
*
|
||||
* ⚠️ **ADVANCED USE ONLY: Your members will be cached in your guild most likely. Only use this when you are absolutely sure the member is not cached.**
|
||||
*/
|
||||
export async function getMembersByQuery(
|
||||
guildID: string,
|
||||
name: string,
|
||||
limit = 1,
|
||||
) {
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
if (!guild) return;
|
||||
|
||||
return new Promise((resolve) => {
|
||||
requestAllMembers(guild, resolve, { query: name, limit });
|
||||
}) as Promise<Collection<string, Member>>;
|
||||
}
|
||||
|
||||
/** Create an emoji in the server. Emojis and animated emojis have a maximum file size of 256kb. Attempting to upload an emoji larger than this limit will fail and return 400 Bad Request and an error message, but not a JSON status code. If a URL is provided to the image parameter, Discordeno will automatically convert it to a base64 string internally. */
|
||||
export async function createEmoji(
|
||||
guildID: string,
|
||||
name: string,
|
||||
image: string,
|
||||
options: CreateEmojisOptions,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_EMOJIS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_EMOJIS);
|
||||
}
|
||||
|
||||
if (image && !image.startsWith("data:image/")) {
|
||||
image = await urlToBase64(image);
|
||||
}
|
||||
|
||||
return RequestManager.post(endpoints.GUILD_EMOJIS(guildID), {
|
||||
...options,
|
||||
name,
|
||||
image,
|
||||
});
|
||||
}
|
||||
|
||||
/** Modify the given emoji. Requires the MANAGE_EMOJIS permission. */
|
||||
export async function editEmoji(
|
||||
guildID: string,
|
||||
id: string,
|
||||
options: EditEmojisOptions,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_EMOJIS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_EMOJIS);
|
||||
}
|
||||
|
||||
return RequestManager.patch(endpoints.GUILD_EMOJI(guildID, id), {
|
||||
name: options.name,
|
||||
roles: options.roles,
|
||||
});
|
||||
}
|
||||
|
||||
/** Delete the given emoji. Requires the MANAGE_EMOJIS permission. Returns 204 No Content on success. */
|
||||
export async function deleteEmoji(
|
||||
guildID: string,
|
||||
id: string,
|
||||
reason?: string,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_EMOJIS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_EMOJIS);
|
||||
}
|
||||
|
||||
return RequestManager.delete(
|
||||
endpoints.GUILD_EMOJI(guildID, id),
|
||||
{ reason },
|
||||
);
|
||||
}
|
||||
|
||||
/** Creates a url to the emoji from the Discord CDN. */
|
||||
export function emojiURL(id: string, animated = false) {
|
||||
return `https://cdn.discordapp.com/emojis/${id}.${animated ? "gif" : "png"}`;
|
||||
}
|
||||
|
||||
/** Create a new role for the guild. Requires the MANAGE_ROLES permission. */
|
||||
export async function createGuildRole(
|
||||
guildID: string,
|
||||
options: CreateRoleOptions,
|
||||
reason?: string,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
const bits = calculateBits(options.permissions || []);
|
||||
|
||||
const result = await RequestManager.post(
|
||||
endpoints.GUILD_ROLES(guildID),
|
||||
{
|
||||
...options,
|
||||
permissions: calculateBits(options?.permissions || []),
|
||||
reason,
|
||||
},
|
||||
);
|
||||
|
||||
const roleData = result as RoleData;
|
||||
const role = await structures.createRole(roleData);
|
||||
const guild = await cacheHandlers.get("guilds", guildID);
|
||||
guild?.roles.set(role.id, role);
|
||||
return role;
|
||||
}
|
||||
|
||||
/** Edit a guild role. Requires the MANAGE_ROLES permission. */
|
||||
export async function editRole(
|
||||
guildID: string,
|
||||
id: string,
|
||||
options: CreateRoleOptions,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.patch(endpoints.GUILD_ROLE(guildID, id), {
|
||||
...options,
|
||||
permissions: options.permissions
|
||||
? calculateBits(options.permissions)
|
||||
: undefined,
|
||||
});
|
||||
}
|
||||
|
||||
/** Delete a guild role. Requires the MANAGE_ROLES permission. */
|
||||
export async function deleteRole(guildID: string, id: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.delete(endpoints.GUILD_ROLE(guildID, id));
|
||||
}
|
||||
|
||||
/** Returns a list of role objects for the guild.
|
||||
*
|
||||
* ⚠️ **If you need this, you are probably doing something wrong. This is not intended for use. Your roles will be cached in your guild.**
|
||||
*/
|
||||
export async function getRoles(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_ROLES(guildID));
|
||||
}
|
||||
|
||||
/** Modify the positions of a set of role objects for the guild. Requires the MANAGE_ROLES permission. */
|
||||
export async function swapRoles(guildID: string, rolePositons: PositionSwap) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.patch(endpoints.GUILD_ROLES(guildID), rolePositons);
|
||||
}
|
||||
|
||||
/** Check how many members would be removed from the server in a prune operation. Requires the KICK_MEMBERS permission */
|
||||
export async function getPruneCount(guildID: string, options: PruneOptions) {
|
||||
if (options.days < 1) {
|
||||
throw new Error(Errors.PRUNE_MIN_DAYS);
|
||||
}
|
||||
|
||||
const hasPerm = await botHasPermission(guildID, ["KICK_MEMBERS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_KICK_MEMBERS);
|
||||
}
|
||||
|
||||
const result = await RequestManager.get(
|
||||
endpoints.GUILD_PRUNE(guildID),
|
||||
{ ...options, include_roles: options.roles.join(",") },
|
||||
) as PrunePayload;
|
||||
|
||||
return result.pruned;
|
||||
}
|
||||
|
||||
/** Begin pruning all members in the given time period */
|
||||
export async function pruneMembers(guildID: string, options: PruneOptions) {
|
||||
if (options.days < 1) {
|
||||
throw new Error(Errors.PRUNE_MIN_DAYS);
|
||||
}
|
||||
|
||||
const hasPerm = await botHasPermission(guildID, ["KICK_MEMBERS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_KICK_MEMBERS);
|
||||
}
|
||||
|
||||
RequestManager.post(
|
||||
endpoints.GUILD_PRUNE(guildID),
|
||||
{ ...options, include_roles: options.roles.join(",") },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* ⚠️ BEGINNER DEVS!! YOU SHOULD ALMOST NEVER NEED THIS AND YOU CAN GET FROM cache.members.get()
|
||||
*
|
||||
* ADVANCED:
|
||||
* Highly recommended to use this function to fetch members instead of getMember from REST.
|
||||
* REST: 50/s global(across all shards) rate limit with ALL requests this included
|
||||
* GW(this function): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is now 960/m.
|
||||
*/
|
||||
export function fetchMembers(guild: Guild, options?: FetchMembersOptions) {
|
||||
// You can request 1 member without the intent
|
||||
if (
|
||||
(!options?.limit || options.limit > 1) &&
|
||||
!(identifyPayload.intents & Intents.GUILD_MEMBERS)
|
||||
) {
|
||||
throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS);
|
||||
}
|
||||
|
||||
if (options?.userIDs?.length) {
|
||||
options.limit = options.userIDs.length;
|
||||
}
|
||||
|
||||
return new Promise((resolve) => {
|
||||
requestAllMembers(guild, resolve, options);
|
||||
}) as Promise<Collection<string, Member>>;
|
||||
}
|
||||
|
||||
/** Returns the audit logs for the guild. Requires VIEW AUDIT LOGS permission */
|
||||
export async function getAuditLogs(
|
||||
guildID: string,
|
||||
options: GetAuditLogsOptions,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["VIEW_AUDIT_LOG"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_VIEW_AUDIT_LOG);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_AUDIT_LOGS(guildID), {
|
||||
...options,
|
||||
action_type: options.action_type
|
||||
? AuditLogs[options.action_type]
|
||||
: undefined,
|
||||
limit: options.limit && options.limit >= 1 && options.limit <= 100
|
||||
? options.limit
|
||||
: 50,
|
||||
});
|
||||
}
|
||||
|
||||
/** Returns the guild embed object. Requires the MANAGE_GUILD permission. */
|
||||
export async function getEmbed(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_EMBED(guildID));
|
||||
}
|
||||
|
||||
/** Modify a guild embed object for the guild. Requires the MANAGE_GUILD permission. */
|
||||
export async function editEmbed(
|
||||
guildID: string,
|
||||
enabled: boolean,
|
||||
channelID?: string | null,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_EMBED(guildID),
|
||||
{ enabled, channel_id: channelID },
|
||||
);
|
||||
}
|
||||
|
||||
/** Returns the code and uses of the vanity url for this server if it is enabled. Requires the MANAGE_GUILD permission. */
|
||||
export function getVanityURL(guildID: string) {
|
||||
return RequestManager.get(endpoints.GUILD_VANITY_URL(guildID));
|
||||
}
|
||||
|
||||
/** Returns a list of integrations for the guild. Requires the MANAGE_GUILD permission. */
|
||||
export async function getIntegrations(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_INTEGRATIONS(guildID));
|
||||
}
|
||||
|
||||
/** Modify the behavior and settings of an integration object for the guild. Requires the MANAGE_GUILD permission. */
|
||||
export async function editIntegration(
|
||||
guildID: string,
|
||||
id: string,
|
||||
options: EditIntegrationOptions,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_INTEGRATION(guildID, id),
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
/** Delete the attached integration object for the guild with this id. Requires MANAGE_GUILD permission. */
|
||||
export async function deleteIntegration(guildID: string, id: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.delete(endpoints.GUILD_INTEGRATION(guildID, id));
|
||||
}
|
||||
|
||||
/** Sync an integration. Requires the MANAGE_GUILD permission. */
|
||||
export async function syncIntegration(guildID: string, id: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.post(endpoints.GUILD_INTEGRATION_SYNC(guildID, id));
|
||||
}
|
||||
|
||||
/** Returns a list of ban objects for the users banned from this guild. Requires the BAN_MEMBERS permission. */
|
||||
export async function getBans(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["BAN_MEMBERS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_BAN_MEMBERS);
|
||||
}
|
||||
|
||||
const results = await RequestManager.get(
|
||||
endpoints.GUILD_BANS(guildID),
|
||||
) as BannedUser[];
|
||||
|
||||
return new Collection<string, BannedUser>(
|
||||
results.map((res) => [res.user.id, res]),
|
||||
);
|
||||
}
|
||||
|
||||
/** Returns a ban object for the given user or a 404 not found if the ban cannot be found. Requires the BAN_MEMBERS permission. */
|
||||
export async function getBan(guildID: string, memberID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["BAN_MEMBERS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_BAN_MEMBERS);
|
||||
}
|
||||
|
||||
return RequestManager.get(
|
||||
endpoints.GUILD_BAN(guildID, memberID),
|
||||
) as Promise<BannedUser>;
|
||||
}
|
||||
|
||||
/** Ban a user from the guild and optionally delete previous messages sent by the user. Requires the BAN_MEMBERS permission. */
|
||||
export async function ban(guildID: string, id: string, options: BanOptions) {
|
||||
const hasPerm = await botHasPermission(guildID, ["BAN_MEMBERS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_BAN_MEMBERS);
|
||||
}
|
||||
|
||||
return RequestManager.put(
|
||||
endpoints.GUILD_BAN(guildID, id),
|
||||
{ ...options, delete_message_days: options.days },
|
||||
);
|
||||
}
|
||||
|
||||
/** Remove the ban for a user. Requires BAN_MEMBERS permission */
|
||||
export async function unban(guildID: string, id: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["BAN_MEMBERS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_BAN_MEMBERS);
|
||||
}
|
||||
return RequestManager.delete(endpoints.GUILD_BAN(guildID, id));
|
||||
}
|
||||
|
||||
/** Modify a guilds settings. Requires the MANAGE_GUILD permission. */
|
||||
export async function editGuild(guildID: string, options: GuildEditOptions) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
if (options.icon && !options.icon.startsWith("data:image/")) {
|
||||
options.icon = await urlToBase64(options.icon);
|
||||
}
|
||||
|
||||
if (options.banner && !options.banner.startsWith("data:image/")) {
|
||||
options.banner = await urlToBase64(options.banner);
|
||||
}
|
||||
|
||||
if (options.splash && !options.splash.startsWith("data:image/")) {
|
||||
options.splash = await urlToBase64(options.splash);
|
||||
}
|
||||
|
||||
return RequestManager.patch(endpoints.GUILD(guildID), options);
|
||||
}
|
||||
|
||||
/** Get all the invites for this guild. Requires MANAGE_GUILD permission */
|
||||
export async function getInvites(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_INVITES(guildID));
|
||||
}
|
||||
|
||||
/** Leave a guild */
|
||||
export function leaveGuild(guildID: string) {
|
||||
return RequestManager.delete(endpoints.GUILD_LEAVE(guildID));
|
||||
}
|
||||
|
||||
/** Returns a list of voice region objects for the guild. Unlike the similar /voice route, this returns VIP servers when the guild is VIP-enabled. */
|
||||
export function getVoiceRegions(guildID: string) {
|
||||
return RequestManager.get(endpoints.GUILD_REGIONS(guildID));
|
||||
}
|
||||
|
||||
/** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
|
||||
export async function getWebhooks(guildID: string) {
|
||||
const hasPerm = await botHasPermission(
|
||||
guildID,
|
||||
["MANAGE_WEBHOOKS"],
|
||||
);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS);
|
||||
}
|
||||
|
||||
return RequestManager.get(endpoints.GUILD_WEBHOOKS(guildID));
|
||||
}
|
||||
|
||||
/** This function will return the raw user payload in the rare cases you need to fetch a user directly from the API. */
|
||||
export function getUser(userID: string) {
|
||||
return RequestManager.get(endpoints.USER(userID)) as Promise<UserPayload>;
|
||||
}
|
||||
|
||||
/**
|
||||
* ⚠️ **If you need this, you are probably doing something wrong. Always use cache.guilds.get()
|
||||
*
|
||||
* Advanced Devs:
|
||||
* This function fetches a guild's data. This is not the same data as a GUILD_CREATE.
|
||||
* So it does not cache the guild, you must do it manually.
|
||||
* */
|
||||
export function getGuild(guildID: string, counts = true) {
|
||||
return RequestManager.get(
|
||||
endpoints.GUILD(guildID),
|
||||
{ with_counts: counts },
|
||||
) as Promise<UpdateGuildPayload>;
|
||||
}
|
||||
|
||||
/** Returns the guild template if it exists */
|
||||
export function getGuildTemplate(
|
||||
guildID: string,
|
||||
templateCode: string,
|
||||
) {
|
||||
return RequestManager.get(
|
||||
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
|
||||
) as Promise<Template>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new guild based on a template
|
||||
* NOTE: This endpoint can be used only by bots in less than 10 guilds.
|
||||
*/
|
||||
export async function createGuildFromTemplate(
|
||||
templateCode: string,
|
||||
data: CreateGuildFromTemplate,
|
||||
) {
|
||||
if (await cacheHandlers.size("guilds") >= 10) {
|
||||
throw new Error(
|
||||
"This function can only be used by bots in less than 10 guilds.",
|
||||
);
|
||||
}
|
||||
|
||||
if (data.icon) {
|
||||
data.icon = await urlToBase64(data.icon);
|
||||
}
|
||||
|
||||
const guild = await RequestManager.post(
|
||||
endpoints.GUILD_TEMPLATE(templateCode),
|
||||
data,
|
||||
) as Promise<CreateGuildPayload>;
|
||||
return guild;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of templates.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function getGuildTemplates(guildID: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
const templates = await RequestManager.get(
|
||||
endpoints.GUILD_TEMPLATES(guildID),
|
||||
) as GuildTemplate[];
|
||||
return templates.map((template) => structures.createTemplate(template));
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a template from a guild.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function deleteGuildTemplate(
|
||||
guildID: string,
|
||||
templateCode: string,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
const deletedTemplate = await RequestManager.delete(
|
||||
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
|
||||
) as GuildTemplate;
|
||||
return structures.createTemplate(deletedTemplate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a template for the guild.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
* @param name name of the template (1-100 characters)
|
||||
* @param description description for the template (0-120 characters
|
||||
*/
|
||||
export async function createGuildTemplate(
|
||||
guildID: string,
|
||||
data: CreateGuildTemplate,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
if (data.name.length < 1 || data.name.length > 100) {
|
||||
throw new Error("The name can only be in between 1-100 characters.");
|
||||
}
|
||||
|
||||
if (
|
||||
data.description?.length &&
|
||||
data.description.length > 120
|
||||
) {
|
||||
throw new Error("The description can only be in between 0-120 characters.");
|
||||
}
|
||||
|
||||
const template = await RequestManager.post(
|
||||
endpoints.GUILD_TEMPLATES(guildID),
|
||||
data,
|
||||
) as GuildTemplate;
|
||||
return structures.createTemplate(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Syncs the template to the guild's current state.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function syncGuildTemplate(guildID: string, templateCode: string) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
const template = await RequestManager.put(
|
||||
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
|
||||
) as GuildTemplate;
|
||||
return structures.createTemplate(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a template's metadata.
|
||||
* Requires the `MANAGE_GUILD` permission.
|
||||
*/
|
||||
export async function editGuildTemplate(
|
||||
guildID: string,
|
||||
templateCode: string,
|
||||
data: EditGuildTemplate,
|
||||
) {
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_GUILD"]);
|
||||
if (!hasPerm) throw new Error(Errors.MISSING_MANAGE_GUILD);
|
||||
|
||||
if (data.name?.length && (data.name.length < 1 || data.name.length > 100)) {
|
||||
throw new Error("The name can only be in between 1-100 characters.");
|
||||
}
|
||||
|
||||
if (
|
||||
data.description?.length &&
|
||||
data.description.length > 120
|
||||
) {
|
||||
throw new Error("The description can only be in between 0-120 characters.");
|
||||
}
|
||||
|
||||
const template = await RequestManager.patch(
|
||||
`${endpoints.GUILD_TEMPLATES(guildID)}/${templateCode}`,
|
||||
data,
|
||||
) as GuildTemplate;
|
||||
return structures.createTemplate(template);
|
||||
}
|
||||
@@ -1,265 +0,0 @@
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
import { botID } from "../module/client.ts";
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
import { Member, structures } from "../structures/structures.ts";
|
||||
import {
|
||||
DMChannelCreatePayload,
|
||||
EditMemberOptions,
|
||||
Errors,
|
||||
ImageFormats,
|
||||
ImageSize,
|
||||
MessageContent,
|
||||
} from "../types/types.ts";
|
||||
import { formatImageURL } from "../utils/cdn.ts";
|
||||
import { endpoints } from "../utils/constants.ts";
|
||||
import {
|
||||
botHasPermission,
|
||||
higherRolePosition,
|
||||
highestRole,
|
||||
} from "../utils/permissions.ts";
|
||||
import { urlToBase64 } from "../utils/utils.ts";
|
||||
import { sendMessage } from "./channel.ts";
|
||||
|
||||
/** The users custom avatar or the default avatar if you don't have a member object. */
|
||||
export function rawAvatarURL(
|
||||
userID: string,
|
||||
discriminator: string,
|
||||
avatar?: string | null,
|
||||
size: ImageSize = 128,
|
||||
format?: ImageFormats,
|
||||
) {
|
||||
return avatar
|
||||
? formatImageURL(endpoints.USER_AVATAR(userID, avatar), size, format)
|
||||
: endpoints.USER_DEFAULT_AVATAR(Number(discriminator) % 5);
|
||||
}
|
||||
|
||||
/** The users custom avatar or the default avatar */
|
||||
export function avatarURL(
|
||||
member: Member,
|
||||
size: ImageSize = 128,
|
||||
format?: ImageFormats,
|
||||
) {
|
||||
return rawAvatarURL(
|
||||
member.id,
|
||||
member.discriminator,
|
||||
member.avatar,
|
||||
size,
|
||||
format,
|
||||
);
|
||||
}
|
||||
|
||||
/** Add a role to the member */
|
||||
export async function addRole(
|
||||
guildID: string,
|
||||
memberID: string,
|
||||
roleID: string,
|
||||
reason?: string,
|
||||
) {
|
||||
const botsHighestRole = await highestRole(guildID, botID);
|
||||
if (botsHighestRole) {
|
||||
const hasHigherRolePosition = await higherRolePosition(
|
||||
guildID,
|
||||
botsHighestRole.id,
|
||||
roleID,
|
||||
);
|
||||
if (!hasHigherRolePosition) {
|
||||
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW);
|
||||
}
|
||||
}
|
||||
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.put(
|
||||
endpoints.GUILD_MEMBER_ROLE(guildID, memberID, roleID),
|
||||
{ reason },
|
||||
);
|
||||
}
|
||||
|
||||
/** Remove a role from the member */
|
||||
export async function removeRole(
|
||||
guildID: string,
|
||||
memberID: string,
|
||||
roleID: string,
|
||||
reason?: string,
|
||||
) {
|
||||
const botsHighestRole = await highestRole(guildID, botID);
|
||||
|
||||
if (botsHighestRole) {
|
||||
const hasHigherRolePosition = await higherRolePosition(
|
||||
guildID,
|
||||
botsHighestRole.id,
|
||||
roleID,
|
||||
);
|
||||
if (!hasHigherRolePosition) {
|
||||
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW);
|
||||
}
|
||||
}
|
||||
|
||||
const hasPerm = await botHasPermission(guildID, ["MANAGE_ROLES"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
return RequestManager.delete(
|
||||
endpoints.GUILD_MEMBER_ROLE(guildID, memberID, roleID),
|
||||
{ reason },
|
||||
);
|
||||
}
|
||||
|
||||
/** Send a message to a users DM. Note: this takes 2 API calls. 1 is to fetch the users dm channel. 2 is to send a message to that channel. */
|
||||
export async function sendDirectMessage(
|
||||
memberID: string,
|
||||
content: string | MessageContent,
|
||||
) {
|
||||
let dmChannel = await cacheHandlers.get("channels", memberID);
|
||||
if (!dmChannel) {
|
||||
// If not available in cache create a new one.
|
||||
const dmChannelData = await RequestManager.post(
|
||||
endpoints.USER_CREATE_DM,
|
||||
{ recipient_id: memberID },
|
||||
) as DMChannelCreatePayload;
|
||||
// Channel create event will have added this channel to the cache
|
||||
cacheHandlers.delete("channels", dmChannelData.id);
|
||||
const channel = await structures.createChannel(dmChannelData);
|
||||
// Recreate the channel and add it undert he users id
|
||||
cacheHandlers.set("channels", memberID, channel);
|
||||
dmChannel = channel;
|
||||
}
|
||||
|
||||
// If it does exist try sending a message to this user
|
||||
return sendMessage(dmChannel.id, content);
|
||||
}
|
||||
|
||||
/** Kick a member from the server */
|
||||
export async function kick(guildID: string, memberID: string, reason?: string) {
|
||||
const botsHighestRole = await highestRole(guildID, botID);
|
||||
const membersHighestRole = await highestRole(guildID, memberID);
|
||||
if (
|
||||
botsHighestRole && membersHighestRole &&
|
||||
botsHighestRole.position <= membersHighestRole.position
|
||||
) {
|
||||
throw new Error(Errors.BOTS_HIGHEST_ROLE_TOO_LOW);
|
||||
}
|
||||
|
||||
const hasPerm = await botHasPermission(guildID, ["KICK_MEMBERS"]);
|
||||
if (!hasPerm) {
|
||||
throw new Error(Errors.MISSING_KICK_MEMBERS);
|
||||
}
|
||||
|
||||
return RequestManager.delete(
|
||||
endpoints.GUILD_MEMBER(guildID, memberID),
|
||||
{ reason },
|
||||
);
|
||||
}
|
||||
|
||||
/** Edit the member */
|
||||
export async function editMember(
|
||||
guildID: string,
|
||||
memberID: string,
|
||||
options: EditMemberOptions,
|
||||
) {
|
||||
if (options.nick) {
|
||||
if (options.nick.length > 32) {
|
||||
throw new Error(Errors.NICKNAMES_MAX_LENGTH);
|
||||
}
|
||||
|
||||
const hasManageNickPerm = await botHasPermission(
|
||||
guildID,
|
||||
["MANAGE_NICKNAMES"],
|
||||
);
|
||||
if (!hasManageNickPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_NICKNAMES);
|
||||
}
|
||||
}
|
||||
|
||||
const hasManageRolesPerm = await botHasPermission(
|
||||
guildID,
|
||||
["MANAGE_ROLES"],
|
||||
);
|
||||
if (
|
||||
options.roles &&
|
||||
!hasManageRolesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_ROLES);
|
||||
}
|
||||
|
||||
if (options.mute) {
|
||||
const hasMuteMembersPerm = await botHasPermission(
|
||||
guildID,
|
||||
["MUTE_MEMBERS"],
|
||||
);
|
||||
// TODO: This should check if the member is in a voice channel
|
||||
if (
|
||||
!hasMuteMembersPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MUTE_MEMBERS);
|
||||
}
|
||||
}
|
||||
|
||||
const hasDeafenMembersPerm = await botHasPermission(
|
||||
guildID,
|
||||
["DEAFEN_MEMBERS"],
|
||||
);
|
||||
if (
|
||||
options.deaf &&
|
||||
!hasDeafenMembersPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_DEAFEN_MEMBERS);
|
||||
}
|
||||
|
||||
// TODO: if channel id is provided check if the bot has CONNECT and MOVE in channel and current channel
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.GUILD_MEMBER(guildID, memberID),
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a member from a voice channel to another.
|
||||
* @param guildID the id of the guild which the channel exists in
|
||||
* @param memberID the id of the member to move.
|
||||
* @param channelID id of channel to move user to (if they are connected to voice)
|
||||
*/
|
||||
export function moveMember(
|
||||
guildID: string,
|
||||
memberID: string,
|
||||
channelID: string,
|
||||
) {
|
||||
return editMember(guildID, memberID, { channel_id: channelID });
|
||||
}
|
||||
|
||||
/** Modifies the bot's username or avatar.
|
||||
* NOTE: username: if changed may cause the bot's discriminator to be randomized.
|
||||
*/
|
||||
export async function editBotProfile(username?: string, avatarURL?: string) {
|
||||
// Nothing was edited
|
||||
if (!username && !avatarURL) return;
|
||||
// Check username requirements if username was provided
|
||||
if (username) {
|
||||
if (username.length > 32) {
|
||||
throw new Error(Errors.USERNAME_MAX_LENGTH);
|
||||
}
|
||||
if (username.length < 2) {
|
||||
throw new Error(Errors.USERNAME_MIN_LENGTH);
|
||||
}
|
||||
if (["@", "#", ":", "```"].some((char) => username.includes(char))) {
|
||||
throw new Error(Errors.USERNAME_INVALID_CHARACTER);
|
||||
}
|
||||
if (["discordtag", "everyone", "here"].includes(username)) {
|
||||
throw new Error(Errors.USERNAME_INVALID_USERNAME);
|
||||
}
|
||||
}
|
||||
|
||||
const avatar = avatarURL ? await urlToBase64(avatarURL) : undefined;
|
||||
RequestManager.patch(
|
||||
endpoints.USER_BOT,
|
||||
{
|
||||
username: username?.trim(),
|
||||
avatar,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -1,286 +0,0 @@
|
||||
import { delay } from "../../deps.ts";
|
||||
import { cacheHandlers } from "../controllers/cache.ts";
|
||||
import { botID } from "../module/client.ts";
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
import { Message, structures } from "../structures/structures.ts";
|
||||
import {
|
||||
Errors,
|
||||
MessageContent,
|
||||
MessageCreateOptions,
|
||||
UserPayload,
|
||||
} from "../types/types.ts";
|
||||
import { endpoints } from "../utils/constants.ts";
|
||||
import { botHasChannelPermissions } from "../utils/permissions.ts";
|
||||
|
||||
/** Delete a message with the channel id and message id only. */
|
||||
export async function deleteMessageByID(
|
||||
channelID: string,
|
||||
messageID: string,
|
||||
reason?: string,
|
||||
delayMilliseconds = 0,
|
||||
) {
|
||||
const message = await cacheHandlers.get("messages", messageID);
|
||||
if (message) return deleteMessage(message, reason, delayMilliseconds);
|
||||
|
||||
if (delayMilliseconds) await delay(delayMilliseconds);
|
||||
|
||||
return RequestManager.delete(
|
||||
endpoints.CHANNEL_MESSAGE(channelID, messageID),
|
||||
{ reason },
|
||||
);
|
||||
}
|
||||
|
||||
/** Delete a message */
|
||||
export async function deleteMessage(
|
||||
message: Message,
|
||||
reason?: string,
|
||||
delayMilliseconds = 0,
|
||||
) {
|
||||
if (message.author.id !== botID) {
|
||||
// This needs to check the channels permission not the guild permission
|
||||
const hasManageMessages = await botHasChannelPermissions(
|
||||
message.channelID,
|
||||
["MANAGE_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
!hasManageMessages
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
}
|
||||
|
||||
if (delayMilliseconds) await delay(delayMilliseconds);
|
||||
|
||||
return RequestManager.delete(
|
||||
endpoints.CHANNEL_MESSAGE(message.channelID, message.id),
|
||||
{ reason },
|
||||
);
|
||||
}
|
||||
|
||||
/** Pin a message in a channel. Requires MANAGE_MESSAGES. Max pins allowed in a channel = 50. */
|
||||
export async function pin(channelID: string, messageID: string) {
|
||||
const hasManageMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
!hasManageMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
RequestManager.put(endpoints.CHANNEL_MESSAGE(channelID, messageID));
|
||||
}
|
||||
|
||||
/** Unpin a message in a channel. Requires MANAGE_MESSAGES. */
|
||||
export async function unpin(channelID: string, messageID: string) {
|
||||
const hasManageMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
!hasManageMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
RequestManager.delete(
|
||||
endpoints.CHANNEL_MESSAGE(channelID, messageID),
|
||||
);
|
||||
}
|
||||
|
||||
/** Create a reaction for the message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */
|
||||
export async function addReaction(
|
||||
channelID: string,
|
||||
messageID: string,
|
||||
reaction: string,
|
||||
) {
|
||||
const hasAddReactionsPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["ADD_REACTIONS"],
|
||||
);
|
||||
if (!hasAddReactionsPerm) {
|
||||
throw new Error(Errors.MISSING_ADD_REACTIONS);
|
||||
}
|
||||
|
||||
const hasReadMessageHistoryPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["READ_MESSAGE_HISTORY"],
|
||||
);
|
||||
if (
|
||||
!hasReadMessageHistoryPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_READ_MESSAGE_HISTORY);
|
||||
}
|
||||
|
||||
if (reaction.startsWith("<:")) {
|
||||
reaction = reaction.substring(2, reaction.length - 1);
|
||||
} else if (reaction.startsWith("<a:")) {
|
||||
reaction = reaction.substring(3, reaction.length - 1);
|
||||
}
|
||||
|
||||
return RequestManager.put(
|
||||
endpoints.CHANNEL_MESSAGE_REACTION_ME(
|
||||
channelID,
|
||||
messageID,
|
||||
reaction,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** Adds multiple reactions to a message. If `ordered` is true(default is false), it will add the reactions one at a time in the order provided. Note: Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. Requires READ_MESSAGE_HISTORY and ADD_REACTIONS */
|
||||
export async function addReactions(
|
||||
channelID: string,
|
||||
messageID: string,
|
||||
reactions: string[],
|
||||
ordered = false,
|
||||
) {
|
||||
if (!ordered) {
|
||||
reactions.forEach((reaction) =>
|
||||
addReaction(channelID, messageID, reaction)
|
||||
);
|
||||
} else {
|
||||
for (const reaction of reactions) {
|
||||
await addReaction(channelID, messageID, reaction);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Removes a reaction from the bot on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
|
||||
export function removeReaction(
|
||||
channelID: string,
|
||||
messageID: string,
|
||||
reaction: string,
|
||||
) {
|
||||
return RequestManager.delete(
|
||||
endpoints.CHANNEL_MESSAGE_REACTION_ME(
|
||||
channelID,
|
||||
messageID,
|
||||
reaction,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** Removes a reaction from the specified user on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
|
||||
export async function removeUserReaction(
|
||||
channelID: string,
|
||||
messageID: string,
|
||||
reaction: string,
|
||||
userID: string,
|
||||
) {
|
||||
const hasManageMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_MESSAGES"],
|
||||
);
|
||||
if (!hasManageMessagesPerm) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
|
||||
return RequestManager.delete(
|
||||
endpoints.CHANNEL_MESSAGE_REACTION_USER(
|
||||
channelID,
|
||||
messageID,
|
||||
reaction,
|
||||
userID,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/** Removes all reactions for all emojis on this message. */
|
||||
export async function removeAllReactions(channelID: string, messageID: string) {
|
||||
const hasManageMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
!hasManageMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
return RequestManager.delete(
|
||||
endpoints.CHANNEL_MESSAGE_REACTIONS(channelID, messageID),
|
||||
);
|
||||
}
|
||||
|
||||
/** Removes all reactions for a single emoji on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
|
||||
export async function removeReactionEmoji(
|
||||
channelID: string,
|
||||
messageID: string,
|
||||
reaction: string,
|
||||
) {
|
||||
const hasManageMessagesPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
!hasManageMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_MESSAGES);
|
||||
}
|
||||
return RequestManager.delete(
|
||||
endpoints.CHANNEL_MESSAGE_REACTION(channelID, messageID, reaction),
|
||||
);
|
||||
}
|
||||
|
||||
/** Get a list of users that reacted with this emoji. */
|
||||
export async function getReactions(message: Message, reaction: string) {
|
||||
const result = (await RequestManager.get(
|
||||
endpoints.CHANNEL_MESSAGE_REACTION(message.channelID, message.id, reaction),
|
||||
)) as UserPayload[];
|
||||
|
||||
return Promise.all(result.map(async (res) => {
|
||||
const member = await cacheHandlers.get("members", res.id);
|
||||
return member || res;
|
||||
}));
|
||||
}
|
||||
|
||||
/** Edit the message. */
|
||||
export async function editMessage(
|
||||
message: Message,
|
||||
content: string | MessageContent,
|
||||
) {
|
||||
if (
|
||||
message.author.id !== botID
|
||||
) {
|
||||
throw "You can only edit a message that was sent by the bot.";
|
||||
}
|
||||
|
||||
if (typeof content === "string") content = { content };
|
||||
|
||||
const hasSendMessagesPerm = await botHasChannelPermissions(
|
||||
message.channelID,
|
||||
["SEND_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
!hasSendMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_MESSAGES);
|
||||
}
|
||||
|
||||
const hasSendTtsMessagesPerm = await botHasChannelPermissions(
|
||||
message.channelID,
|
||||
["SEND_TTS_MESSAGES"],
|
||||
);
|
||||
if (
|
||||
content.tts &&
|
||||
!hasSendTtsMessagesPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_SEND_TTS_MESSAGE);
|
||||
}
|
||||
|
||||
if (content.content && content.content.length > 2000) {
|
||||
throw new Error(Errors.MESSAGE_MAX_LENGTH);
|
||||
}
|
||||
|
||||
const result = await RequestManager.patch(
|
||||
endpoints.CHANNEL_MESSAGE(message.channelID, message.id),
|
||||
content,
|
||||
);
|
||||
return structures.createMessage(result as MessageCreateOptions);
|
||||
}
|
||||
|
||||
export async function publishMessage(channelID: string, messageID: string) {
|
||||
const data = await RequestManager.post(
|
||||
endpoints.CHANNEL_MESSAGE_CROSSPOST(channelID, messageID),
|
||||
) as MessageCreateOptions;
|
||||
|
||||
return structures.createMessage(data);
|
||||
}
|
||||
@@ -1,203 +0,0 @@
|
||||
import {
|
||||
channelOverwriteHasPermission,
|
||||
createInvite,
|
||||
deleteMessages,
|
||||
editChannel,
|
||||
followChannel,
|
||||
getChannelInvites,
|
||||
getChannelWebhooks,
|
||||
getMessage,
|
||||
getMessages,
|
||||
getPins,
|
||||
isChannelSynced,
|
||||
sendMessage,
|
||||
} from "./channel.ts";
|
||||
import {
|
||||
ban,
|
||||
categoryChildrenIDs,
|
||||
createEmoji,
|
||||
createGuildChannel,
|
||||
createGuildFromTemplate,
|
||||
createGuildRole,
|
||||
createGuildTemplate,
|
||||
createServer,
|
||||
deleteChannel,
|
||||
deleteEmoji,
|
||||
deleteGuildTemplate,
|
||||
deleteIntegration,
|
||||
deleteRole,
|
||||
deleteServer,
|
||||
editEmbed,
|
||||
editEmoji,
|
||||
editGuild,
|
||||
editGuildTemplate,
|
||||
editIntegration,
|
||||
editRole,
|
||||
emojiURL,
|
||||
fetchMembers,
|
||||
getAuditLogs,
|
||||
getBan,
|
||||
getBans,
|
||||
getChannel,
|
||||
getChannels,
|
||||
getEmbed,
|
||||
getGuild,
|
||||
getGuildTemplate,
|
||||
getGuildTemplates,
|
||||
getIntegrations,
|
||||
getInvites,
|
||||
getMember,
|
||||
getMembersByQuery,
|
||||
getPruneCount,
|
||||
getRoles,
|
||||
getUser,
|
||||
getVanityURL,
|
||||
getVoiceRegions,
|
||||
getWebhooks,
|
||||
guildBannerURL,
|
||||
guildIconURL,
|
||||
guildSplashURL,
|
||||
leaveGuild,
|
||||
pruneMembers,
|
||||
swapChannels,
|
||||
swapRoles,
|
||||
syncGuildTemplate,
|
||||
syncIntegration,
|
||||
unban,
|
||||
} from "./guild.ts";
|
||||
import {
|
||||
addRole,
|
||||
avatarURL,
|
||||
editBotProfile,
|
||||
editMember,
|
||||
kick,
|
||||
moveMember,
|
||||
rawAvatarURL,
|
||||
removeRole,
|
||||
sendDirectMessage,
|
||||
} from "./member.ts";
|
||||
import {
|
||||
addReaction,
|
||||
addReactions,
|
||||
deleteMessage,
|
||||
deleteMessageByID,
|
||||
editMessage,
|
||||
getReactions,
|
||||
pin,
|
||||
publishMessage,
|
||||
removeAllReactions,
|
||||
removeReaction,
|
||||
removeReactionEmoji,
|
||||
removeUserReaction,
|
||||
unpin,
|
||||
} from "./message.ts";
|
||||
import { createWebhook, executeWebhook, getWebhook } from "./webhook.ts";
|
||||
|
||||
export let handlers = {
|
||||
// Channel handler
|
||||
channelOverwriteHasPermission,
|
||||
createInvite,
|
||||
deleteMessages,
|
||||
editChannel,
|
||||
followChannel,
|
||||
getChannelInvites,
|
||||
getChannelWebhooks,
|
||||
getMessage,
|
||||
getMessages,
|
||||
getPins,
|
||||
isChannelSynced,
|
||||
sendMessage,
|
||||
|
||||
// Guild handler
|
||||
ban,
|
||||
categoryChildrenIDs,
|
||||
createEmoji,
|
||||
createGuildChannel,
|
||||
createGuildFromTemplate,
|
||||
createGuildRole,
|
||||
createGuildTemplate,
|
||||
createServer,
|
||||
deleteChannel,
|
||||
deleteEmoji,
|
||||
deleteGuildTemplate,
|
||||
deleteIntegration,
|
||||
deleteRole,
|
||||
deleteServer,
|
||||
editEmbed,
|
||||
editEmoji,
|
||||
editGuild,
|
||||
editGuildTemplate,
|
||||
editIntegration,
|
||||
editRole,
|
||||
emojiURL,
|
||||
fetchMembers,
|
||||
getAuditLogs,
|
||||
getBan,
|
||||
getBans,
|
||||
getChannel,
|
||||
getChannels,
|
||||
getEmbed,
|
||||
getGuild,
|
||||
getGuildTemplate,
|
||||
getGuildTemplates,
|
||||
getIntegrations,
|
||||
getInvites,
|
||||
getMember,
|
||||
getMembersByQuery,
|
||||
getPruneCount,
|
||||
getRoles,
|
||||
getUser,
|
||||
getVanityURL,
|
||||
getVoiceRegions,
|
||||
getWebhooks,
|
||||
guildBannerURL,
|
||||
guildIconURL,
|
||||
guildSplashURL,
|
||||
leaveGuild,
|
||||
pruneMembers,
|
||||
swapChannels,
|
||||
swapRoles,
|
||||
syncGuildTemplate,
|
||||
syncIntegration,
|
||||
unban,
|
||||
|
||||
// Member handler
|
||||
addRole,
|
||||
avatarURL,
|
||||
editBotProfile,
|
||||
editMember,
|
||||
kick,
|
||||
moveMember,
|
||||
rawAvatarURL,
|
||||
removeRole,
|
||||
sendDirectMessage,
|
||||
|
||||
// Message handler
|
||||
addReaction,
|
||||
addReactions,
|
||||
deleteMessage,
|
||||
deleteMessageByID,
|
||||
editMessage,
|
||||
getReactions,
|
||||
pin,
|
||||
publishMessage,
|
||||
removeAllReactions,
|
||||
removeReaction,
|
||||
removeReactionEmoji,
|
||||
removeUserReaction,
|
||||
unpin,
|
||||
|
||||
// Webhook handler
|
||||
createWebhook,
|
||||
executeWebhook,
|
||||
getWebhook,
|
||||
};
|
||||
|
||||
export type Handlers = typeof handlers;
|
||||
|
||||
export function updateHandlers(newHandlers: Partial<Handlers>) {
|
||||
handlers = {
|
||||
...handlers,
|
||||
...newHandlers,
|
||||
};
|
||||
}
|
||||
@@ -1,173 +0,0 @@
|
||||
import { RequestManager } from "../module/requestManager.ts";
|
||||
import { structures } from "../structures/structures.ts";
|
||||
import {
|
||||
EditWebhookMessageOptions,
|
||||
Errors,
|
||||
ExecuteWebhookOptions,
|
||||
MessageCreateOptions,
|
||||
WebhookCreateOptions,
|
||||
WebhookPayload,
|
||||
} from "../types/types.ts";
|
||||
import { endpoints } from "../utils/constants.ts";
|
||||
import { botHasChannelPermissions } from "../utils/permissions.ts";
|
||||
import { urlToBase64 } from "../utils/utils.ts";
|
||||
|
||||
/** Create a new webhook. Requires the MANAGE_WEBHOOKS permission. Returns a webhook object on success. Webhook names follow our naming restrictions that can be found in our Usernames and Nicknames documentation, with the following additional stipulations:
|
||||
*
|
||||
* Webhook names cannot be: 'clyde'
|
||||
*/
|
||||
export async function createWebhook(
|
||||
channelID: string,
|
||||
options: WebhookCreateOptions,
|
||||
) {
|
||||
const hasManageWebhooksPerm = await botHasChannelPermissions(
|
||||
channelID,
|
||||
["MANAGE_WEBHOOKS"],
|
||||
);
|
||||
if (
|
||||
!hasManageWebhooksPerm
|
||||
) {
|
||||
throw new Error(Errors.MISSING_MANAGE_WEBHOOKS);
|
||||
}
|
||||
|
||||
if (
|
||||
// Specific usernames that discord does not allow
|
||||
options.name === "clyde" ||
|
||||
// Character limit checks. [...] checks are because of js unicode length handling
|
||||
[...options.name].length < 2 || [...options.name].length > 32
|
||||
) {
|
||||
throw new Error(Errors.INVALID_WEBHOOK_NAME);
|
||||
}
|
||||
|
||||
return RequestManager.post(
|
||||
endpoints.CHANNEL_WEBHOOKS(channelID),
|
||||
{
|
||||
...options,
|
||||
avatar: options.avatar ? await urlToBase64(options.avatar) : undefined,
|
||||
},
|
||||
) as Promise<WebhookPayload>;
|
||||
}
|
||||
|
||||
export async function executeWebhook(
|
||||
webhookID: string,
|
||||
webhookToken: string,
|
||||
options: ExecuteWebhookOptions,
|
||||
) {
|
||||
if (!options.content && !options.file && !options.embeds) {
|
||||
throw new Error(Errors.INVALID_WEBHOOK_OPTIONS);
|
||||
}
|
||||
|
||||
if (options.content && options.content.length > 2000) {
|
||||
throw Error(Errors.MESSAGE_MAX_LENGTH);
|
||||
}
|
||||
|
||||
if (options.embeds && options.embeds.length > 10) {
|
||||
options.embeds.splice(10);
|
||||
}
|
||||
|
||||
if (options.mentions) {
|
||||
if (options.mentions.users?.length) {
|
||||
if (options.mentions.parse.includes("users")) {
|
||||
options.mentions.parse = options.mentions.parse.filter((p) =>
|
||||
p !== "users"
|
||||
);
|
||||
}
|
||||
|
||||
if (options.mentions.users.length > 100) {
|
||||
options.mentions.users = options.mentions.users.slice(0, 100);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.mentions.roles?.length) {
|
||||
if (options.mentions.parse.includes("roles")) {
|
||||
options.mentions.parse = options.mentions.parse.filter((p) =>
|
||||
p !== "roles"
|
||||
);
|
||||
}
|
||||
|
||||
if (options.mentions.roles.length > 100) {
|
||||
options.mentions.roles = options.mentions.roles.slice(0, 100);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = await RequestManager.post(
|
||||
`${endpoints.WEBHOOK(webhookID, webhookToken)}${
|
||||
options.wait ? "?wait=true" : ""
|
||||
}`,
|
||||
{
|
||||
...options,
|
||||
allowed_mentions: options.mentions,
|
||||
avatar_url: options.avatar_url,
|
||||
},
|
||||
);
|
||||
if (!options.wait) return;
|
||||
|
||||
return structures.createMessage(result as MessageCreateOptions);
|
||||
}
|
||||
|
||||
export function getWebhook(webhookID: string) {
|
||||
return RequestManager.get(endpoints.WEBHOOK_ID(webhookID));
|
||||
}
|
||||
|
||||
export function editWebhookMessage(
|
||||
webhookID: string,
|
||||
webhookToken: string,
|
||||
messageID: string,
|
||||
options: EditWebhookMessageOptions,
|
||||
) {
|
||||
if (options.content && options.content.length > 2000) {
|
||||
throw Error(Errors.MESSAGE_MAX_LENGTH);
|
||||
}
|
||||
|
||||
if (options.embeds && options.embeds.length > 10) {
|
||||
options.embeds.splice(10);
|
||||
}
|
||||
|
||||
if (options.allowed_mentions) {
|
||||
if (options.allowed_mentions.users?.length) {
|
||||
if (options.allowed_mentions.parse.includes("users")) {
|
||||
options.allowed_mentions.parse = options.allowed_mentions.parse.filter((
|
||||
p,
|
||||
) => p !== "users");
|
||||
}
|
||||
|
||||
if (options.allowed_mentions.users.length > 100) {
|
||||
options.allowed_mentions.users = options.allowed_mentions.users.slice(
|
||||
0,
|
||||
100,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (options.allowed_mentions.roles?.length) {
|
||||
if (options.allowed_mentions.parse.includes("roles")) {
|
||||
options.allowed_mentions.parse = options.allowed_mentions.parse.filter((
|
||||
p,
|
||||
) => p !== "roles");
|
||||
}
|
||||
|
||||
if (options.allowed_mentions.roles.length > 100) {
|
||||
options.allowed_mentions.roles = options.allowed_mentions.roles.slice(
|
||||
0,
|
||||
100,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return RequestManager.patch(
|
||||
endpoints.WEBHOOK_EDIT(webhookID, webhookToken, messageID),
|
||||
{ ...options, allowed_mentions: options.allowed_mentions },
|
||||
);
|
||||
}
|
||||
|
||||
export function deleteWebhookMessage(
|
||||
webhookID: string,
|
||||
webhookToken: string,
|
||||
messageID: string,
|
||||
) {
|
||||
return RequestManager.delete(
|
||||
endpoints.WEBHOOK_DELETE(webhookID, webhookToken, messageID),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user