Merge branch 'main' of https://github.com/discordeno/discordeno into buttons-dropdowns

This commit is contained in:
Skillz
2021-05-01 08:20:04 -04:00
237 changed files with 630 additions and 997 deletions
+2 -2
View File
@@ -29,8 +29,8 @@ export async function cloneChannel(channelId: string, reason?: string) {
) => ({
id: overwrite.id,
type: overwrite.type,
allow: calculatePermissions(BigInt(overwrite.allow)),
deny: calculatePermissions(BigInt(overwrite.deny)),
allow: calculatePermissions(overwrite.allow),
deny: calculatePermissions(overwrite.deny),
})),
};
@@ -0,0 +1,17 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { ApplicationCommandPermissions } from "../../types/interactions/application_command_permissions.ts";
import { endpoints } from "../../util/constants.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Batch edits permissions for all commands in a guild. Takes an array of partial GuildApplicationCommandPermissions objects including `id` and `permissions`. */
export async function batchEditSlashCommandPermissions(
guildId: string,
options: { id: string; permissions: ApplicationCommandPermissions[] }[],
) {
return await rest.runMethod(
"put",
endpoints.COMMANDS_PERMISSIONS(applicationId, guildId),
camelKeysToSnakeCase(options),
);
}
+1 -1
View File
@@ -21,7 +21,7 @@ import {
*/
export async function createSlashCommand(
options: CreateGlobalApplicationCommand,
guildId: string,
guildId?: string,
) {
validateSlashCommands([options], true);
@@ -0,0 +1,18 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { ApplicationCommandPermissions } from "../../types/interactions/application_command_permissions.ts";
import { endpoints } from "../../util/constants.ts";
import { camelKeysToSnakeCase } from "../../util/utils.ts";
/** Edits command permissions for a specific command for your application in a guild. */
export async function editSlashCommandPermissions(
guildId: string,
commandId: string,
options: ApplicationCommandPermissions[],
) {
return await rest.runMethod(
"put",
endpoints.COMMANDS_PERMISSION(applicationId, guildId, commandId),
{ permissions: camelKeysToSnakeCase(options) },
);
}
@@ -0,0 +1,15 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { GuildApplicationCommandPermissions } from "../../types/interactions/guild_application_command_permissions.ts";
import { endpoints } from "../../util/constants.ts";
/** Fetches command permissions for a specific command for your application in a guild. Returns a GuildApplicationCommandPermissions object. */
export async function getSlashCommandPermission(
guildId: string,
commandId: string,
) {
return await rest.runMethod<GuildApplicationCommandPermissions>(
"get",
endpoints.COMMANDS_PERMISSION(applicationId, guildId, commandId),
);
}
@@ -0,0 +1,12 @@
import { applicationId } from "../../bot.ts";
import { rest } from "../../rest/rest.ts";
import { GuildApplicationCommandPermissions } from "../../types/interactions/guild_application_command_permissions.ts";
import { endpoints } from "../../util/constants.ts";
/** Fetches command permissions for all commands for your application in a guild. Returns an array of GuildApplicationCommandPermissions objects. */
export async function getSlashCommandPermissions(guildId: string) {
return await rest.runMethod<GuildApplicationCommandPermissions[]>(
"get",
endpoints.COMMANDS_PERMISSIONS(applicationId, guildId),
);
}
@@ -1,9 +1,9 @@
import { rest } from "../../rest/rest.ts";
import { DiscordValidateDiscoverySearchTerm } from "../../types/discovery/validate_discovery_search_term.ts";
import { ValidateDiscoverySearchTerm } from "../../types/discovery/validate_discovery_search_term.ts";
import { endpoints } from "../../util/constants.ts";
export async function validDiscoveryTerm(term: string) {
const result = await rest.runMethod<DiscordValidateDiscoverySearchTerm>(
const result = await rest.runMethod<ValidateDiscoverySearchTerm>(
"get",
endpoints.DISCOVERY_VALID_TERM,
{ term },
@@ -1,9 +1,8 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event.
*/
export async function deleteServer(guildId: string) {
/** Delete a guild permanently. User must be owner. Returns 204 No Content on success. Fires a Guild Delete Gateway event. */
export async function deleteGuild(guildId: string) {
return await rest.runMethod<undefined>(
"delete",
endpoints.GUILDS_BASE(guildId),
+1 -1
View File
@@ -12,5 +12,5 @@ export async function getWidget(guildId: string, options?: { force: boolean }) {
if (!guild?.widgetEnabled) throw new Error(Errors.GUILD_WIDGET_NOT_ENABLED);
}
return await rest.runMethod("get", `${endpoints.GUILD_WIDGET(guildId)}.json`) as GuildWidgetDetails;
return await rest.runMethod<GuildWidgetDetails>("get", `${endpoints.GUILD_WIDGET(guildId)}.json`);
}
+21 -19
View File
@@ -1,8 +1,11 @@
import { cache } from "../../cache.ts";
import { DiscordenoMember } from "../../structures/member.ts";
import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts";
import { DiscordGatewayIntents } from "../../types/gateway/gateway_intents.ts";
import { RequestGuildMembers } from "../../types/guilds/request_guild_members.ts";
import type { RequestGuildMembers } from "../../types/guilds/request_guild_members.ts";
import { Errors } from "../../types/misc/errors.ts";
import { Collection } from "../../util/collection.ts";
import { sendShardMessage } from "../../ws/send_shard_message.ts";
import { ws } from "../../ws/ws.ts";
/**
@@ -16,12 +19,12 @@ import { ws } from "../../ws/ws.ts";
export function fetchMembers(
guildId: string,
shardId: number,
options?: RequestGuildMembers,
options?: Omit<RequestGuildMembers, "guildId">,
) {
// You can request 1 member without the intent
if (
(!options?.limit || options.limit > 1) &&
!(ws.identifyPayload.intents && DiscordGatewayIntents.GUILD_MEMBERS)
!(ws.identifyPayload.intents & DiscordGatewayIntents.GUILD_MEMBERS)
) {
throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS);
}
@@ -31,21 +34,20 @@ export function fetchMembers(
}
return new Promise((resolve) => {
return requestAllMembers(guildId, shardId, resolve, options);
const nonce = `${guildId}-${Date.now()}`;
cache.fetchAllMembersProcessingRequests.set(nonce, resolve);
sendShardMessage(shardId, {
op: DiscordGatewayOpcodes.RequestGuildMembers,
d: {
guild_id: guildId,
// If a query is provided use it, OR if a limit is NOT provided use ""
query: options?.query || (options?.limit ? undefined : ""),
limit: options?.limit || 0,
presences: options?.presences || false,
user_ids: options?.userIds,
nonce,
},
});
}) as Promise<Collection<string, DiscordenoMember>>;
}
// TODO: finish implementing this
function requestAllMembers(
_guildId: string,
_shardId: number,
_resolve: (
value:
| Collection<string, DiscordenoMember>
| PromiseLike<Collection<string, DiscordenoMember>>,
) => void,
// deno-lint-ignore no-explicit-any
_options: any,
): void {
throw new Error("Function not implemented.");
}
+11 -6
View File
@@ -19,7 +19,10 @@ import { ws } from "../../ws/ws.ts";
* REST(this function): 50/s global(across all shards) rate limit with ALL requests this included
* GW(fetchMembers): 120/m(PER shard) rate limit. Meaning if you have 8 shards your limit is 960/m.
*/
export async function getMembers(guildId: string, options?: ListGuildMembers) {
export async function getMembers(
guildId: string,
options?: ListGuildMembers & { addToCache?: boolean },
) {
if (!(ws.identifyPayload.intents && DiscordGatewayIntents.GUILD_MEMBERS)) {
throw new Error(Errors.MISSING_INTENT_GUILD_MEMBERS);
}
@@ -61,11 +64,13 @@ export async function getMembers(guildId: string, options?: ListGuildMembers) {
guildId,
);
await cacheHandlers.set(
"members",
discordenoMember.id,
discordenoMember,
);
if (options?.addToCache !== false) {
await cacheHandlers.set(
"members",
discordenoMember.id,
discordenoMember,
);
}
return discordenoMember;
}),
@@ -1,37 +0,0 @@
import { cacheHandlers } from "../../cache.ts";
import { DiscordenoMember } from "../../structures/member.ts";
import { Collection } from "../../util/collection.ts";
/** 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) => {
return requestAllMembers(guild.id, guild.shardId, resolve, {
query: name,
limit,
});
}) as Promise<Collection<string, DiscordenoMember>>;
}
// TODO: implement this
function requestAllMembers(
_id: string,
_shardId: number,
_resolve: (
value:
| Collection<string, DiscordenoMember>
| PromiseLike<Collection<string, DiscordenoMember>>,
) => void,
_arg3: { query: string; limit: number },
): void {
throw new Error("Function not implemented.");
}
+15 -2
View File
@@ -1,12 +1,18 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Removes a reaction from the bot on this message. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
/** Removes a reaction from the given user on this message, defaults to bot. Reaction takes the form of **name:id** for custom guild emoji, or Unicode characters. */
export async function removeReaction(
channelId: string,
messageId: string,
reaction: string,
options?: { userId?: string },
) {
if (options?.userId) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
}
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
} else if (reaction.startsWith("<a:")) {
@@ -15,6 +21,13 @@ export async function removeReaction(
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction),
options?.userId
? endpoints.CHANNEL_MESSAGE_REACTION_USER(
channelId,
messageId,
reaction,
options.userId,
)
: endpoints.CHANNEL_MESSAGE_REACTION_ME(channelId, messageId, reaction),
);
}
@@ -1,29 +0,0 @@
import { rest } from "../../rest/rest.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** 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,
) {
await requireBotChannelPermissions(channelId, ["MANAGE_MESSAGES"]);
if (reaction.startsWith("<:")) {
reaction = reaction.substring(2, reaction.length - 1);
} else if (reaction.startsWith("<a:")) {
reaction = reaction.substring(3, reaction.length - 1);
}
return await rest.runMethod<undefined>(
"delete",
endpoints.CHANNEL_MESSAGE_REACTION_USER(
channelId,
messageId,
reaction,
userId,
),
);
}
+2 -2
View File
@@ -5,7 +5,7 @@ import { DiscordChannelTypes } from "../../types/channels/channel_types.ts";
import { DiscordAllowedMentionsTypes } from "../../types/messages/allowed_mentions_types.ts";
import { ButtonStyles } from "../../types/messages/components/button_styles.ts";
import { CreateMessage } from "../../types/messages/create_message.ts";
import { DiscordMessage, Message } from "../../types/messages/message.ts";
import { Message } from "../../types/messages/message.ts";
import { Errors } from "../../types/misc/errors.ts";
import { PermissionStrings } from "../../types/permissions/permission_strings.ts";
import { endpoints } from "../../util/constants.ts";
@@ -153,7 +153,7 @@ export async function sendMessage(
const result = await rest.runMethod<Message>(
"post",
endpoints.CHANNEL_MESSAGES(channelId),
camelKeysToSnakeCase<DiscordMessage>({
camelKeysToSnakeCase({
...content,
...(content.messageReference?.messageId
? {
+2 -2
View File
@@ -1,6 +1,7 @@
import { eventHandlers } from "../../bot.ts";
import { DiscordGatewayOpcodes } from "../../types/codes/gateway_opcodes.ts";
import type { StatusUpdate } from "../../types/gateway/status_update.ts";
import { sendShardMessage } from "../../ws/send_shard_message.ts";
import { ws } from "../../ws/ws.ts";
export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
@@ -10,7 +11,7 @@ export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
`Running forEach loop in editBotStatus function.`,
);
shard.queue.push({
sendShardMessage(shard, {
op: DiscordGatewayOpcodes.StatusUpdate,
d: {
since: null,
@@ -18,6 +19,5 @@ export function editBotStatus(data: Omit<StatusUpdate, "afk" | "since">) {
...data,
},
});
ws.processQueue(shard.id);
});
}
+19 -14
View File
@@ -13,15 +13,24 @@ import { getPins } from "./channels/get_pins.ts";
import { isChannelSynced } from "./channels/is_channel_synced.ts";
import { startTyping } from "./channels/start_typing.ts";
import { swapChannels } from "./channels/swap_channels.ts";
import { batchEditSlashCommandPermissions } from "./commands/batch_edit_slash_command_permissions.ts";
import { createSlashCommand } from "./commands/create_slash_command.ts";
import { deleteSlashCommand } from "./commands/delete_slash_command.ts";
import { deleteSlashResponse } from "./commands/delete_slash_response.ts";
import { editSlashCommandPermissions } from "./commands/edit_slash_command_permissions.ts";
import { editSlashResponse } from "./commands/edit_slash_response.ts";
import { getSlashCommand } from "./commands/get_slash_command.ts";
import { getSlashCommands } from "./commands/get_slash_commands.ts";
import { getSlashCommandPermission } from "./commands/get_slash_command_permission.ts";
import { getSlashCommandPermissions } from "./commands/get_slash_command_permissions.ts";
import { sendInteractionResponse } from "./commands/send_interaction_response.ts";
import { upsertSlashCommand } from "./commands/upsert_slash_command.ts";
import { upsertSlashCommands } from "./commands/upsert_slash_commands.ts";
import { addDiscoverySubcategory } from "./discovery/add_discovery_subcategory.ts";
import { editDiscovery } from "./discovery/edit_discovery.ts";
import { getDiscoveryCategories } from "./discovery/get_discovery_categories.ts";
import { removeDiscoverySubcategory } from "./discovery/remove_discovery_subcategory.ts";
import { validDiscoveryTerm } from "./discovery/valid_discovery_term.ts";
import { createEmoji } from "./emojis/create_emoji.ts";
import { deleteEmoji } from "./emojis/delete_emoji.ts";
import { editEmoji } from "./emojis/edit_emoji.ts";
@@ -29,7 +38,7 @@ import { emojiURL } from "./emojis/emoji_url.ts";
import { getEmoji } from "./emojis/get_emoji.ts";
import { getEmojis } from "./emojis/get_emojis.ts";
import { createGuild } from "./guilds/create_guild.ts";
import { deleteServer } from "./guilds/delete_server.ts";
import { deleteGuild } from "./guilds/delete_guild.ts";
import { editGuild } from "./guilds/edit_guild.ts";
import { editWelcomeScreen } from "./guilds/edit_welcome_screen.ts";
import { editWidget } from "./guilds/edit_widget.ts";
@@ -66,7 +75,6 @@ import { editMember } from "./members/edit_member.ts";
import { fetchMembers } from "./members/fetch_members.ts";
import { getMember } from "./members/get_member.ts";
import { getMembers } from "./members/get_members.ts";
import { getMembersByQuery } from "./members/get_members_by_query.ts";
import { kick, kickMember } from "./members/kick_member.ts";
import { moveMember } from "./members/move_member.ts";
import { pruneMembers } from "./members/prune_members.ts";
@@ -85,7 +93,6 @@ import { publishMessage } from "./messages/publish_message.ts";
import { removeAllReactions } from "./messages/remove_all_reactions.ts";
import { removeReaction } from "./messages/remove_reaction.ts";
import { removeReactionEmoji } from "./messages/remove_reaction_emoji.ts";
import { removeUserReaction } from "./messages/remove_user_reaction.ts";
import { sendMessage } from "./messages/send_message.ts";
import { unpin, unpinMessage } from "./messages/unpin_message.ts";
import { editBotStatus } from "./misc/edit_bot_status.ts";
@@ -115,11 +122,6 @@ import { executeWebhook } from "./webhooks/execute_webhook.ts";
import { getWebhook } from "./webhooks/get_webhook.ts";
import { getWebhooks } from "./webhooks/get_webhooks.ts";
import { getWebhookWithToken } from "./webhooks/get_webhook_with_token.ts";
import { addDiscoverySubcategory } from "./discovery/add_discovery_subcategory.ts";
import { editDiscovery } from "./discovery/edit_discovery.ts";
import { getDiscoveryCategories } from "./discovery/get_discovery_categories.ts";
import { removeDiscoverySubcategory } from "./discovery/remove_discovery_subcategory.ts";
import { validDiscoveryTerm } from "./discovery/valid_discovery_term.ts";
export {
addDiscoverySubcategory,
@@ -129,6 +131,7 @@ export {
avatarURL,
ban,
banMember,
batchEditSlashCommandPermissions,
categoryChildren,
channelOverwriteHasPermission,
createChannel,
@@ -143,13 +146,13 @@ export {
deleteChannel,
deleteChannelOverwrite,
deleteEmoji,
deleteGuild,
deleteGuildTemplate,
deleteIntegration,
deleteInvite,
deleteMessage,
deleteMessages,
deleteRole,
deleteServer,
deleteSlashCommand,
deleteSlashResponse,
deleteWebhook,
@@ -198,7 +201,6 @@ export {
getInvites,
getMember,
getMembers,
getMembersByQuery,
getMessage,
getMessages,
getPins,
@@ -206,6 +208,8 @@ export {
getReactions,
getRoles,
getSlashCommand,
getSlashCommandPermission,
getSlashCommandPermissions,
getSlashCommands,
getTemplate,
getUser,
@@ -235,7 +239,6 @@ export {
removeReaction,
removeReactionEmoji,
removeRole,
removeUserReaction,
sendDirectMessage,
sendInteractionResponse,
sendMessage,
@@ -272,6 +275,10 @@ export let helpers = {
deleteSlashCommand,
deleteSlashResponse,
editSlashResponse,
getSlashCommandPermission,
getSlashCommandPermissions,
batchEditSlashCommandPermissions,
editSlashCommandPermissions,
sendInteractionResponse,
getSlashCommand,
getSlashCommands,
@@ -286,7 +293,7 @@ export let helpers = {
// guilds
categoryChildren,
createGuild,
deleteServer,
deleteGuild,
editGuild,
editWidget,
editWelcomeScreen,
@@ -332,7 +339,6 @@ export let helpers = {
editMember,
fetchMembers,
getMember,
getMembersByQuery,
getMembers,
kickMember,
moveMember,
@@ -353,7 +359,6 @@ export let helpers = {
removeAllReactions,
removeReactionEmoji,
removeReaction,
removeUserReaction,
sendMessage,
unpinMessage,
// misc