refactor: discordeno

This commit is contained in:
H01001000
2022-12-05 18:51:54 +08:00
parent f8fcc8229c
commit 35a3c5d91c
49 changed files with 192 additions and 86 deletions

View File

@@ -1,5 +1,3 @@
export * from '@discordeno/types'
export * from '@discordeno/utils'
export * from './client.js'
export * from './handlers/index.js'
export * from './transformers/index.js'

View File

@@ -1,11 +1,11 @@
import { DiscordComponent } from '@discordeno/types'
import { Client } from '../client.js'
import {
ButtonStyles,
DiscordComponent,
MessageComponentTypes,
SelectOption,
TextStyles
} from '../index.js'
} from '@discordeno/types'
import { Client } from '../client.js'
export function transformComponent (
client: Client,

View File

@@ -1,8 +1,5 @@
import {
AllowedMentions,
Client,
DiscordAllowedMentions
} from '../../index.js'
import { AllowedMentions, DiscordAllowedMentions } from '@discordeno/types'
import { Client } from '../../client.js'
export function transformAllowedMentionsToDiscordAllowedMentions (
client: Client,

View File

@@ -22,10 +22,10 @@
"test:test-type": "tsc --project tsconfig.test.json"
},
"dependencies": {
"@discordeno/bot": "18.0.0-alpha.1",
"@discordeno/client": "18.0.0-alpha.1",
"@discordeno/gateway": "18.0.0-alpha.1",
"@discordeno/rest": "18.0.0-alpha.1"
"@discordeno/rest": "18.0.0-alpha.1",
"@discordeno/utils": "18.0.0-alpha.1"
},
"devDependencies": {
"@discordeno/types": "18.0.0-alpha.1",

View File

@@ -0,0 +1,111 @@
import { Client, createClient, CreateClientOptions } from '@discordeno/client'
import {
createGatewayManager,
ShardSocketCloseCodes
} from '@discordeno/gateway'
import { createRestManager, CreateRestManagerOptions } from '@discordeno/rest'
import {
DiscordGatewayPayload,
Errors,
GatewayDispatchEventNames,
GetGatewayBot
} from '@discordeno/types'
import {
baseEndpoints,
CHANNEL_MENTION_REGEX,
CONTEXT_MENU_COMMANDS_NAME_REGEX,
DISCORDENO_VERSION,
DISCORD_SNOWFLAKE_REGEX,
SLASH_COMMANDS_NAME_REGEX,
USER_AGENT
} from '@discordeno/utils'
export function createBot (options: CreateBotOptions): Bot {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
const bot = {
...createClient(options),
botGatewayData: options.botGatewayData,
rest: createRestManager({
token: options.token,
debug: options.events?.debug,
secretKey: options.secretKey ?? undefined
})
} as Bot
bot.gateway = createGatewayManager({
gatewayBot: bot.botGatewayData ?? ({} as any),
gatewayConfig: {
token: options.token,
intents: options.intents
},
debug: bot.events.debug,
handleDiscordPayload:
bot.handleDiscordPayload ??
async function (shard, data: DiscordGatewayPayload) {
// TRIGGER RAW EVENT
bot.events.raw(bot, data, shard.id)
if (!data.t) return
// RUN DISPATCH CHECK
await bot.events.dispatchRequirements(bot, data, shard.id)
bot.handlers[data.t as GatewayDispatchEventNames]?.(
bot,
data,
shard.id
)
}
})
return bot
}
export async function startBot (bot: Bot): Promise<void> {
if (Object.keys(bot.botGatewayData ?? {}).length === 0) {
bot.gateway.gatewayBot = await bot.rest.helpers.getGatewayBot()
bot.gateway.lastShardId = bot.gateway.gatewayBot.shards - 1
bot.gateway.manager.totalShards = bot.gateway.gatewayBot.shards
}
bot.gateway.spawnShards()
}
export async function stopBot (bot: Bot): Promise<Bot> {
await bot.gateway.stop(
ShardSocketCloseCodes.Shutdown,
'User requested bot stop'
)
return bot
}
export interface CreateBotOptions extends CreateClientOptions {
botGatewayData?: GetGatewayBot
rest?: Omit<CreateRestManagerOptions, 'token'>
}
export interface Bot extends Client {
botGatewayData?: GetGatewayBot
rest: ReturnType<typeof createRestManager>
gateway: ReturnType<typeof createGatewayManager>
}
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function createBotConstants () {
return {
DISCORDENO_VERSION,
USER_AGENT,
BASE_URL: baseEndpoints.BASE_URL,
CDN_URL: baseEndpoints.CDN_URL,
regexes: {
SLASH_COMMANDS_NAME_REGEX,
CONTEXT_MENU_COMMANDS_NAME_REGEX,
CHANNEL_MENTION_REGEX,
DISCORD_SNOWFLAKE_REGEX
},
Errors
}
}

View File

@@ -1 +1,5 @@
export * from '@discordeno/bot'
export * as client from '@discordeno/client'
export * as gateway from '@discordeno/gateway'
export * as rest from '@discordeno/rest'
export * from '@discordeno/utils'
export * from './bot.js'

View File

@@ -1,6 +1,3 @@
import { calculateShardId } from './utils/index.js'
export * from '@discordeno/types'
export * from '@discordeno/utils'
export * from './manager/index.js'
export * from './shard/index.js'
export { calculateShardId }
export * from './utils/index.js'

View File

@@ -3,10 +3,10 @@ import {
ChannelTypes,
DiscordChannel,
OverwriteReadable,
SortOrderTypes
SortOrderTypes,
WithReason
} from '@discordeno/types'
import { calculateBits } from '@discordeno/utils'
import { WithReason } from '../../index.js'
import type { RestManager } from '../../restManager.js'
import { Channel } from '../../transformers/channel.js'

View File

@@ -4,10 +4,10 @@ import {
DiscordChannel,
OverwriteReadable,
SortOrderTypes,
VideoQualityModes
VideoQualityModes,
WithReason
} from '@discordeno/types'
import { calculateBits } from '@discordeno/utils'
import { WithReason } from '../../index.js'
import type { RestManager } from '../../restManager.js'
import { Channel } from '../../transformers/channel.js'

View File

@@ -1,6 +1,5 @@
import { OverwriteReadable } from '@discordeno/types'
import { BigString, OverwriteReadable, WithReason } from '@discordeno/types'
import { calculateBits } from '@discordeno/utils'
import { BigString, WithReason } from '../../index.js'
import type { RestManager } from '../../restManager.js'
/**

View File

@@ -1,5 +1,4 @@
import { DiscordStageInstance } from '@discordeno/types'
import { BigString, WithReason } from '../../../index.js'
import { BigString, DiscordStageInstance, WithReason } from '@discordeno/types'
import type { RestManager } from '../../../restManager.js'
import { StageInstance } from '../../../transformers/stageInstance.js'

View File

@@ -1,5 +1,4 @@
import { DiscordStageInstance } from '@discordeno/types'
import { BigString, WithReason } from '../../../index.js'
import { BigString, DiscordStageInstance, WithReason } from '@discordeno/types'
import type { RestManager } from '../../../restManager.js'
import { StageInstance } from '../../../transformers/stageInstance.js'

View File

@@ -1,5 +1,4 @@
import { DiscordChannel } from '@discordeno/types'
import { BigString, WithReason } from '../../../index.js'
import { BigString, DiscordChannel, WithReason } from '@discordeno/types'
import type { RestManager } from '../../../restManager.js'
import { Channel } from '../../../transformers/channel.js'

View File

@@ -1,5 +1,9 @@
import { BigString, ChannelTypes, DiscordChannel } from '@discordeno/types'
import { WithReason } from '../../../index.js'
import {
BigString,
ChannelTypes,
DiscordChannel,
WithReason
} from '@discordeno/types'
import type { RestManager } from '../../../restManager.js'
import { Channel } from '../../../transformers/channel.js'

View File

@@ -1,6 +1,5 @@
import { DiscordEmoji } from '@discordeno/types'
import { BigString, DiscordEmoji, WithReason } from '@discordeno/types'
import { urlToBase64 } from '@discordeno/utils'
import { BigString, WithReason } from '../../index.js'
import type { RestManager } from '../../restManager.js'
import { Emoji } from '../../transformers/emoji.js'

View File

@@ -1,5 +1,4 @@
import { DiscordEmoji } from '@discordeno/types'
import { BigString, WithReason } from '../../index.js'
import { BigString, DiscordEmoji, WithReason } from '@discordeno/types'
import type { RestManager } from '../../restManager.js'
import { Emoji } from '../../transformers/emoji.js'

View File

@@ -1,5 +1,5 @@
import { BigString } from '@discordeno/types'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
/**
* Builds a URL to an emoji in the Discord CDN.

View File

@@ -2,12 +2,13 @@ import {
AutoModerationActionType,
AutoModerationEventTypes,
AutoModerationTriggerTypes,
BigString,
DiscordAutoModerationRule,
DiscordAutoModerationRuleTriggerMetadataPresets,
DiscordCreateAutomoderationRule
DiscordCreateAutomoderationRule,
WithReason
} from '@discordeno/types'
import { BigString, WithReason } from '../../../index.js'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { AutoModerationRule } from '../../../transformers/automodRule.js'
/**

View File

@@ -1,5 +1,5 @@
import { BigString } from '@discordeno/types'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
/**
* Deletes an automod rule.

View File

@@ -1,11 +1,12 @@
import {
AutoModerationActionType,
AutoModerationEventTypes,
BigString,
DiscordAutoModerationRule,
DiscordAutoModerationRuleTriggerMetadataPresets
DiscordAutoModerationRuleTriggerMetadataPresets,
WithReason
} from '@discordeno/types'
import { BigString, WithReason } from '../../../index.js'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { AutoModerationRule } from '../../../transformers/automodRule.js'
/**

View File

@@ -1,5 +1,5 @@
import { BigString, DiscordAutoModerationRule } from '@discordeno/types'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { AutoModerationRule } from '../../../transformers/automodRule.js'
/**

View File

@@ -1,6 +1,6 @@
import { BigString, DiscordAutoModerationRule } from '@discordeno/types'
import { Collection } from '@discordeno/utils'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { AutoModerationRule } from '../../../transformers/automodRule.js'
/**

View File

@@ -1,5 +1,5 @@
import type { BigString, MfaLevels } from '@discordeno/types'
import type { RestManager } from '../../index.js'
import type { RestManager } from '../../restManager.js'
/** Modify a guild's MFA level. Requires guild ownership. */
export async function editGuildMfaLevel (

View File

@@ -2,11 +2,11 @@ import {
BigString,
DiscordScheduledEvent,
ScheduledEventEntityType,
ScheduledEventPrivacyLevel
ScheduledEventPrivacyLevel,
WithReason
} from '@discordeno/types'
import { validateLength } from '@discordeno/utils'
import { WithReason } from '../../../index.js'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { ScheduledEvent } from '../../../transformers/scheduledEvent.js'
/**

View File

@@ -1,5 +1,5 @@
import { BigString } from '@discordeno/types'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
/**
* Deletes a scheduled event from a guild.

View File

@@ -3,11 +3,11 @@ import {
DiscordScheduledEvent,
ScheduledEventEntityType,
ScheduledEventPrivacyLevel,
ScheduledEventStatus
ScheduledEventStatus,
WithReason
} from '@discordeno/types'
import { validateLength } from '@discordeno/utils'
import { WithReason } from '../../../index.js'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { ScheduledEvent } from '../../../transformers/scheduledEvent.js'
/**

View File

@@ -1,5 +1,5 @@
import { BigString, DiscordScheduledEvent } from '@discordeno/types'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { ScheduledEvent } from '../../../transformers/scheduledEvent.js'
/**

View File

@@ -1,6 +1,6 @@
import { BigString, DiscordMember, DiscordUser } from '@discordeno/types'
import { Collection } from '@discordeno/utils'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { Member, User } from '../../../transformers/member.js'
// TODO: This endpoint discards certain data from the result.

View File

@@ -1,6 +1,6 @@
import { BigString, DiscordScheduledEvent } from '@discordeno/types'
import { Collection } from '@discordeno/utils'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { ScheduledEvent } from '../../../transformers/scheduledEvent.js'
/**

View File

@@ -1,5 +1,9 @@
import { BigString, DiscordInvite, TargetTypes } from '@discordeno/types'
import { WithReason } from '../../../index.js'
import {
BigString,
DiscordInvite,
TargetTypes,
WithReason
} from '@discordeno/types'
import type { RestManager } from '../../../restManager.js'
import { BaseInvite } from './getInvite.js'

View File

@@ -1,5 +1,5 @@
import { BigString, DiscordApplicationCommand } from '@discordeno/types'
import { RestManager } from '../../../restManager.js'
import type { RestManager } from '../../../restManager.js'
import { ApplicationCommand } from '../../../transformers/applicationCommand.js'
import { CreateApplicationCommand } from '../../../types.js'

View File

@@ -1,4 +1,4 @@
import { BigString, WithReason } from '../../index.js'
import { BigString, WithReason } from '@discordeno/types'
import type { RestManager } from '../../restManager.js'
/**

View File

@@ -1,4 +1,4 @@
import { BigString, DiscordMember, WithReason } from '../../index.js'
import { BigString, DiscordMember, WithReason } from '@discordeno/types'
import type { RestManager } from '../../restManager.js'
import { Member } from '../../transformers/member.js'

View File

@@ -1,5 +1,5 @@
import { BigString } from '@discordeno/types'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
/**
* Kicks a member from a guild.

View File

@@ -2,7 +2,7 @@ import type { DiscordMemberWithUser, SearchMembers } from '@discordeno/types'
import { BigString } from '@discordeno/types'
import { Collection } from '@discordeno/utils'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
import { Member } from '../../transformers/member.js'
/**
@@ -23,7 +23,9 @@ export async function searchMembers (
options?: Omit<SearchMembers, 'query'>
): Promise<Collection<bigint, Member>> {
if (options?.limit) {
if (options.limit < 1) { throw new Error(rest.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_LOW) }
if (options.limit < 1) {
throw new Error(rest.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_LOW)
}
if (options.limit > 1000) {
throw new Error(rest.constants.Errors.MEMBER_SEARCH_LIMIT_TOO_HIGH)
}

View File

@@ -1,6 +1,6 @@
import { DiscordStickerPack } from '@discordeno/types'
import { Collection } from '@discordeno/utils'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
import { StickerPack } from '../../transformers/sticker.js'
/**

View File

@@ -1,6 +1,6 @@
import { BigString, DiscordRole } from '@discordeno/types'
import { Collection } from '@discordeno/utils'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
import { Role } from '../../transformers/role.js'
/**

View File

@@ -1,5 +1,5 @@
import { FileContent, WithReason } from '@discordeno/types'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
import { Sticker } from '../../transformers/sticker.js'
/**

View File

@@ -1,4 +1,4 @@
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Sticker } from '../../transformers/sticker.js'

View File

@@ -1,5 +1,5 @@
import { AtLeastOne, WithReason } from '@discordeno/types'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
import { Sticker } from '../../transformers/sticker.js'
/**

View File

@@ -1,4 +1,4 @@
import { RestManager } from '../../index.js'
import type { RestManager } from '../../restManager.js'
import { Sticker } from '../../transformers/sticker.js'
/**

View File

@@ -1,6 +1,6 @@
import { DiscordSticker } from '@discordeno/types'
import { Collection } from '@discordeno/utils'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
import { Sticker } from '../../transformers/sticker.js'
/**

View File

@@ -1,5 +1,5 @@
import { DiscordSticker } from '@discordeno/types'
import { RestManager } from '../../restManager.js'
import type { RestManager } from '../../restManager.js'
import { Sticker } from '../../transformers/sticker.js'
/**

View File

@@ -1,6 +1,5 @@
import { DiscordWebhook } from '@discordeno/types'
import { BigString, DiscordWebhook, WithReason } from '@discordeno/types'
import { urlToBase64 } from '@discordeno/utils'
import { BigString, WithReason } from '../../index.js'
import type { RestManager } from '../../restManager.js'
import { Webhook } from '../../transformers/webhook.js'

View File

@@ -1,5 +1,4 @@
import { DiscordWebhook } from '@discordeno/types'
import { BigString, WithReason } from '../../index.js'
import { BigString, DiscordWebhook, WithReason } from '@discordeno/types'
import type { RestManager } from '../../restManager.js'
import { Webhook } from '../../transformers/webhook.js'

View File

@@ -1,5 +1,3 @@
export * from '@discordeno/types'
export * from '@discordeno/utils'
export * from './checkRateLimits.js'
export * from './cleanupQueues.js'
export * from './convertRestError.js'

View File

@@ -1,10 +1,10 @@
import { DiscordComponent } from '@discordeno/types'
import {
ButtonStyles,
DiscordComponent,
MessageComponentTypes,
SelectOption,
TextStyles
} from '../index.js'
} from '@discordeno/types'
import type { RestManager } from '../restManager.js'
export function transformComponent (

View File

@@ -1,8 +1,5 @@
import {
AllowedMentions,
DiscordAllowedMentions,
RestManager
} from '../../index.js'
import { AllowedMentions, DiscordAllowedMentions } from '@discordeno/types'
import type { RestManager } from '../../restManager'
export function transformAllowedMentionsToDiscordAllowedMentions (
rest: RestManager,

View File

@@ -1460,11 +1460,11 @@ __metadata:
version: 0.0.0-use.local
resolution: "discordeno@workspace:packages/discordeno"
dependencies:
"@discordeno/bot": 18.0.0-alpha.1
"@discordeno/client": 18.0.0-alpha.1
"@discordeno/gateway": 18.0.0-alpha.1
"@discordeno/rest": 18.0.0-alpha.1
"@discordeno/types": 18.0.0-alpha.1
"@discordeno/utils": 18.0.0-alpha.1
"@swc/cli": ^0.1.57
"@swc/core": ^1.3.21
"@types/chai": ^4