feat: clean up things
This commit is contained in:
		
						commit
						484c6e3c7b
					
				
					 11 changed files with 206 additions and 53 deletions
				
			
		
							
								
								
									
										2
									
								
								LICENSE
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								LICENSE
									
										
									
									
									
								
							|  | @ -1,6 +1,6 @@ | ||||||
| MIT License | MIT License | ||||||
| 
 | 
 | ||||||
| Copyright (c) 2020 Helloyunho | Copyright (c) 2020 Harmony Org | ||||||
| 
 | 
 | ||||||
| Permission is hereby granted, free of charge, to any person obtaining a copy | Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||
| of this software and associated documentation files (the "Software"), to deal | of this software and associated documentation files (the "Software"), to deal | ||||||
|  |  | ||||||
|  | @ -2,7 +2,7 @@ | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
| 
 | 
 | ||||||
| [](https://github.com/RichardLitt/standard-readme) | [](https://github.com/RichardLitt/standard-readme) [](https://discord.gg/WVN2JF2FRv) | ||||||
| 
 | 
 | ||||||
| **An easy to use Discord API Library for Deno.** | **An easy to use Discord API Library for Deno.** | ||||||
| * Lightweight and easy to use. | * Lightweight and easy to use. | ||||||
|  | @ -22,6 +22,7 @@ Note: Library is yet under development and not completely usable. You're still a | ||||||
| 
 | 
 | ||||||
| - [Usage](#usage) | - [Usage](#usage) | ||||||
| - [Docs](#docs) | - [Docs](#docs) | ||||||
|  | - [Discord](#discord) | ||||||
| - [Maintainer](#maintainer) | - [Maintainer](#maintainer) | ||||||
| - [Contributing](#contributing) | - [Contributing](#contributing) | ||||||
| - [License](#license) | - [License](#license) | ||||||
|  | @ -92,6 +93,10 @@ client.connect('super secret token comes here', Intents.All) | ||||||
| 
 | 
 | ||||||
| Not made yet. | Not made yet. | ||||||
| 
 | 
 | ||||||
|  | ## Discord | ||||||
|  | 
 | ||||||
|  | [](https://discord.gg/WVN2JF2FRv) | ||||||
|  | 
 | ||||||
| ## Maintainer | ## Maintainer | ||||||
| 
 | 
 | ||||||
| [@Helloyunho](https://github.com/Helloyunho) | [@Helloyunho](https://github.com/Helloyunho) | ||||||
|  | @ -106,4 +111,4 @@ Small note: If editing the README, please conform to the [standard-readme](https | ||||||
| 
 | 
 | ||||||
| ## License | ## License | ||||||
| 
 | 
 | ||||||
| [MIT © 2020 Helloyunho](LICENSE) | [MIT © 2020 Harmony Org](LICENSE) | ||||||
|  |  | ||||||
							
								
								
									
										3
									
								
								mod.ts
									
										
									
									
									
								
							
							
						
						
									
										3
									
								
								mod.ts
									
										
									
									
									
								
							|  | @ -16,6 +16,8 @@ export * from './src/managers/guilds.ts' | ||||||
| export * from './src/managers/guildChannels.ts' | export * from './src/managers/guildChannels.ts' | ||||||
| export * from './src/managers/guildEmojis.ts' | export * from './src/managers/guildEmojis.ts' | ||||||
| export * from './src/managers/members.ts' | export * from './src/managers/members.ts' | ||||||
|  | export * from './src/managers/messageReactions.ts' | ||||||
|  | export * from './src/managers/reactionUsers.ts' | ||||||
| export * from './src/managers/messages.ts' | export * from './src/managers/messages.ts' | ||||||
| export * from './src/managers/roles.ts' | export * from './src/managers/roles.ts' | ||||||
| export * from './src/managers/users.ts' | export * from './src/managers/users.ts' | ||||||
|  | @ -39,6 +41,7 @@ export * from './src/structures/presence.ts' | ||||||
| export * from './src/structures/role.ts' | export * from './src/structures/role.ts' | ||||||
| export * from './src/structures/snowflake.ts' | export * from './src/structures/snowflake.ts' | ||||||
| export * from './src/structures/textChannel.ts' | export * from './src/structures/textChannel.ts' | ||||||
|  | export * from './src/structures/messageReaction.ts' | ||||||
| export * from './src/structures/user.ts' | export * from './src/structures/user.ts' | ||||||
| export * from './src/structures/webhook.ts' | export * from './src/structures/webhook.ts' | ||||||
| export * from './src/types/application.ts' | export * from './src/types/application.ts' | ||||||
|  |  | ||||||
|  | @ -45,7 +45,10 @@ import { messageReactionRemoveAll } from './messageReactionRemoveAll.ts' | ||||||
| import { messageReactionRemoveEmoji } from './messageReactionRemoveEmoji.ts' | import { messageReactionRemoveEmoji } from './messageReactionRemoveEmoji.ts' | ||||||
| import { guildMembersChunk } from './guildMembersChunk.ts' | import { guildMembersChunk } from './guildMembersChunk.ts' | ||||||
| import { presenceUpdate } from './presenceUpdate.ts' | import { presenceUpdate } from './presenceUpdate.ts' | ||||||
|  | import { inviteCreate } from './inviteCreate.ts' | ||||||
|  | import { inviteDelete } from './inviteDelete.ts' | ||||||
| import { MessageReaction } from '../../structures/messageReaction.ts' | import { MessageReaction } from '../../structures/messageReaction.ts' | ||||||
|  | import { Invite } from '../../structures/invite.ts' | ||||||
| 
 | 
 | ||||||
| export const gatewayHandlers: { | export const gatewayHandlers: { | ||||||
|   [eventCode in GatewayEvents]: GatewayEventHandler | undefined |   [eventCode in GatewayEvents]: GatewayEventHandler | undefined | ||||||
|  | @ -71,8 +74,8 @@ export const gatewayHandlers: { | ||||||
|   GUILD_ROLE_CREATE: guildRoleCreate, |   GUILD_ROLE_CREATE: guildRoleCreate, | ||||||
|   GUILD_ROLE_UPDATE: guildRoleUpdate, |   GUILD_ROLE_UPDATE: guildRoleUpdate, | ||||||
|   GUILD_ROLE_DELETE: guildRoleDelete, |   GUILD_ROLE_DELETE: guildRoleDelete, | ||||||
|   INVITE_CREATE: undefined, |   INVITE_CREATE: inviteCreate, | ||||||
|   INVITE_DELETE: undefined, |   INVITE_DELETE: inviteDelete, | ||||||
|   MESSAGE_CREATE: messageCreate, |   MESSAGE_CREATE: messageCreate, | ||||||
|   MESSAGE_UPDATE: messageUpdate, |   MESSAGE_UPDATE: messageUpdate, | ||||||
|   MESSAGE_DELETE: messageDelete, |   MESSAGE_DELETE: messageDelete, | ||||||
|  | @ -140,6 +143,8 @@ export interface ClientEvents extends EventTypes { | ||||||
|     at: Date, |     at: Date, | ||||||
|     guildData?: TypingStartGuildData |     guildData?: TypingStartGuildData | ||||||
|   ) => void |   ) => void | ||||||
|  |   inviteCreate: (invite: Invite) => void | ||||||
|  |   inviteDelete: (invite: Invite) => void | ||||||
|   userUpdate: (before: User, after: User) => void |   userUpdate: (before: User, after: User) => void | ||||||
|   voiceServerUpdate: (data: VoiceServerUpdateData) => void |   voiceServerUpdate: (data: VoiceServerUpdateData) => void | ||||||
|   voiceStateAdd: (state: VoiceState) => void |   voiceStateAdd: (state: VoiceState) => void | ||||||
|  |  | ||||||
							
								
								
									
										41
									
								
								src/gateway/handlers/inviteCreate.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/gateway/handlers/inviteCreate.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | ||||||
|  | import { Gateway, GatewayEventHandler } from '../index.ts' | ||||||
|  | import { Guild } from '../../structures/guild.ts' | ||||||
|  | import { InviteCreatePayload } from '../../types/gateway.ts' | ||||||
|  | import { ChannelPayload, GuildPayload, InvitePayload } from '../../../mod.ts' | ||||||
|  | import { Invite } from '../../structures/invite.ts' | ||||||
|  | 
 | ||||||
|  | export const inviteCreate: GatewayEventHandler = async ( | ||||||
|  |   gateway: Gateway, | ||||||
|  |   d: InviteCreatePayload | ||||||
|  | ) => { | ||||||
|  |   // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
 | ||||||
|  |   const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id!) | ||||||
|  | 
 | ||||||
|  |   // Weird case, shouldn't happen
 | ||||||
|  |   if (guild === undefined) return | ||||||
|  | 
 | ||||||
|  |   /** | ||||||
|  |    * TODO(DjDeveloperr): Add _get method in BaseChildManager | ||||||
|  |    */ | ||||||
|  |   const cachedChannel = await gateway.client.channels._get(d.channel_id) | ||||||
|  | 
 | ||||||
|  |   // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
 | ||||||
|  |   const cachedGuild: GuildPayload | undefined = | ||||||
|  |     d.guild_id === undefined | ||||||
|  |       ? undefined | ||||||
|  |       : await guild.client.guilds._get(d.guild_id) | ||||||
|  | 
 | ||||||
|  |   const dataConverted: InvitePayload = { | ||||||
|  |     code: d.code, | ||||||
|  |     guild: cachedGuild, | ||||||
|  |     // had to use `as ChannelPayload` because the _get method returned `ChannelPayload | undefined` which errored
 | ||||||
|  |     channel: (cachedChannel as unknown) as ChannelPayload, | ||||||
|  |     inviter: d.inviter, | ||||||
|  |     target_user: d.target_user, | ||||||
|  |     target_user_type: d.target_user_type, | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   await guild.invites.set(d.code, dataConverted) | ||||||
|  |   const invite = await guild.invites.get(d.code) | ||||||
|  |   gateway.client.emit('inviteCreate', (invite as unknown) as Invite) | ||||||
|  | } | ||||||
							
								
								
									
										34
									
								
								src/gateway/handlers/inviteDelete.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								src/gateway/handlers/inviteDelete.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,34 @@ | ||||||
|  | import { Gateway, GatewayEventHandler } from '../index.ts' | ||||||
|  | import { Guild } from '../../structures/guild.ts' | ||||||
|  | import { InviteDeletePayload } from '../../types/gateway.ts' | ||||||
|  | import { PartialInvitePayload } from '../../types/invite.ts' | ||||||
|  | import { Channel } from '../../../mod.ts' | ||||||
|  | 
 | ||||||
|  | export const inviteDelete: GatewayEventHandler = async ( | ||||||
|  |   gateway: Gateway, | ||||||
|  |   d: InviteDeletePayload | ||||||
|  | ) => { | ||||||
|  |   // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
 | ||||||
|  |   const guild: Guild | undefined = await gateway.client.guilds.get(d.guild_id!) | ||||||
|  | 
 | ||||||
|  |   // Weird case, shouldn't happen
 | ||||||
|  |   if (guild === undefined) return | ||||||
|  | 
 | ||||||
|  |   const cachedInvite = await guild.invites.get(d.code) | ||||||
|  |   const cachedChannel = await gateway.client.channels.get(d.channel_id) | ||||||
|  |   // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
 | ||||||
|  |   const cachedGuild = await gateway.client.guilds.get(d.guild_id!) | ||||||
|  | 
 | ||||||
|  |   // TODO(DjDeveloperr): Make it support self-bots and make Guild not always defined
 | ||||||
|  |   if (cachedInvite === undefined) { | ||||||
|  |     const uncachedInvite: PartialInvitePayload = { | ||||||
|  |       guild: (cachedGuild as unknown) as Guild, | ||||||
|  |       channel: (cachedChannel as unknown) as Channel, | ||||||
|  |       code: d.code, | ||||||
|  |     } | ||||||
|  |     return gateway.client.emit('inviteDeleteUncached', uncachedInvite) | ||||||
|  |   } else { | ||||||
|  |     await guild.invites.delete(d.code) | ||||||
|  |     gateway.client.emit('inviteDelete', cachedInvite) | ||||||
|  |   } | ||||||
|  | } | ||||||
							
								
								
									
										41
									
								
								src/managers/invites.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								src/managers/invites.ts
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,41 @@ | ||||||
|  | import { Client } from '../models/client.ts' | ||||||
|  | import { Guild } from '../structures/guild.ts' | ||||||
|  | import { Invite } from '../structures/invite.ts' | ||||||
|  | import { GUILD_INVITES } from '../types/endpoint.ts' | ||||||
|  | import { InvitePayload } from '../types/invite.ts' | ||||||
|  | import { BaseManager } from './base.ts' | ||||||
|  | 
 | ||||||
|  | export class InviteManager extends BaseManager<InvitePayload, Invite> { | ||||||
|  |   guild: Guild | ||||||
|  | 
 | ||||||
|  |   constructor(client: Client, guild: Guild) { | ||||||
|  |     super(client, `invites:${guild.id}`, Invite) | ||||||
|  |     this.guild = guild | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async get(key: string): Promise<Invite | undefined> { | ||||||
|  |     const raw = await this._get(key) | ||||||
|  |     if (raw === undefined) return | ||||||
|  |     return new Invite(this.client, raw) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async fetch(id: string): Promise<Invite | undefined> { | ||||||
|  |     return await new Promise((resolve, reject) => { | ||||||
|  |       this.client.rest | ||||||
|  |         .get(GUILD_INVITES(this.guild.id)) | ||||||
|  |         .then(async (data) => { | ||||||
|  |           this.set(id, data as InvitePayload) | ||||||
|  |           const newInvite = await this.get(data.code) | ||||||
|  |           resolve(newInvite) | ||||||
|  |         }) | ||||||
|  |         .catch((e) => reject(e)) | ||||||
|  |     }) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   async fromPayload(invites: InvitePayload[]): Promise<boolean> { | ||||||
|  |     for (const invite of invites) { | ||||||
|  |       await this.set(invite.code, invite) | ||||||
|  |     } | ||||||
|  |     return true | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | @ -10,6 +10,7 @@ import { | ||||||
| import { PresenceUpdatePayload } from '../types/gateway.ts' | import { PresenceUpdatePayload } from '../types/gateway.ts' | ||||||
| import { Base } from './base.ts' | import { Base } from './base.ts' | ||||||
| import { RolesManager } from '../managers/roles.ts' | import { RolesManager } from '../managers/roles.ts' | ||||||
|  | import { InviteManager } from '../managers/invites.ts' | ||||||
| import { GuildChannelsManager } from '../managers/guildChannels.ts' | import { GuildChannelsManager } from '../managers/guildChannels.ts' | ||||||
| import { MembersManager } from '../managers/members.ts' | import { MembersManager } from '../managers/members.ts' | ||||||
| import { Role } from './role.ts' | import { Role } from './role.ts' | ||||||
|  | @ -132,6 +133,7 @@ export class Guild extends Base { | ||||||
|   explicitContentFilter?: string |   explicitContentFilter?: string | ||||||
|   roles: RolesManager |   roles: RolesManager | ||||||
|   emojis: GuildEmojisManager |   emojis: GuildEmojisManager | ||||||
|  |   invites: InviteManager | ||||||
|   features?: GuildFeatures[] |   features?: GuildFeatures[] | ||||||
|   mfaLevel?: string |   mfaLevel?: string | ||||||
|   applicationID?: string |   applicationID?: string | ||||||
|  | @ -174,6 +176,7 @@ export class Guild extends Base { | ||||||
|     ) |     ) | ||||||
|     this.roles = new RolesManager(this.client, this) |     this.roles = new RolesManager(this.client, this) | ||||||
|     this.emojis = new GuildEmojisManager(this.client, this.client.emojis, this) |     this.emojis = new GuildEmojisManager(this.client, this.client.emojis, this) | ||||||
|  |     this.invites = new InviteManager(this.client, this) | ||||||
| 
 | 
 | ||||||
|     if (!this.unavailable) { |     if (!this.unavailable) { | ||||||
|       this.name = data.name |       this.name = data.name | ||||||
|  |  | ||||||
|  | @ -15,11 +15,11 @@ export class Invite extends Base { | ||||||
|   approximatePresenceCount?: number |   approximatePresenceCount?: number | ||||||
|   approximateMemberCount?: number |   approximateMemberCount?: number | ||||||
| 
 | 
 | ||||||
|   get link (): string { |   get link(): string { | ||||||
|     return `https://discord.gg/${this.code}` |     return `https://discord.gg/${this.code}` | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   constructor (client: Client, data: InvitePayload) { |   constructor(client: Client, data: InvitePayload) { | ||||||
|     super(client) |     super(client) | ||||||
|     this.code = data.code |     this.code = data.code | ||||||
|     this.guild = data.guild |     this.guild = data.guild | ||||||
|  | @ -31,7 +31,7 @@ export class Invite extends Base { | ||||||
|     this.approximatePresenceCount = data.approximate_presence_count |     this.approximatePresenceCount = data.approximate_presence_count | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   protected readFromData (data: InvitePayload): void { |   protected readFromData(data: InvitePayload): void { | ||||||
|     super.readFromData(data) |     super.readFromData(data) | ||||||
|     this.code = data.code ?? this.code |     this.code = data.code ?? this.code | ||||||
|     this.guild = data.guild ?? this.guild |     this.guild = data.guild ?? this.guild | ||||||
|  |  | ||||||
|  | @ -6,6 +6,7 @@ import { | ||||||
|   CommandContext, |   CommandContext, | ||||||
|   Extension, |   Extension, | ||||||
| } from '../../mod.ts' | } from '../../mod.ts' | ||||||
|  | import { Invite } from '../structures/invite.ts' | ||||||
| import { TOKEN } from './config.ts' | import { TOKEN } from './config.ts' | ||||||
| 
 | 
 | ||||||
| const client = new CommandClient({ | const client = new CommandClient({ | ||||||
|  | @ -62,6 +63,19 @@ client.on('webhooksUpdate', (guild, channel) => { | ||||||
|   console.log(`Webhooks Updated in #${channel.name} from ${guild.name}`) |   console.log(`Webhooks Updated in #${channel.name} from ${guild.name}`) | ||||||
| }) | }) | ||||||
| 
 | 
 | ||||||
|  | client.on('commandError', console.error) | ||||||
|  | client.on('inviteCreate', (invite: Invite) => { | ||||||
|  |   console.log(`Invite Create: ${invite.code}`) | ||||||
|  | }) | ||||||
|  | 
 | ||||||
|  | client.on('inviteDelete', (invite: Invite) => { | ||||||
|  |   console.log(`Invite Delete: ${invite.code}`) | ||||||
|  | }) | ||||||
|  | 
 | ||||||
|  | client.on('inviteDeleteUncached', (invite: Invite) => { | ||||||
|  |   console.log(invite) | ||||||
|  | }) | ||||||
|  | 
 | ||||||
| client.on('commandError', console.error) | client.on('commandError', console.error) | ||||||
| 
 | 
 | ||||||
| class ChannelLog extends Extension { | class ChannelLog extends Extension { | ||||||
|  |  | ||||||
|  | @ -1,3 +1,4 @@ | ||||||
|  | import { Channel, Guild } from '../../mod.ts' | ||||||
| import { ChannelPayload } from './channel.ts' | import { ChannelPayload } from './channel.ts' | ||||||
| import { GuildPayload } from './guild.ts' | import { GuildPayload } from './guild.ts' | ||||||
| import { UserPayload } from './user.ts' | import { UserPayload } from './user.ts' | ||||||
|  | @ -12,3 +13,9 @@ export interface InvitePayload { | ||||||
|   approximate_presence_count?: number |   approximate_presence_count?: number | ||||||
|   approximate_member_count?: number |   approximate_member_count?: number | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | export interface PartialInvitePayload { | ||||||
|  |   code: string | ||||||
|  |   channel: Channel | ||||||
|  |   guild?: Guild | ||||||
|  | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue