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:
Ayyan
2020-12-22 21:26:27 +04:00
committed by GitHub
parent 6ecff50d2b
commit e49a23687e
40 changed files with 118 additions and 101 deletions
-7
View File
@@ -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";
@@ -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) {
@@ -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"
@@ -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) {
@@ -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(
@@ -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) {
@@ -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) {
@@ -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(
@@ -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) {
@@ -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) {
@@ -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(
@@ -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) {
@@ -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. */
@@ -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(
@@ -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:
*
@@ -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,
@@ -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<string, MemberCreatePayload[]>();
@@ -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 {
@@ -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 {
@@ -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;
@@ -1,4 +1,4 @@
import { GuildTemplate } from "../types/types.ts";
import { GuildTemplate } from "../../types/types.ts";
export function createTemplate(
data: GuildTemplate,
+4 -4
View File
@@ -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 = "";
@@ -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<string, RateLimitedPath>();
+1 -1
View File
@@ -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";
+1 -1
View File
@@ -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";
+1 -1
View File
@@ -4,7 +4,7 @@ import {
Member,
Message,
Role,
} from "../structures/structures.ts";
} from "../api/structures/structures.ts";
import {
DiscordPayload,
Emoji,
+6 -1
View File
@@ -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";
@@ -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. */
+10 -2
View File
@@ -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<void> {
return new Promise((res): number =>
setTimeout((): void => {
res();
}, ms)
);
}
+1
View File
@@ -0,0 +1 @@
export { decompress_with as decompressWith } from "https://unpkg.com/@evan/wasm@0.0.22/target/zlib/deno.js";
+6 -6
View File
@@ -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<number, BasicShard>();
const heartbeating = new Map<number, boolean>();
@@ -73,7 +73,7 @@ export async function createShard(
}
if (message instanceof Uint8Array) {
message = inflate(
message = decompressWith(
message,
0,
(slice: Uint8Array) => utf8decoder.decode(slice),
@@ -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;
+5
View File
@@ -0,0 +1,5 @@
export {
assert,
assertArrayIncludes,
assertEquals,
} from "https://deno.land/std@0.81.0/testing/asserts.ts";