Merge branch 'main' into discord-prs

This commit is contained in:
ITOH
2021-04-13 22:01:06 +02:00
17 changed files with 137 additions and 286 deletions

View File

@@ -1,4 +1,3 @@
// TODO: DM support idk need to discuss how we solve this
import { eventHandlers } from "../../bot.ts";
import { cacheHandlers } from "../../cache.ts";
import { structures } from "../../structures/mod.ts";
@@ -8,16 +7,18 @@ import { DiscordInteraction } from "../../types/interactions/interaction.ts";
export async function handleInteractionCreate(data: DiscordGatewayPayload) {
const payload = data.d as DiscordInteraction;
const discordenoMember = await structures.createDiscordenoMember(
payload.member as DiscordGuildMemberWithUser,
payload.guild_id ?? "",
);
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
const discordenoMember = payload.guild_id
? await structures.createDiscordenoMember(
payload.member as DiscordGuildMemberWithUser,
payload.guild_id,
)
: undefined;
if (discordenoMember) {
await cacheHandlers.set("members", discordenoMember.id, discordenoMember);
eventHandlers.interactionGuildCreate?.(payload, discordenoMember);
} else {
eventHandlers.interactionDMCreate?.(payload);
}
eventHandlers.interactionCreate?.(
{
...payload,
member: discordenoMember,
},
);
eventHandlers.interactionCreate?.(payload, discordenoMember);
}

View File

@@ -1,16 +1,23 @@
import { rest } from "../../rest/rest.ts";
import { DiscordWebhook } from "../../types/webhooks/webhook.ts";
import { DiscordWebhook, Webhook } from "../../types/webhooks/webhook.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Gets the webhooks for this channel. Requires MANAGE_WEBHOOKS */
export async function getChannelWebhooks(channelId: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_WEBHOOKS"]);
const result = await rest.runMethod(
const result = (await rest.runMethod(
"get",
endpoints.CHANNEL_WEBHOOKS(channelId),
);
)) as DiscordWebhook[];
return result as DiscordWebhook[];
return new Collection(
result.map((webhook) => [
webhook.id,
snakeKeysToCamelCase<Webhook>(webhook),
]),
);
}

View File

