Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
typicalninja committed Apr 28, 2023
1 parent 5e85f35 commit 4519c97
Show file tree
Hide file tree
Showing 10 changed files with 3,312 additions and 243 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ build
.docusaurus
.cache-loader
test
.cache
# Beta docs, not released or permanent
docs2

.vscode

Expand Down
2 changes: 1 addition & 1 deletion apps/example_cf_worker/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name = "example_cf_worker"
name = "disciWorker"
main = "src/index.ts"
compatibility_date = "2023-04-21"
compatibility_flags = [ "nodejs_compat" ]
3 changes: 3 additions & 0 deletions apps/example_server/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "../../tsconfig.json"
}
1 change: 0 additions & 1 deletion packages/disci/src/structures/BaseInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ export abstract class BaseInteraction implements IBase {
if (RawInteractionData.guild_id && RawInteractionData.member) {
// from a guild
this.member = new Member(this.handler, RawInteractionData.member)

Reflect.defineProperty(this, 'user', {
get: () => {
return this.member?.user
Expand Down
110 changes: 109 additions & 1 deletion packages/disci/src/structures/primitives/Guild.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,112 @@
import { APIGuild, CDNRoutes, GuildFeature, GuildIconFormat, ImageFormat, Routes } from 'discord-api-types/v10'
import type { InteractionHandler } from '../../InteractionHandler'
import type { IBase } from '../Base'
import { PartialUser } from './User'
import { convertSnowflakeToTimeStamp } from '../../utils/helpers'
import { DiscordImageSize, URLS } from '../../utils/constants'
export class PartialGuild implements IBase {
handler!: InteractionHandler
/**
* Id of this guild
*/
id: string
constructor(handler: InteractionHandler, { id }: { id: string }) {
Object.defineProperty(this, 'handler', { value: handler })
this.id = id
}
/**
* Fetch the guild this partial belongs to
* @param opts.witCounts when true, will return approximate member and presence counts for the guild
*/
async fetch({ withCounts }: { withCounts?: boolean} = {}) {
const guild = await this.handler.api.get<APIGuild>(Routes.guild(this.id), {
query: withCounts ? {
'with_counts': 'true'
} : {}
})
return new Guild(this.handler, guild)
}
}

/**
* Represents a discord guild
*/
export default class Guild {}
export default class Guild extends PartialGuild {
/**
* Owner of this Guild as a partial
*/
owner: PartialUser
/**
* Name of this guild
*/
name: string;
/**
* Approximate Member count not always present (use Guild.fetch() with "withCounts" enabled)
*/
approximateMemberCount?: number;
/**
* Approximate Presence count not always present (use Guild.fetch() with "withCounts" enabled)
*/
approximatePresenceCount?: number;
/**
* The description for the guild
*/
description: string | null
/**
* Enabled guild features (animated banner, news, auto moderation, etc).
* @link https://discord.com/developers/docs/resources/guild#guild-object-guild-features
*/
features: GuildFeature[]
/**
* Icon hash for this guild's Icon
* @link https://discord.com/developers/docs/reference#image-formatting
*/
iconHash: string | null
constructor(handler: InteractionHandler, apiData: APIGuild) {
super(handler, { id: apiData.id })
this.owner = new PartialUser(handler, { id: apiData.owner_id })
this.name = apiData.name;
this.description = apiData.description
this.features = apiData.features
this.iconHash = apiData.icon
// present only with "with_counts"
if('approximate_member_count' in apiData) {
this.approximateMemberCount = apiData.approximate_member_count
}

if('approximate_presence_count' in apiData) {
this.approximatePresenceCount = apiData.approximate_presence_count
}
}
/**
* boolean to indicate if this guild is a verified guild or not
*/
get verified() {
return this.features.includes(GuildFeature.Verified);
}
/**
* boolean to indicate if this guild is a partnered guild or not
*/
get partnered() {
return this.features.includes(GuildFeature.Partnered);
}
/**
* TimeStamp of when this guild was created
*/
get createdTimestamp(): number {
return convertSnowflakeToTimeStamp(this.id)
}
/**
* The time this guild was created as a date
*/
get createdAt(): Date {
return new Date(this.createdTimestamp)
}
/**
* iconURL gets the current guild icon.
* @link https://discord.com/developers/docs/reference#image-formatting
*/
iconURL(opts: { size?: DiscordImageSize; format?: GuildIconFormat} = { size: 128 }) {
return this.iconHash && (`${URLS.DiscordCdn}/${CDNRoutes.guildIcon(this.id, this.iconHash, opts.format ?? (this.iconHash.startsWith('a_') ? ImageFormat.JPEG : ImageFormat.JPEG))}`)
}
}
8 changes: 4 additions & 4 deletions packages/disci/src/structures/primitives/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { type APIUser, type Snowflake, Routes } from 'discord-api-types/v10';
import type { InteractionHandler } from '../../InteractionHandler';
import type { IBase } from '../Base';
import { type APIUser, type Snowflake, Routes } from 'discord-api-types/v10'
import type { InteractionHandler } from '../../InteractionHandler'
import type { IBase } from '../Base'

/**
* Partial Class for accessing Discord Api with minimal data
Expand All @@ -17,7 +17,7 @@ export class PartialUser implements IBase {
constructor(handler: InteractionHandler, data: { id: string }) {
// assign the handler
Object.defineProperty(this, 'handler', { value: handler })
this.id = data.id;
this.id = data.id
}
/**
* Fetch the user this partial belongs to
Expand Down
3 changes: 2 additions & 1 deletion packages/disci/src/utils/Factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
APIChatInputApplicationCommandInteraction,
APIMessageComponentInteraction,
APIUserApplicationCommandInteraction,
APIMessageApplicationCommandInteraction,
} from 'discord-api-types/v10'
import type { InteractionHandler } from '../InteractionHandler'
import {
Expand Down Expand Up @@ -56,7 +57,7 @@ export class ApplicationCommandFactory {
case ApplicationCommandType.ChatInput:
return new ChatInputInteraction(handler, APIData as APIChatInputApplicationCommandInteraction)
case ApplicationCommandType.Message:
return new MessageCommandInteraction(handler, APIData)
return new MessageCommandInteraction(handler, APIData as APIMessageApplicationCommandInteraction)
case ApplicationCommandType.User:
return new UserCommandInteraction(handler, APIData as APIUserApplicationCommandInteraction)
default:
Expand Down
6 changes: 6 additions & 0 deletions packages/disci/src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ export enum DiscordVerificationHeaders {
TimeStamp = 'x-signature-timestamp',
}

/**
* @link https://discord.com/developers/docs/reference#image-formatting
*/
export type DiscordImageSize = 16 | 32 | 64 | 128 | 256 | 512 | 1024 | 2048 | 4096;

export const DiscordEpoch = 14200704e5

// Common type for interactions
Expand Down Expand Up @@ -55,6 +60,7 @@ export type TRespondCallback = (interaction: InteractionContext) => IResponse |

export enum URLS {
DiscordApi = 'https://discord.com/api',
DiscordCdn = 'https://cdn.discordapp.com'
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/disci/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ export const isObject = (value: unknown) => value !== null && typeof value === '
* Detect if current runtime is node.js
* from is-node (npm)
*/
export const isNode = !!(typeof process !== 'undefined' && process.versions && process.versions.node)
export const isNode = !!(typeof process !== 'undefined' && process.versions && process.versions.node)
Loading

0 comments on commit 4519c97

Please sign in to comment.