From e49a23687e68b028187d44b3d7b097e4187208b3 Mon Sep 17 00:00:00 2001 From: Ayyan Date: Tue, 22 Dec 2020 21:26:27 +0400 Subject: [PATCH] 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 --- deps.ts | 7 ------- src/{ => api}/controllers/bans.ts | 4 ++-- src/{ => api}/controllers/cache.ts | 6 +++--- src/{ => api}/controllers/channels.ts | 4 ++-- src/{ => api}/controllers/guilds.ts | 6 +++--- src/{ => api}/controllers/members.ts | 6 +++--- src/{ => api}/controllers/messages.ts | 4 ++-- src/{ => api}/controllers/misc.ts | 16 +++++++++------- src/{ => api}/controllers/mod.ts | 0 src/{ => api}/controllers/reactions.ts | 4 ++-- src/{ => api}/controllers/roles.ts | 4 ++-- src/{ => api}/handlers/channel.ts | 8 ++++---- src/{ => api}/handlers/guild.ts | 18 +++++++++--------- src/{ => api}/handlers/member.ts | 14 +++++++------- src/{ => api}/handlers/message.ts | 12 ++++++------ src/{ => api}/handlers/mod.ts | 0 src/{ => api}/handlers/webhook.ts | 10 +++++----- src/{ => api}/structures/channel.ts | 2 +- src/{ => api}/structures/guild.ts | 4 ++-- src/{ => api}/structures/member.ts | 8 ++++++-- src/{ => api}/structures/message.ts | 2 +- src/{ => api}/structures/mod.ts | 0 src/{ => api}/structures/role.ts | 2 +- src/{ => api}/structures/structures.ts | 0 src/{ => api}/structures/template.ts | 2 +- src/{module/client.ts => bot.ts} | 8 ++++---- src/{module/requestManager.ts => rest/mod.ts} | 6 +++--- src/types/guild.ts | 2 +- src/types/message.ts | 2 +- src/types/options.ts | 2 +- src/{utils => util}/cache.ts | 7 ++++++- src/{utils => util}/cdn.ts | 0 src/{utils => util}/collection.ts | 0 src/{utils => util}/constants.ts | 0 src/{utils => util}/permissions.ts | 6 +++--- src/{utils => util}/utils.ts | 12 ++++++++++-- src/ws/deps.ts | 1 + src/{module/shard.ts => ws/mod.ts} | 12 ++++++------ .../shardingManager.ts => ws/shard_manager.ts} | 13 ++++++------- tests/deps.ts | 5 +++++ 40 files changed, 118 insertions(+), 101 deletions(-) rename src/{ => api}/controllers/bans.ts (87%) rename src/{ => api}/controllers/cache.ts (96%) rename src/{ => api}/controllers/channels.ts (96%) rename src/{ => api}/controllers/guilds.ts (96%) rename src/{ => api}/controllers/members.ts (96%) rename src/{ => api}/controllers/messages.ts (97%) rename src/{ => api}/controllers/misc.ts (91%) rename src/{ => api}/controllers/mod.ts (100%) rename src/{ => api}/controllers/reactions.ts (97%) rename src/{ => api}/controllers/roles.ts (96%) rename src/{ => api}/handlers/channel.ts (98%) rename src/{ => api}/handlers/guild.ts (98%) rename src/{ => api}/handlers/member.ts (95%) rename src/{ => api}/handlers/message.ts (96%) rename src/{ => api}/handlers/mod.ts (100%) rename src/{ => api}/handlers/webhook.ts (94%) rename src/{ => api}/structures/channel.ts (94%) rename src/{ => api}/structures/guild.ts (97%) rename src/{ => api}/structures/member.ts (90%) rename src/{ => api}/structures/message.ts (93%) rename src/{ => api}/structures/mod.ts (100%) rename src/{ => api}/structures/role.ts (88%) rename src/{ => api}/structures/structures.ts (100%) rename src/{ => api}/structures/template.ts (91%) rename src/{module/client.ts => bot.ts} (94%) rename src/{module/requestManager.ts => rest/mod.ts} (98%) rename src/{utils => util}/cache.ts (89%) rename src/{utils => util}/cdn.ts (100%) rename src/{utils => util}/collection.ts (100%) rename src/{utils => util}/constants.ts (100%) rename src/{utils => util}/permissions.ts (98%) rename src/{utils => util}/utils.ts (80%) create mode 100644 src/ws/deps.ts rename src/{module/shard.ts => ws/mod.ts} (97%) rename src/{module/shardingManager.ts => ws/shard_manager.ts} (88%) create mode 100644 tests/deps.ts diff --git a/deps.ts b/deps.ts index 0f20500ec..6d66e7974 100644 --- a/deps.ts +++ b/deps.ts @@ -1,8 +1 @@ -export { delay } from "https://deno.land/std@0.81.0/async/delay.ts"; export { encode } from "https://deno.land/std@0.81.0/encoding/base64.ts"; -export { - assert, - assertArrayIncludes, - assertEquals, -} from "https://deno.land/std@0.81.0/testing/asserts.ts"; -export { decompress_with as inflate } from "https://unpkg.com/@evan/wasm@0.0.25/target/zlib/deno.js"; diff --git a/src/controllers/bans.ts b/src/api/controllers/bans.ts similarity index 87% rename from src/controllers/bans.ts rename to src/api/controllers/bans.ts index be689ade5..22039f053 100644 --- a/src/controllers/bans.ts +++ b/src/api/controllers/bans.ts @@ -1,5 +1,5 @@ -import { eventHandlers } from "../module/client.ts"; -import { DiscordPayload, GuildBanPayload } from "../types/types.ts"; +import { eventHandlers } from "../../bot.ts"; +import { DiscordPayload, GuildBanPayload } from "../../types/types.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalGuildBanAdd(data: DiscordPayload) { diff --git a/src/controllers/cache.ts b/src/api/controllers/cache.ts similarity index 96% rename from src/controllers/cache.ts rename to src/api/controllers/cache.ts index e6cbdfc58..10edc18ba 100644 --- a/src/controllers/cache.ts +++ b/src/api/controllers/cache.ts @@ -1,7 +1,7 @@ import { Channel, Guild, Member, Message } from "../structures/structures.ts"; -import { PresenceUpdatePayload } from "../types/types.ts"; -import { cache } from "../utils/cache.ts"; -import { Collection } from "../utils/collection.ts"; +import { PresenceUpdatePayload } from "../../types/types.ts"; +import { cache } from "../../util/cache.ts"; +import { Collection } from "../../util/collection.ts"; export type TableName = | "guilds" diff --git a/src/controllers/channels.ts b/src/api/controllers/channels.ts similarity index 96% rename from src/controllers/channels.ts rename to src/api/controllers/channels.ts index b9dc88ebc..f967c1830 100644 --- a/src/controllers/channels.ts +++ b/src/api/controllers/channels.ts @@ -1,10 +1,10 @@ -import { eventHandlers } from "../module/client.ts"; +import { eventHandlers } from "../../bot.ts"; import { structures } from "../structures/structures.ts"; import { ChannelCreatePayload, ChannelTypes, DiscordPayload, -} from "../types/types.ts"; +} from "../../types/types.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalChannelCreate(data: DiscordPayload) { diff --git a/src/controllers/guilds.ts b/src/api/controllers/guilds.ts similarity index 96% rename from src/controllers/guilds.ts rename to src/api/controllers/guilds.ts index 1d93068c9..c8c2d41fe 100644 --- a/src/controllers/guilds.ts +++ b/src/api/controllers/guilds.ts @@ -1,4 +1,4 @@ -import { eventHandlers } from "../module/client.ts"; +import { eventHandlers } from "../../bot.ts"; import { structures } from "../structures/structures.ts"; import { CreateGuildPayload, @@ -7,8 +7,8 @@ import { GuildEmojisUpdatePayload, GuildUpdateChange, UpdateGuildPayload, -} from "../types/types.ts"; -import { cache } from "../utils/cache.ts"; +} from "../../types/types.ts"; +import { cache } from "../../util/cache.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalGuildCreate( diff --git a/src/controllers/members.ts b/src/api/controllers/members.ts similarity index 96% rename from src/controllers/members.ts rename to src/api/controllers/members.ts index 12032d4e7..1e7983ca8 100644 --- a/src/controllers/members.ts +++ b/src/api/controllers/members.ts @@ -1,4 +1,4 @@ -import { eventHandlers } from "../module/client.ts"; +import { eventHandlers } from "../../bot.ts"; import { structures } from "../structures/structures.ts"; import { DiscordPayload, @@ -6,8 +6,8 @@ import { GuildMemberAddPayload, GuildMemberChunkPayload, GuildMemberUpdatePayload, -} from "../types/types.ts"; -import { cache } from "../utils/cache.ts"; +} from "../../types/types.ts"; +import { cache } from "../../util/cache.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalGuildMemberAdd(data: DiscordPayload) { diff --git a/src/controllers/messages.ts b/src/api/controllers/messages.ts similarity index 97% rename from src/controllers/messages.ts rename to src/api/controllers/messages.ts index 750ff0d0c..5a2d62ae6 100644 --- a/src/controllers/messages.ts +++ b/src/api/controllers/messages.ts @@ -1,11 +1,11 @@ -import { eventHandlers } from "../module/client.ts"; +import { eventHandlers } from "../../bot.ts"; import { structures } from "../structures/structures.ts"; import { DiscordPayload, MessageCreateOptions, MessageDeleteBulkPayload, MessageDeletePayload, -} from "../types/types.ts"; +} from "../../types/types.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalMessageCreate(data: DiscordPayload) { diff --git a/src/controllers/misc.ts b/src/api/controllers/misc.ts similarity index 91% rename from src/controllers/misc.ts rename to src/api/controllers/misc.ts index 7ef4ffa87..bf23d8966 100644 --- a/src/controllers/misc.ts +++ b/src/api/controllers/misc.ts @@ -1,8 +1,9 @@ -import { delay } from "../../deps.ts"; -import { initialMemberLoadQueue } from "../../mod.ts"; -import { eventHandlers, setBotID } from "../module/client.ts"; -import { allowNextShard } from "../module/shardingManager.ts"; -import { structures } from "../structures/structures.ts"; +import { + initialMemberLoadQueue, + structures, +} from "../structures/structures.ts"; +import { eventHandlers, setBotID } from "../../bot.ts"; +import { allowNextShard } from "../../ws/shard_manager.ts"; import { DiscordPayload, PresenceUpdatePayload, @@ -11,8 +12,9 @@ import { UserPayload, VoiceStateUpdatePayload, WebhookUpdatePayload, -} from "../types/types.ts"; -import { cache } from "../utils/cache.ts"; +} from "../../types/types.ts"; +import { cache } from "../../util/cache.ts"; +import { delay } from "../../util/utils.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalReady( diff --git a/src/controllers/mod.ts b/src/api/controllers/mod.ts similarity index 100% rename from src/controllers/mod.ts rename to src/api/controllers/mod.ts diff --git a/src/controllers/reactions.ts b/src/api/controllers/reactions.ts similarity index 97% rename from src/controllers/reactions.ts rename to src/api/controllers/reactions.ts index 4bcd7b06e..d0a19fc9d 100644 --- a/src/controllers/reactions.ts +++ b/src/api/controllers/reactions.ts @@ -1,11 +1,11 @@ -import { botID, eventHandlers } from "../module/client.ts"; +import { botID, eventHandlers } from "../../bot.ts"; import { structures } from "../structures/structures.ts"; import { BaseMessageReactionPayload, DiscordPayload, MessageReactionPayload, MessageReactionRemoveEmojiPayload, -} from "../types/types.ts"; +} from "../../types/types.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalMessageReactionAdd(data: DiscordPayload) { diff --git a/src/controllers/roles.ts b/src/api/controllers/roles.ts similarity index 96% rename from src/controllers/roles.ts rename to src/api/controllers/roles.ts index d45132ae6..95a8f439a 100644 --- a/src/controllers/roles.ts +++ b/src/api/controllers/roles.ts @@ -1,10 +1,10 @@ -import { eventHandlers } from "../module/client.ts"; +import { eventHandlers } from "../../bot.ts"; import { structures } from "../structures/structures.ts"; import { DiscordPayload, GuildRoleDeletePayload, GuildRolePayload, -} from "../types/types.ts"; +} from "../../types/types.ts"; import { cacheHandlers } from "./cache.ts"; export async function handleInternalGuildRoleCreate(data: DiscordPayload) { diff --git a/src/handlers/channel.ts b/src/api/handlers/channel.ts similarity index 98% rename from src/handlers/channel.ts rename to src/api/handlers/channel.ts index a06078fde..205882d00 100644 --- a/src/handlers/channel.ts +++ b/src/api/handlers/channel.ts @@ -1,5 +1,5 @@ import { cacheHandlers } from "../controllers/cache.ts"; -import { RequestManager } from "../module/requestManager.ts"; +import { RequestManager } from "../../rest/mod.ts"; import { structures } from "../structures/structures.ts"; import { ChannelEditOptions, @@ -17,12 +17,12 @@ import { Permissions, RawOverwrite, WebhookPayload, -} from "../types/types.ts"; -import { endpoints } from "../utils/constants.ts"; +} from "../../types/types.ts"; +import { endpoints } from "../../util/constants.ts"; import { botHasChannelPermissions, calculateBits, -} from "../utils/permissions.ts"; +} from "../../util/permissions.ts"; /** Checks if a channel overwrite for a user id or a role id has permission in this channel */ export function channelOverwriteHasPermission( diff --git a/src/handlers/guild.ts b/src/api/handlers/guild.ts similarity index 98% rename from src/handlers/guild.ts rename to src/api/handlers/guild.ts index 47c065fe6..69a1f3193 100644 --- a/src/handlers/guild.ts +++ b/src/api/handlers/guild.ts @@ -1,7 +1,7 @@ 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 { identifyPayload } from "../../bot.ts"; +import { RequestManager } from "../../rest/mod.ts"; +import { requestAllMembers } from "../../ws/shard_manager.ts"; import { Guild, Member, @@ -39,12 +39,12 @@ import { 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"; +} from "../../types/types.ts"; +import { formatImageURL } from "../../util/cdn.ts"; +import { Collection } from "../../util/collection.ts"; +import { endpoints } from "../../util/constants.ts"; +import { botHasPermission, calculateBits } from "../../util/permissions.ts"; +import { urlToBase64 } from "../../util/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) { diff --git a/src/handlers/member.ts b/src/api/handlers/member.ts similarity index 95% rename from src/handlers/member.ts rename to src/api/handlers/member.ts index 52911e28d..7abb1840b 100644 --- a/src/handlers/member.ts +++ b/src/api/handlers/member.ts @@ -1,6 +1,6 @@ import { cacheHandlers } from "../controllers/cache.ts"; -import { botID } from "../module/client.ts"; -import { RequestManager } from "../module/requestManager.ts"; +import { botID } from "../../bot.ts"; +import { RequestManager } from "../../rest/mod.ts"; import { Member, structures } from "../structures/structures.ts"; import { DMChannelCreatePayload, @@ -9,15 +9,15 @@ import { ImageFormats, ImageSize, MessageContent, -} from "../types/types.ts"; -import { formatImageURL } from "../utils/cdn.ts"; -import { endpoints } from "../utils/constants.ts"; +} from "../../types/types.ts"; +import { formatImageURL } from "../../util/cdn.ts"; +import { endpoints } from "../../util/constants.ts"; import { botHasPermission, higherRolePosition, highestRole, -} from "../utils/permissions.ts"; -import { urlToBase64 } from "../utils/utils.ts"; +} from "../../util/permissions.ts"; +import { urlToBase64 } from "../../util/utils.ts"; import { sendMessage } from "./channel.ts"; /** The users custom avatar or the default avatar if you don't have a member object. */ diff --git a/src/handlers/message.ts b/src/api/handlers/message.ts similarity index 96% rename from src/handlers/message.ts rename to src/api/handlers/message.ts index d8a26a0af..855266069 100644 --- a/src/handlers/message.ts +++ b/src/api/handlers/message.ts @@ -1,16 +1,16 @@ -import { delay } from "../../deps.ts"; import { cacheHandlers } from "../controllers/cache.ts"; -import { botID } from "../module/client.ts"; -import { RequestManager } from "../module/requestManager.ts"; +import { botID } from "../../bot.ts"; +import { RequestManager } from "../../rest/mod.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"; +} from "../../types/types.ts"; +import { endpoints } from "../../util/constants.ts"; +import { botHasChannelPermissions } from "../../util/permissions.ts"; +import { delay } from "../../util/utils.ts"; /** Delete a message with the channel id and message id only. */ export async function deleteMessageByID( diff --git a/src/handlers/mod.ts b/src/api/handlers/mod.ts similarity index 100% rename from src/handlers/mod.ts rename to src/api/handlers/mod.ts diff --git a/src/handlers/webhook.ts b/src/api/handlers/webhook.ts similarity index 94% rename from src/handlers/webhook.ts rename to src/api/handlers/webhook.ts index a8f58f63d..ed9fe9583 100644 --- a/src/handlers/webhook.ts +++ b/src/api/handlers/webhook.ts @@ -1,4 +1,4 @@ -import { RequestManager } from "../module/requestManager.ts"; +import { RequestManager } from "../../rest/mod.ts"; import { structures } from "../structures/structures.ts"; import { EditWebhookMessageOptions, @@ -7,10 +7,10 @@ import { 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"; +} from "../../types/types.ts"; +import { endpoints } from "../../util/constants.ts"; +import { botHasChannelPermissions } from "../../util/permissions.ts"; +import { urlToBase64 } from "../../util/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: * diff --git a/src/structures/channel.ts b/src/api/structures/channel.ts similarity index 94% rename from src/structures/channel.ts rename to src/api/structures/channel.ts index fa3029636..f9742faca 100644 --- a/src/structures/channel.ts +++ b/src/api/structures/channel.ts @@ -1,5 +1,5 @@ import { cacheHandlers } from "../controllers/cache.ts"; -import { ChannelCreatePayload, Unpromise } from "../types/types.ts"; +import { ChannelCreatePayload, Unpromise } from "../../types/types.ts"; export async function createChannel( data: ChannelCreatePayload, diff --git a/src/structures/guild.ts b/src/api/structures/guild.ts similarity index 97% rename from src/structures/guild.ts rename to src/api/structures/guild.ts index f0c9ab4aa..88d89dccf 100644 --- a/src/structures/guild.ts +++ b/src/api/structures/guild.ts @@ -2,8 +2,8 @@ import { CreateGuildPayload, MemberCreatePayload, Unpromise, -} from "../types/types.ts"; -import { Collection } from "../utils/collection.ts"; +} from "../../types/types.ts"; +import { Collection } from "../../util/collection.ts"; import { structures } from "./mod.ts"; export const initialMemberLoadQueue = new Map(); diff --git a/src/structures/member.ts b/src/api/structures/member.ts similarity index 90% rename from src/structures/member.ts rename to src/api/structures/member.ts index a85ae2c37..7303950a9 100644 --- a/src/structures/member.ts +++ b/src/api/structures/member.ts @@ -1,6 +1,10 @@ import { cacheHandlers } from "../controllers/cache.ts"; -import { GuildMember, MemberCreatePayload, Unpromise } from "../types/types.ts"; -import { Collection } from "../utils/collection.ts"; +import { + GuildMember, + MemberCreatePayload, + Unpromise, +} from "../../types/types.ts"; +import { Collection } from "../../util/collection.ts"; export async function createMember(data: MemberCreatePayload, guildID: string) { const { diff --git a/src/structures/message.ts b/src/api/structures/message.ts similarity index 93% rename from src/structures/message.ts rename to src/api/structures/message.ts index 193ed9304..b63977e67 100644 --- a/src/structures/message.ts +++ b/src/api/structures/message.ts @@ -1,4 +1,4 @@ -import { MessageCreateOptions, Unpromise } from "../types/types.ts"; +import { MessageCreateOptions, Unpromise } from "../../types/types.ts"; export async function createMessage(data: MessageCreateOptions) { const { diff --git a/src/structures/mod.ts b/src/api/structures/mod.ts similarity index 100% rename from src/structures/mod.ts rename to src/api/structures/mod.ts diff --git a/src/structures/role.ts b/src/api/structures/role.ts similarity index 88% rename from src/structures/role.ts rename to src/api/structures/role.ts index 51ace0370..ae1e1db50 100644 --- a/src/structures/role.ts +++ b/src/api/structures/role.ts @@ -1,4 +1,4 @@ -import { RoleData, Unpromise } from "../types/types.ts"; +import { RoleData, Unpromise } from "../../types/types.ts"; export async function createRole(data: RoleData) { const { tags, ...rest } = data; diff --git a/src/structures/structures.ts b/src/api/structures/structures.ts similarity index 100% rename from src/structures/structures.ts rename to src/api/structures/structures.ts diff --git a/src/structures/template.ts b/src/api/structures/template.ts similarity index 91% rename from src/structures/template.ts rename to src/api/structures/template.ts index bb199a20b..248af8861 100644 --- a/src/structures/template.ts +++ b/src/api/structures/template.ts @@ -1,4 +1,4 @@ -import { GuildTemplate } from "../types/types.ts"; +import { GuildTemplate } from "../../types/types.ts"; export function createTemplate( data: GuildTemplate, diff --git a/src/module/client.ts b/src/bot.ts similarity index 94% rename from src/module/client.ts rename to src/bot.ts index 66b4bd5c3..7472ffdef 100644 --- a/src/module/client.ts +++ b/src/bot.ts @@ -2,10 +2,10 @@ import { ClientOptions, DiscordBotGatewayData, EventHandlers, -} from "../types/types.ts"; -import { baseEndpoints, endpoints } from "../utils/constants.ts"; -import { RequestManager } from "./requestManager.ts"; -import { spawnShards } from "./shardingManager.ts"; +} from "./types/types.ts"; +import { baseEndpoints, endpoints } from "./util/constants.ts"; +import { RequestManager } from "./rest/mod.ts"; +import { spawnShards } from "./ws/shard_manager.ts"; export let authorization = ""; export let botID = ""; diff --git a/src/module/requestManager.ts b/src/rest/mod.ts similarity index 98% rename from src/module/requestManager.ts rename to src/rest/mod.ts index a1bdd4d7f..e722f9a9b 100644 --- a/src/module/requestManager.ts +++ b/src/rest/mod.ts @@ -1,7 +1,7 @@ -import { delay } from "../../deps.ts"; import { Errors, HttpResponseCode, RequestMethods } from "../types/types.ts"; -import { baseEndpoints, discordAPIURLS } from "../utils/constants.ts"; -import { authorization, eventHandlers } from "./client.ts"; +import { baseEndpoints, discordAPIURLS } from "../util/constants.ts"; +import { delay } from "../util/utils.ts"; +import { authorization, eventHandlers } from "../bot.ts"; const pathQueues: { [key: string]: QueuedRequest[] } = {}; const ratelimitedPaths = new Map(); diff --git a/src/types/guild.ts b/src/types/guild.ts index 88b6b8a09..f99709d39 100644 --- a/src/types/guild.ts +++ b/src/types/guild.ts @@ -1,4 +1,4 @@ -import { Guild } from "../structures/structures.ts"; +import { Guild } from "../api/structures/structures.ts"; import { ChannelCreatePayload, ChannelTypes } from "./channel.ts"; import { Emoji, StatusType } from "./discord.ts"; import { MemberCreatePayload } from "./member.ts"; diff --git a/src/types/message.ts b/src/types/message.ts index ebedb18bb..c27766ee2 100644 --- a/src/types/message.ts +++ b/src/types/message.ts @@ -1,4 +1,4 @@ -import { Channel } from "../structures/structures.ts"; +import { Channel } from "../api/structures/structures.ts"; import { ChannelType } from "./channel.ts"; import { UserPayload } from "./guild.ts"; import { MemberCreatePayload } from "./member.ts"; diff --git a/src/types/options.ts b/src/types/options.ts index 52b199aa2..de93ed971 100644 --- a/src/types/options.ts +++ b/src/types/options.ts @@ -4,7 +4,7 @@ import { Member, Message, Role, -} from "../structures/structures.ts"; +} from "../api/structures/structures.ts"; import { DiscordPayload, Emoji, diff --git a/src/utils/cache.ts b/src/util/cache.ts similarity index 89% rename from src/utils/cache.ts rename to src/util/cache.ts index 6126b1f69..eef2973bb 100644 --- a/src/utils/cache.ts +++ b/src/util/cache.ts @@ -1,4 +1,9 @@ -import { Channel, Guild, Member, Message } from "../structures/structures.ts"; +import { + Channel, + Guild, + Member, + Message, +} from "../api/structures/structures.ts"; import { PresenceUpdatePayload } from "../types/types.ts"; import { Collection } from "./collection.ts"; diff --git a/src/utils/cdn.ts b/src/util/cdn.ts similarity index 100% rename from src/utils/cdn.ts rename to src/util/cdn.ts diff --git a/src/utils/collection.ts b/src/util/collection.ts similarity index 100% rename from src/utils/collection.ts rename to src/util/collection.ts diff --git a/src/utils/constants.ts b/src/util/constants.ts similarity index 100% rename from src/utils/constants.ts rename to src/util/constants.ts diff --git a/src/utils/permissions.ts b/src/util/permissions.ts similarity index 98% rename from src/utils/permissions.ts rename to src/util/permissions.ts index 5c968fb67..919464584 100644 --- a/src/utils/permissions.ts +++ b/src/util/permissions.ts @@ -1,6 +1,6 @@ -import { cacheHandlers } from "../controllers/cache.ts"; -import { botID } from "../module/client.ts"; -import { Guild, Role } from "../structures/structures.ts"; +import { cacheHandlers } from "../api/controllers/cache.ts"; +import { botID } from "../bot.ts"; +import { Guild, Role } from "../api/structures/structures.ts"; import { Permission, Permissions, RawOverwrite } from "../types/types.ts"; /** Checks if the member has this permission. If the member is an owner or has admin perms it will always be true. */ diff --git a/src/utils/utils.ts b/src/util/utils.ts similarity index 80% rename from src/utils/utils.ts rename to src/util/utils.ts index bacaf4c6c..092e21af1 100644 --- a/src/utils/utils.ts +++ b/src/util/utils.ts @@ -1,6 +1,6 @@ -import { encode } from "../../deps.ts"; -import { sendGatewayCommand } from "../module/shardingManager.ts"; +import { sendGatewayCommand } from "../ws/shard_manager.ts"; import { ActivityType, StatusType } from "../types/types.ts"; +import { encode } from "../../deps.ts"; export const sleep = (timeout: number) => { return new Promise((resolve) => setTimeout(resolve, timeout)); @@ -32,3 +32,11 @@ export async function urlToBase64(url: string) { const type = url.substring(url.lastIndexOf(".") + 1); return `data:image/${type};base64,${imageStr}`; } + +export function delay(ms: number): Promise { + return new Promise((res): number => + setTimeout((): void => { + res(); + }, ms) + ); +} diff --git a/src/ws/deps.ts b/src/ws/deps.ts new file mode 100644 index 000000000..7a67c7808 --- /dev/null +++ b/src/ws/deps.ts @@ -0,0 +1 @@ +export { decompress_with as decompressWith } from "https://unpkg.com/@evan/wasm@0.0.22/target/zlib/deno.js"; diff --git a/src/module/shard.ts b/src/ws/mod.ts similarity index 97% rename from src/module/shard.ts rename to src/ws/mod.ts index 7f3f25b79..035b198b9 100644 --- a/src/module/shard.ts +++ b/src/ws/mod.ts @@ -1,5 +1,4 @@ -import { delay, inflate } from "../../deps.ts"; -import { eventHandlers } from "../../mod.ts"; +import { botGatewayData, eventHandlers } from "../bot.ts"; import { DiscordBotGatewayData, DiscordHeartbeatPayload, @@ -7,9 +6,10 @@ import { GatewayOpcode, ReadyPayload, } from "../types/types.ts"; -import { BotStatusRequest } from "../utils/utils.ts"; -import { botGatewayData, IdentifyPayload, proxyWSURL } from "./client.ts"; -import { handleDiscordPayload } from "./shardingManager.ts"; +import { BotStatusRequest, delay } from "../util/utils.ts"; +import { IdentifyPayload, proxyWSURL } from "../bot.ts"; +import { handleDiscordPayload } from "./shard_manager.ts"; +import { decompressWith } from "./deps.ts"; const basicShards = new Map(); const heartbeating = new Map(); @@ -73,7 +73,7 @@ export async function createShard( } if (message instanceof Uint8Array) { - message = inflate( + message = decompressWith( message, 0, (slice: Uint8Array) => utf8decoder.decode(slice), diff --git a/src/module/shardingManager.ts b/src/ws/shard_manager.ts similarity index 88% rename from src/module/shardingManager.ts rename to src/ws/shard_manager.ts index 2c456cfb5..8236d561a 100644 --- a/src/module/shardingManager.ts +++ b/src/ws/shard_manager.ts @@ -1,20 +1,19 @@ -import { delay } from "../../deps.ts"; -import { controllers } from "../controllers/mod.ts"; -import { Guild } from "../structures/structures.ts"; +import { controllers } from "../api/controllers/mod.ts"; +import { Guild } from "../api/structures/structures.ts"; import { DiscordBotGatewayData, DiscordPayload, FetchMembersOptions, GatewayOpcode, } from "../types/types.ts"; -import { cache } from "../utils/cache.ts"; -import { BotStatusRequest } from "../utils/utils.ts"; +import { cache } from "../util/cache.ts"; +import { BotStatusRequest, delay } from "../util/utils.ts"; import { botGatewayStatusRequest, createShard, requestGuildMembers, -} from "./shard.ts"; -import { eventHandlers, IdentifyPayload } from "./client.ts"; +} from "./mod.ts"; +import { eventHandlers, IdentifyPayload } from "../bot.ts"; let createNextShard = true; diff --git a/tests/deps.ts b/tests/deps.ts new file mode 100644 index 000000000..db792fa2c --- /dev/null +++ b/tests/deps.ts @@ -0,0 +1,5 @@ +export { + assert, + assertArrayIncludes, + assertEquals, +} from "https://deno.land/std@0.81.0/testing/asserts.ts";