@@ -2,6 +2,7 @@ import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { structures } from "../../structures/mod.ts";
import { DiscordChannel } from "../../types/channels/channel.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
/** Returns a list of guild channel objects.
@@ -12,21 +13,27 @@ export async function getChannels(guildId: string, addToCache = true) {
const result = (await rest.runMethod(
"get",
endpoints.GUILD_CHANNELS(guildId),
) as DiscordChannel[]);
)) as DiscordChannel[];
return Promise.all(result.map(async (res) => {
const discordenoChannel = await structures.createDiscordenoChannel(
res,
guildId,
);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel,
);
}
return new Collection(
(
await Promise.all(
result.map(async (res) => {
const discordenoChannel = await structures.createDiscordenoChannel(
res,
guildId,
);
if (addToCache) {
await cacheHandlers.set(
"channels",
discordenoChannel.id,
discordenoChannel,
);
}
return discordenoChannel;
}));
return discordenoChannel;
}),
)
).map((c) => [c.id, c]),
);
}

View File

@@ -3,6 +3,7 @@ import { cacheHandlers } from "../../cache.ts";
import { rest } from "../../rest/rest.ts";
import { Emoji } from "../../types/emojis/emoji.ts";
import { Errors } from "../../types/misc/errors.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
/**
@@ -29,5 +30,5 @@ export async function getEmojis(guildId: string, addToCache = true) {
cacheHandlers.set("guilds", guildId, guild);
}
return result;
return new Collection(result.map((e) => [e.id!, e]));
}

View File

@@ -1,12 +1,12 @@
import { rest } from "../../rest/rest.ts";
import { InviteCreate } from "../../types/invites/invite_create.ts";
import { CreateChannelInvite } from "../../types/invites/create_channel_invite.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
/** Creates a new invite for this channel. Requires CREATE_INSTANT_INVITE */
export async function createInvite(
channelId: string,
options: InviteCreate,
options: CreateChannelInvite,
) {
await requireBotChannelPermissions(channelId, ["CREATE_INSTANT_INVITE"]);

View File

@@ -1,15 +1,20 @@
import { rest } from "../../rest/rest.ts";
import { DiscordInvite, Invite } from "../../types/invites/invite.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotChannelPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Gets the invites for this channel. Requires MANAGE_CHANNEL */
export async function getChannelInvites(channelId: string) {
await requireBotChannelPermissions(channelId, ["MANAGE_CHANNELS"]);
const result = await rest.runMethod(
const result = (await rest.runMethod(
"get",
endpoints.CHANNEL_INVITES(channelId),
);
)) as DiscordInvite[];
return result;
return new Collection(
result.map((invite) => [invite.code, snakeKeysToCamelCase<Invite>(invite)]),
);
}

View File

@@ -1,12 +1,20 @@
import { rest } from "../../rest/rest.ts";
import { DiscordInvite, Invite } from "../../types/invites/invite.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Get all the invites for this guild. Requires MANAGE_GUILD permission */
export async function getInvites(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_GUILD"]);
const result = await rest.runMethod("get", endpoints.GUILD_INVITES(guildId));
const result = (await rest.runMethod(
"get",
endpoints.GUILD_INVITES(guildId),
)) as DiscordInvite[];
return result;
return new Collection(
result.map((invite) => [invite.code, snakeKeysToCamelCase<Invite>(invite)]),
);
}

View File

@@ -1,6 +1,9 @@
import { rest } from "../../rest/rest.ts";
import { DiscordRole, Role } from "../../types/permissions/role.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Returns a list of role objects for the guild.
*
@@ -9,7 +12,12 @@ import { requireBotGuildPermissions } from "../../util/permissions.ts";
export async function getRoles(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_ROLES"]);
const result = await rest.runMethod("get", endpoints.GUILD_ROLES(guildId));
const result = (await rest.runMethod(
"get",
endpoints.GUILD_ROLES(guildId),
)) as DiscordRole[];
return result;
return new Collection(
result.map((role) => [role.id, snakeKeysToCamelCase<Role>(role)]),
);
}

View File

@@ -3,6 +3,7 @@ import { structures } from "../../structures/mod.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { DiscordTemplate } from "../../types/templates/template.ts";
import { Collection } from "../../util/collection.ts";
/**
* Returns an array of templates.
@@ -16,5 +17,10 @@ export async function getGuildTemplates(guildId: string) {
endpoints.GUILD_TEMPLATES(guildId),
)) as DiscordTemplate[];
return templates.map((template) => structures.createTemplateStruct(template));
return new Collection(
templates.map((template) => [
template.code,
structures.createTemplateStruct(template),
]),
);
}

View File

@@ -1,12 +1,23 @@
import { rest } from "../../rest/rest.ts";
import { DiscordWebhook, Webhook } from "../../types/webhooks/webhook.ts";
import { Collection } from "../../util/collection.ts";
import { endpoints } from "../../util/constants.ts";
import { requireBotGuildPermissions } from "../../util/permissions.ts";
import { snakeKeysToCamelCase } from "../../util/utils.ts";
/** Returns a list of guild webhooks objects. Requires the MANAGE_WEBHOOKs permission. */
export async function getWebhooks(guildId: string) {
await requireBotGuildPermissions(guildId, ["MANAGE_WEBHOOKS"]);
const result = await rest.runMethod("get", endpoints.GUILD_WEBHOOKS(guildId));
const result = (await rest.runMethod(
"get",
endpoints.GUILD_WEBHOOKS(guildId),
)) as DiscordWebhook[];
return result;
return new Collection(
result.map((webhook) => [
webhook.id,
snakeKeysToCamelCase<Webhook>(webhook),
]),
);
}

View File

@@ -121,11 +121,19 @@ export interface EventHandlers {
member: DiscordenoMember,
oldMember?: DiscordenoMember,
) => unknown;
// TODO: remove this?
//heartbeat?: () => unknown;
/** Sent when a user in a guild uses a Slash Command. */
/** Sent when a user uses a Slash Command. */
interactionCreate?: (
data: Omit<Interaction, "member"> & { member: DiscordenoMember },
data: Omit<Interaction, "member">,
member?: DiscordenoMember,
) => unknown;
/** Sent when a user uses a Slash Command in a guild. */
interactionGuildCreate?: (
data: Omit<Interaction, "member">,
member: DiscordenoMember,
) => unknown;
/** Sent when a user uses a Slash Command in a dm. */
interactionDMCreate?: (
data: Omit<Interaction, "member">,
) => unknown;
/** Sent when a message is created. */
messageCreate?: (message: DiscordenoMessage) => unknown;

View File

@@ -0,0 +1,18 @@
import { DiscordInviteTargetTypes } from "./invite_target_types.ts";
export interface CreateChannelInvite {
/** Durationi of invite in seconds before expiry, or 0 for never. Between 0 and 604800 (7 days). Default: 86400 (24 hours) */
maxAge?: number;
/** Max number of users or 0 for unlimited. Between 0 and 100. Default: 0 */
maxUses?: number;
/** Whether this invite only grants temporary membership. Default: false */
temporary?: boolean;
/** If true, don't try to reuse simmilar invite (useful for creating many unique one time use invites). Default: false */
unique?: boolean;
/** The type of target for this voice channel invite */
targetType?: DiscordInviteTargetTypes;
/** The id of the user whose stream to display for this invite, required if `target_type` is 1, the user must be streaming in the channel */
targetUserId?: string;
/** The id of the embedded application to open for this invite, required if `target_type` is 2, the application must have the `EMBEDDED` flag */
targetApplicationId?: string;
}

View File

@@ -0,0 +1,5 @@
/** https://discord.com/developers/docs/resources/invite#invite-object-invite-target-types */
export enum DiscordInviteTargetTypes {
Stream = 1,
EmbeddedApplication,
}

View File

@@ -7,9 +7,12 @@ export const API_VERSION = 8;
/** https://discord.com/developers/docs/topics/gateway#gateways-gateway-versions */
export const GATEWAY_VERSION = 8;
/** https://github.com/discordeno/discordeno/releases */
export const DISCORDENO_VERSION = 11;
/** https://discord.com/developers/docs/reference#user-agent */
export const USER_AGENT =
"DiscordBot (https://github.com/discordeno/discordeno, v10)";
`DiscordBot (https://github.com/discordeno/discordeno, v${DISCORDENO_VERSION})`;
/** https://discord.com/developers/docs/reference#image-formatting-image-base-url */
export const IMAGE_BASE_URL = "https://cdn.discordapp.com";

View File

@@ -8,7 +8,6 @@ export async function tellClusterToIdentify(
) {
// When resharding this may exist already
const oldShard = ws.shards.get(shardId);
// TODO: Use workers
await ws.identify(shardId, ws.maxShards);
if (oldShard) {

View File

@@ -173,5 +173,3 @@ Deno.test({
// TODO: Need to validate tests for these options
// /** Sorting position of the channel */
// position?: number;
// /** The channel's permission overwrites */
// permissionOverwrites?: Overwrite[];

View File

@@ -58,237 +58,3 @@ Deno.test({
},
...defaultTestOptions,
});
// // Role
// Deno.test({
// name: "[role] create a role in a guild",
// async fn() {
// if (!tempData.guildId) {
// throw new Error("guildId not present in temporary data");
// }
// const name = "Discordeno Test";
// const role = await createRole(tempData.guildId, {
// name,
// });
// // Assertions
// assertExists(role);
// assertEquals(role.name, name);
// tempData.roleId = role.id;
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[role] edit a role in a guild",
// async fn() {
// const name = "Discordeno Test Edited";
// const color = 4320244;
// const role = await editRole(tempData.guildId, tempData.roleId, {
// name,
// color,
// hoist: false,
// mentionable: false,
// }) as Role;
// // Assertions
// assertExists(role);
// assertEquals(role.name, name);
// assertEquals(role.color, color);
// assertEquals(role.hoist, false);
// assertEquals(role.mentionable, false);
// tempData.roleId = role.id;
// },
// ...defaultTestOptions,
// });
// // Channel
// Deno.test({
// name: "[channel] create a channel in a guild",
// async fn() {
// const channel = await createChannel(tempData.guildId, "test");
// // Assertions
// assertExists(channel);
// tempData.channelId = channel.id;
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[channel] get a channel in a guild",
// async fn() {
// const channel = await getChannel(tempData.channelId);
// // Assertions
// assertExists(channel);
// assertEquals(channel.id, tempData.channelId);
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[channel] edit a channel in a guild",
// async fn() {
// const channel = await editChannel(tempData.channelId, {
// name: "discordeno-test-edited",
// overwrites: [
// {
// id: tempData.roleId,
// type: OverwriteType.ROLE,
// allow: ["VIEW_CHANNEL", "SEND_MESSAGES"],
// deny: ["USE_EXTERNAL_EMOJIS"],
// },
// ],
// }) as Channel;
// // Wait 5s for CHANNEL_UPDATE to fire
// await delay(3000);
// // Assertions
// assertExists(channel);
// assertEquals(channel.name, "discordeno-test-edited");
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[channel] channel overwrite has permission",
// fn() {
// const channel = cache.channels.get(tempData.channelId);
// if (!channel) throw new Error("Channel not found");
// if (!channel.permissionOverwrites) {
// throw new Error("permissionOverwrites not found");
// }
// const hasPerm = channelOverwriteHasPermission(
// tempData.guildId,
// tempData.roleId,
// channel.permissionOverwrites,
// ["VIEW_CHANNEL", "SEND_MESSAGES"],
// );
// const missingPerm = channelOverwriteHasPermission(
// tempData.guildId,
// tempData.roleId,
// channel.permissionOverwrites,
// ["USE_EXTERNAL_EMOJIS"],
// );
// assertEquals(hasPerm, true);
// assertEquals(missingPerm, false);
// },
// ...defaultTestOptions,
// });
// // Message
// Deno.test({
// name: "[message] send a message in a text channel",
// async fn() {
// const message = await sendMessage(tempData.channelId, {
// embed: {
// title: "Discordeno Test",
// },
// });
// // Assertions
// assertExists(message);
// assertEquals(message.embeds[0].title, "Discordeno Test");
// tempData.messageId = message.id;
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[message] get a message in a guild",
// async fn() {
// const message = await getMessage(tempData.channelId, tempData.messageId);
// // Assertions
// assertExists(message);
// assertEquals(message.embeds[0].title, "Discordeno Test");
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[message] pin a message in a channel",
// async fn() {
// await pinMessage(tempData.channelId, tempData.messageId);
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[message] get pinned message in a channel",
// async fn() {
// const [msg] = await getPins(tempData.channelId);
// // Assertions
// assertExists(msg);
// assertEquals(msg.id, tempData.messageId);
// assertEquals(msg.pinned, true);
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[message] unpin a message",
// async fn() {
// await unpinMessage(tempData.channelId, tempData.messageId);
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[message] add a reaction to a message",
// async fn() {
// // TODO: add tests for a guild emoji ― <:name:id>
// await addReaction(tempData.channelId, tempData.messageId, "👍");
// },
// ...defaultTestOptions,
// });
// // TODO(ayntee): add unit tests for getReactions()
// Deno.test({
// name: "[message] remove a reaction to a message",
// async fn() {
// await removeReaction(tempData.channelId, tempData.messageId, "👍");
// },
// ...defaultTestOptions,
// });
// // Cleanup
// Deno.test({
// name: "[message] delete a message by channel Id",
// async fn() {
// await deleteMessage(tempData.channelId, tempData.messageId);
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[channel] delete a channel in a guild",
// async fn() {
// await deleteChannel(tempData.guildId, tempData.channelId);
// },
// ...defaultTestOptions,
// });
// Deno.test({
// name: "[role] delete a role in a guild",
// async fn() {
// await deleteRole(tempData.guildId, tempData.roleId);
// },
// ...defaultTestOptions,
// });