Merge pull request #15 from yky4589/main
Editting Base and User class, Embed etc
This commit is contained in:
		
						commit
						8f12196370
					
				
					 17 changed files with 109 additions and 202 deletions
				
			
		
							
								
								
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							|  | @ -110,3 +110,5 @@ yarn.lock | |||
| # PRIVACY XDDDD | ||||
| src/test/config.ts | ||||
| .vscode | ||||
| 
 | ||||
| src/test/ | ||||
|  | @ -1,38 +1,65 @@ | |||
| import { Client } from '../models/client.ts' | ||||
| import * as cache from '../models/cache.ts' | ||||
| import endpoint from '../types/endpoint.ts' | ||||
| 
 | ||||
| interface IInit { | ||||
|   useCache?: boolean | ||||
|   cacheName: string | ||||
|   endpoint: string, | ||||
|   restURLfuncArgs: string[] | ||||
| } | ||||
| 
 | ||||
| export class Base { | ||||
|   client: Client | ||||
|   static useCache = true | ||||
|   static cacheName: string | ||||
|   static cacheArgIndex = 0 | ||||
|   static restFunc: (...restArgs: string[]) => string | ||||
|   static useCache?: boolean = true | ||||
|   static restFunc?: ((...restURLfuncArgs: string[]) => string) | ||||
| 
 | ||||
|   constructor (client: Client, _data?: any) { | ||||
|     this.client = client | ||||
|   } | ||||
| 
 | ||||
|   static async autoInit (client: Client, ...restURLfuncArgs: string[]) { | ||||
|   static async autoInit (client: Client, init: IInit) { | ||||
|     this.useCache = init.useCache; | ||||
|     const cacheID = init.restURLfuncArgs.join(':') | ||||
|     if (this.useCache) { | ||||
|       const cached = cache.get( | ||||
|         this.cacheName, | ||||
|         restURLfuncArgs[this.cacheArgIndex] | ||||
|         init.cacheName, | ||||
|         cacheID | ||||
|       ) | ||||
|       if (cached !== undefined && cached instanceof this) { | ||||
|       if (cached !== undefined) { | ||||
|         return cached | ||||
|       } | ||||
|     } | ||||
|     this.restFunc = endpoint.find(((v) => { | ||||
|       v.name === init.endpoint | ||||
|     })) | ||||
|     // TODO: Make error for this
 | ||||
|     if(this.restFunc) { | ||||
|       const resp = await fetch(this.restFunc(...init.restURLfuncArgs), { | ||||
|         headers: { | ||||
|           Authorization: `Bot ${client.token}` | ||||
|         } | ||||
|       }) | ||||
|       const jsonParsed = await resp.json() | ||||
| 
 | ||||
|     const resp = await fetch(this.restFunc(...restURLfuncArgs), { | ||||
|       headers: { | ||||
|         Authorization: `Bot ${client.token}` | ||||
|       } | ||||
|     }) | ||||
|       cache.set(init.cacheName, cacheID, new this(client, jsonParsed)) | ||||
|    | ||||
|     const jsonParsed = await resp.json() | ||||
|     const initialized = new this(client, jsonParsed) | ||||
|     cache.set(this.cacheName, restURLfuncArgs[this.cacheArgIndex], initialized) | ||||
|       return new this(client, jsonParsed) | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|     return initialized | ||||
|   async refresh (client: Client, init: IInit) { | ||||
|     const restFunc: ((...restURLfuncArgs: string[]) => string) | undefined = endpoint.find(v => v.name === init.endpoint) | ||||
|     // TODO: Make error for this
 | ||||
|     if(restFunc) { | ||||
|       const resp = await fetch(restFunc(...init.restURLfuncArgs), { | ||||
|         headers: { | ||||
|           Authorization: `Bot ${client.token}` | ||||
|         } | ||||
|       }) | ||||
|       const jsonParsed = await resp.json() | ||||
|    | ||||
|       Object.assign(this, jsonParsed) | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -24,7 +24,7 @@ export class Channel extends Base { | |||
|   static cacheArgIndex = 0 | ||||
| 
 | ||||
|   constructor (client: Client, data: ChannelPayload) { | ||||
|     super(client) | ||||
|     super(client, data) | ||||
|     this.type = data.type | ||||
|     this.id = data.id | ||||
|   } | ||||
|  | @ -32,34 +32,4 @@ export class Channel extends Base { | |||
|   get mention () { | ||||
|     return `<#${this.id}>` | ||||
|   } | ||||
| 
 | ||||
|   static async autoInit (client: Client, channelID: string) { | ||||
|     return super.autoInit(client, channelID) | ||||
|   } | ||||
| 
 | ||||
|   static from ( | ||||
|     data: | ||||
|       | GuildChannelCategoryPayload | ||||
|       | GuildNewsChannelPayload | ||||
|       | GuildTextChannelPayload | ||||
|       | GuildVoiceChannelPayload | ||||
|       | DMChannelPayload | ||||
|       | GroupDMChannelPayload, | ||||
|     client: Client | ||||
|   ) { | ||||
|     switch (data.type) { | ||||
|       case ChannelTypes.GUILD_CATEGORY: | ||||
|         return new CategoryChannel(client, data as GuildChannelCategoryPayload) | ||||
|       case ChannelTypes.GUILD_NEWS: | ||||
|         return new NewsChannel(client, data as GuildNewsChannelPayload) | ||||
|       case ChannelTypes.GUILD_TEXT: | ||||
|         return new TextChannel(client, data as GuildTextChannelPayload) | ||||
|       case ChannelTypes.GUILD_VOICE: | ||||
|         return new VoiceChannel(client, data as GuildVoiceChannelPayload) | ||||
|       case ChannelTypes.DM: | ||||
|         return new DMChannel(client, data as DMChannelPayload) | ||||
|       case ChannelTypes.GROUP_DM: | ||||
|         return new GroupDMChannel(client, data as GroupDMChannelPayload) | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -12,7 +12,7 @@ import { | |||
|   EmbedVideo | ||||
| } from '../types/channelTypes.ts' | ||||
| 
 | ||||
| export class Embed extends Base { | ||||
| export class Embed { | ||||
|   title?: string | ||||
|   type?: EmbedTypes | ||||
|   description?: string | ||||
|  | @ -26,21 +26,22 @@ export class Embed extends Base { | |||
|   provider?: EmbedProvider | ||||
|   author?: EmbedAuthor | ||||
|   fields?: EmbedField[] | ||||
|   constructor (client: Client, data: EmbedPayload) { | ||||
|     super(client) | ||||
|     this.title = data.title | ||||
|     this.type = data.type | ||||
|     this.description = data.description | ||||
|     this.url = data.url | ||||
|     this.timestamp = data.timestamp | ||||
|     this.color = data.color | ||||
|     this.footer = data.footer | ||||
|     this.image = data.image | ||||
|     this.thumbnail = data.thumbnail | ||||
|     this.video = data.video | ||||
|     this.provider = data.provider | ||||
|     this.author = data.author | ||||
|     this.fields = data.fields | ||||
|   constructor (client: Client, data?: EmbedPayload) { | ||||
|     if(data) { | ||||
|       this.title = data.title | ||||
|       this.type = data.type | ||||
|       this.description = data.description | ||||
|       this.url = data.url | ||||
|       this.timestamp = data.timestamp | ||||
|       this.color = data.color | ||||
|       this.footer = data.footer | ||||
|       this.image = data.image | ||||
|       this.thumbnail = data.thumbnail | ||||
|       this.video = data.video | ||||
|       this.provider = data.provider | ||||
|       this.author = data.author | ||||
|       this.fields = data.fields | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   toJSON () { | ||||
|  |  | |||
|  | @ -20,7 +20,7 @@ export class Emoji extends Base { | |||
|   } | ||||
| 
 | ||||
|   constructor (client: Client, data: EmojiPayload) { | ||||
|     super(client) | ||||
|     super(client, data) | ||||
|     this.id = data.id | ||||
|     this.name = data.name | ||||
|     this.roles = data.roles | ||||
|  |  | |||
|  | @ -1,8 +1,9 @@ | |||
| import { Client } from '../models/client.ts' | ||||
| import { GroupDMChannelPayload } from '../types/channelTypes.ts' | ||||
| import { Base } from "./base.ts" | ||||
| import { Channel } from './channel.ts' | ||||
| 
 | ||||
| export class GroupDMChannel extends Channel { | ||||
| export class GroupDMChannel extends Channel{ | ||||
|   name: string | ||||
|   icon?: string | ||||
|   ownerID: string | ||||
|  |  | |||
|  | @ -1,7 +1,7 @@ | |||
| import { Client } from '../models/client.ts' | ||||
| import { ChannelPayload } from '../types/channelTypes.ts' | ||||
| import { EmojiPayload } from '../types/emojiTypes.ts' | ||||
| import { GUILD } from '../types/endpoint.ts' | ||||
| 
 | ||||
| import { | ||||
|   GuildFeatures, | ||||
|   GuildPayload, | ||||
|  | @ -61,7 +61,7 @@ export class Guild extends Base { | |||
|   approximatePresenceCount?: number | ||||
| 
 | ||||
|   constructor (client: Client, data: GuildPayload) { | ||||
|     super(client) | ||||
|     super(client, data) | ||||
|     this.id = data.id | ||||
|     this.name = data.name | ||||
|     this.icon = data.icon | ||||
|  | @ -108,79 +108,4 @@ export class Guild extends Base { | |||
|     this.approximatePresenceCount = data.approximate_presence_count | ||||
|   } | ||||
| 
 | ||||
|   static async autoInit (client: Client, guildID: string) { | ||||
|     const cached = cache.get('guild', guildID) | ||||
|     if (cached === undefined || !(cached instanceof Guild)) { | ||||
|       const resp = await fetch(GUILD(guildID), { | ||||
|         headers: { | ||||
|           Authorization: `Bot ${client.token}` | ||||
|         } | ||||
|       }) | ||||
|       const guildParsed: GuildPayload = await resp.json() | ||||
| 
 | ||||
|       const newGuild = new Guild(client, guildParsed) | ||||
|       cache.set('guild', guildID, newGuild) | ||||
|       return newGuild | ||||
|     } else { | ||||
|       return cached | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   async refresh () { | ||||
|     const resp = await fetch(GUILD(this.id), { | ||||
|       headers: { | ||||
|         Authorization: `Bot ${this.client.token}` | ||||
|       } | ||||
|     }) | ||||
| 
 | ||||
|     const guildParsed: GuildPayload = await resp.json() | ||||
|     /*for (const rawKey of Object.keys(guildParsed)) { | ||||
|       const _key: string[] = rawKey.split('_').map((v, i) => i === 0 ? v : v.substr(0, 1).toUpperCase() + v.substr(1)) | ||||
|       const key = _key.join('') | ||||
|       if (this.hasOwnProperty(key)) this[key] // fucking ts
 | ||||
|     }*/ | ||||
|     this.name = guildParsed.name | ||||
|     this.icon = guildParsed.icon | ||||
|     this.iconHash = guildParsed.icon_hash | ||||
|     this.splash = guildParsed.splash | ||||
|     this.discoverySplash = guildParsed.discovery_splash | ||||
|     this.owner = guildParsed.owner | ||||
|     this.ownerID = guildParsed.owner_id | ||||
|     this.permissions = guildParsed.permissions | ||||
|     this.region = guildParsed.region | ||||
|     this.afkTimeout = guildParsed.afk_timeout | ||||
|     this.afkChannelID = guildParsed.afk_channel_id | ||||
|     this.widgetEnabled = guildParsed.widget_enabled | ||||
|     this.widgetChannelID = guildParsed.widget_channel_id | ||||
|     this.verificationLevel = guildParsed.verification_level | ||||
|     this.defaultMessageNotifications = guildParsed.default_message_notifications | ||||
|     this.explicitContentFilter = guildParsed.explicit_content_filter | ||||
|     this.roles = guildParsed.roles | ||||
|     this.emojis = guildParsed.emojis | ||||
|     this.features = guildParsed.features | ||||
|     this.mfaLevel = guildParsed.mfa_level | ||||
|     this.systemChannelID = guildParsed.system_channel_id | ||||
|     this.systemChannelFlags = guildParsed.system_channel_flags | ||||
|     this.rulesChannelID = guildParsed.rules_channel_id | ||||
|     this.joinedAt = guildParsed.joined_at | ||||
|     this.large = guildParsed.large | ||||
|     this.unavailable = guildParsed.unavailable | ||||
|     this.memberCount = guildParsed.member_count | ||||
|     this.voiceStates = guildParsed.voice_states | ||||
|     this.members = guildParsed.members | ||||
|     this.channels = guildParsed.channels | ||||
|     this.presences = guildParsed.presences | ||||
|     this.maxPresences = guildParsed.max_presences | ||||
|     this.maxMembers = guildParsed.max_members | ||||
|     this.vanityURLCode = guildParsed.vanity_url_code | ||||
|     this.description = guildParsed.description | ||||
|     this.banner = guildParsed.banner | ||||
|     this.premiumTier = guildParsed.premium_tier | ||||
|     this.premiumSubscriptionCount = guildParsed.premium_subscription_count | ||||
|     this.preferredLocale = guildParsed.preferred_locale | ||||
|     this.publicUpdatesChannelID = guildParsed.public_updates_channel_id | ||||
|     this.maxVideoChannelUsers = guildParsed.max_video_channel_users | ||||
|     this.approximateNumberCount = guildParsed.approximate_number_count | ||||
|     this.approximatePresenceCount = guildParsed.approximate_presence_count | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -3,7 +3,6 @@ import { GuildChannelPayload, Overwrite } from '../types/channelTypes.ts' | |||
| import { Channel } from './channel.ts' | ||||
| import * as cache from '../models/cache.ts' | ||||
| import { Guild } from './guild.ts' | ||||
| import { GUILD_CHANNEL } from '../types/endpoint.ts' | ||||
| 
 | ||||
| export class GuildChannel extends Channel { | ||||
|   guildID: string | ||||
|  | @ -23,21 +22,4 @@ export class GuildChannel extends Channel { | |||
|     this.parentID = data.parent_id | ||||
|   } | ||||
| 
 | ||||
|   static async autoInit (client: Client, guildID: string) { | ||||
|     const cached = cache.get('guildChannel', guildID) | ||||
|     if (cached === undefined || !(cached instanceof GuildChannel)) { | ||||
|       const resp = await fetch(GUILD_CHANNEL(guildID), { | ||||
|         headers: { | ||||
|           Authorization: `Bot ${client.token}` | ||||
|         } | ||||
|       }) | ||||
|       const guildChannelParsed: GuildChannelPayload = await resp.json() | ||||
| 
 | ||||
|       const newGuild = new GuildChannel(client, guildChannelParsed) | ||||
|       cache.set('guildChannel', guildID, newGuild) | ||||
|       return newGuild | ||||
|     } else { | ||||
|       return cached | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -1,6 +1,7 @@ | |||
| import { Client } from '../models/client.ts' | ||||
| import { Channel } from './channel.ts' | ||||
| import { GuildNewsChannelPayload } from '../types/channelTypes.ts' | ||||
| import { Base } from "./base.ts" | ||||
| 
 | ||||
| export class NewsChannel extends Channel { | ||||
|   constructor (client: Client, data: GuildNewsChannelPayload) { | ||||
|  |  | |||
|  | @ -7,20 +7,18 @@ import { Guild } from './guild.ts' | |||
| import { Role } from './role.ts' | ||||
| import { User } from './user.ts' | ||||
| 
 | ||||
| export class Member extends Base { | ||||
|   user: UserPayload | ||||
| export class Member extends User { | ||||
|   nick?: string | ||||
|   roles: RolePayload[] | ||||
|   roles: Role[] | ||||
|   joinedAt: string | ||||
|   premiumSince?: string | ||||
|   deaf: boolean | ||||
|   mute: boolean | ||||
| 
 | ||||
|   constructor (client: Client, data: MemberPayload) { | ||||
|     super(client) | ||||
|     this.user = data.user | ||||
|     super(client, data.user) | ||||
|     this.nick = data.nick | ||||
|     this.roles = data.roles | ||||
|     this.roles = data.roles.map(v => new Role(client, v)) | ||||
|     this.joinedAt = data.joined_at | ||||
|     this.premiumSince = data.premium_since | ||||
|     this.deaf = data.deaf | ||||
|  |  | |||
|  | @ -13,7 +13,7 @@ import { Client } from '../models/client.ts' | |||
| import { UserPayload } from '../types/userTypes.ts' | ||||
| import { RolePayload } from '../types/roleTypes.ts' | ||||
| 
 | ||||
| class Message extends Base { | ||||
| export class Message extends Base { | ||||
|   id: string | ||||
|   channelID: string | ||||
|   guildID?: string | ||||
|  |  | |||
|  | @ -17,7 +17,7 @@ export class Role extends Base { | |||
|   } | ||||
| 
 | ||||
|   constructor (client: Client, data: RolePayload) { | ||||
|     super(client) | ||||
|     super(client, data) | ||||
|     this.id = data.id | ||||
|     this.name = data.name | ||||
|     this.color = data.color | ||||
|  |  | |||
|  | @ -1,5 +1,6 @@ | |||
| import { Client } from '../models/client.ts' | ||||
| import { TextChannelPayload } from '../types/channelTypes.ts' | ||||
| import { Base } from "./base.ts" | ||||
| import { Channel } from './channel.ts' | ||||
| import { Embed } from './embed.ts' | ||||
| export class TextChannel extends Channel { | ||||
|  |  | |||
|  | @ -2,7 +2,6 @@ import { Client } from '../models/client.ts' | |||
| import { UserPayload } from '../types/userTypes.ts' | ||||
| import { Base } from './base.ts' | ||||
| import * as cache from '../models/cache.ts' | ||||
| import { USER } from '../types/endpoint.ts' | ||||
| 
 | ||||
| export class User extends Base { | ||||
|   id: string | ||||
|  | @ -28,7 +27,7 @@ export class User extends Base { | |||
|   } | ||||
| 
 | ||||
|   constructor (client: Client, data: UserPayload) { | ||||
|     super(client) | ||||
|     super(client, data) | ||||
|     this.id = data.id | ||||
|     this.username = data.username | ||||
|     this.discriminator = data.discriminator | ||||
|  | @ -43,23 +42,4 @@ export class User extends Base { | |||
|     this.premiumType = data.premium_type | ||||
|     this.publicFlags = data.public_flags | ||||
|   } | ||||
| 
 | ||||
|   static async autoInit (client: Client, userID: string) { | ||||
|     // user? users?
 | ||||
|     const cached = cache.get('user', userID) | ||||
|     if (cached === undefined || !(cached instanceof User)) { | ||||
|       const resp = await fetch(USER(userID), { | ||||
|         headers: { | ||||
|           Authorization: `Bot ${client.token}` | ||||
|         } | ||||
|       }) | ||||
|       const userParsed: UserPayload = await resp.json() | ||||
| 
 | ||||
|       const newUser = new User(client, userParsed) | ||||
|       cached.set('user', userID, newUser) | ||||
|       return newUser | ||||
|     } else { | ||||
|       return cached | ||||
|     } | ||||
|   } | ||||
| } | ||||
|  |  | |||
|  | @ -19,7 +19,7 @@ export class VoiceState extends Base { | |||
|   suppress: boolean | ||||
| 
 | ||||
|   constructor (client: Client, data: VoiceStatePayload) { | ||||
|     super(client) | ||||
|     super(client, data) | ||||
|     this.channelID = data.channel_id | ||||
|     this.sessionID = data.session_id | ||||
|     this.userID = data.user_id | ||||
|  |  | |||
|  | @ -2,14 +2,33 @@ import { Client } from '../models/client.ts' | |||
| import { Guild } from '../structures/guild.ts' | ||||
| import { GatewayIntents } from '../types/gatewayTypes.ts' | ||||
| import { TOKEN } from './config.ts' | ||||
| import * as cache from '../models/cache.ts' | ||||
| import { Member } from '../structures/member.ts' | ||||
| 
 | ||||
| const bot = new Client() | ||||
| 
 | ||||
| bot.connect(TOKEN, [GatewayIntents.GUILD_MESSAGES]) | ||||
| bot.connect(TOKEN, [GatewayIntents.GUILD_MEMBERS, GatewayIntents.GUILD_PRESENCES, GatewayIntents.GUILD_MESSAGES]) | ||||
| 
 | ||||
| Guild.autoInit(bot, '').then(a => console.log(a)) | ||||
| 
 | ||||
| setTimeout(async () => { | ||||
|   const result = Guild.autoInit(bot, '') | ||||
|   console.log(result) | ||||
| }, 30000) | ||||
| const member = <Member> await Member.autoInit(bot, { | ||||
|   cacheName: 'member', | ||||
|   endpoint: 'GUILD_MEMBER', | ||||
|   restURLfuncArgs: ['', ''] | ||||
| }) | ||||
| console.log('getted (cached) ' + member.id) | ||||
| setInterval(async () => { | ||||
|   //refreshed check
 | ||||
|   console.log('refreshed check: ' + member.id) | ||||
|   //cached
 | ||||
|   console.log('cache: '+(<Member> cache.get('member', '')).id) | ||||
| }, 10000) | ||||
| 
 | ||||
| setInterval(async() => { | ||||
|   member.refresh(bot, { | ||||
|     cacheName: 'member', | ||||
|     endpoint: 'GUILD_MEMBER', | ||||
|     restURLfuncArgs: ['', ''] | ||||
|   }) | ||||
|   //refreshed
 | ||||
|   console.log('refreshed: ' + member.id) | ||||
| }, 20000) | ||||
|  | @ -139,7 +139,7 @@ const GATEWAY_BOT = () => | |||
| 
 | ||||
| //CDN Endpoints
 | ||||
| const CUSTOM_EMOJI = (emojiID: string) => `${DISCORD_CDN_URL}/emojis/${emojiID}` | ||||
| const GUILD_ICON = (guildID: string, iconID: number) => | ||||
| const GUILD_ICON = (guildID: string, iconID: string) => | ||||
|   `${DISCORD_CDN_URL}/icons/${guildID}/${iconID}` | ||||
| const GUILD_SPLASH = (guildID: string, guildSPLASH: string) => | ||||
|   `${DISCORD_CDN_URL}/splashes/${guildID}/${guildSPLASH}` | ||||
|  | @ -153,7 +153,7 @@ const DEFAULT_USER_AVATAR = (iconID: string) => | |||
|   `${DISCORD_CDN_URL}/embed/avatars/${iconID}` | ||||
| const USER_AVATAR = (userID: string, iconID: string) => | ||||
|   `${DISCORD_CDN_URL}/avatars/${userID}/${iconID}` | ||||
| const APPLICATION_ASSET = (applicationID: string, assetID: number) => | ||||
| const APPLICATION_ASSET = (applicationID: string, assetID: string) => | ||||
|   `${DISCORD_CDN_URL}/app-icons/${applicationID}/${assetID}` | ||||
| const ACHIEVEMENT_ICON = ( | ||||
|   applicationID: string, | ||||
|  | @ -182,7 +182,7 @@ const INVITE = (inviteCODE: string) => | |||
| const VOICE_REGIONS = (guildID: string) => | ||||
|   `${DISCORD_API_URL}/v${DISCORD_API_VERSION}/guilds/${guildID}/regions` | ||||
| 
 | ||||
| export { | ||||
| export default [ | ||||
|   GUILDS, | ||||
|   GUILD, | ||||
|   GUILD_AUDIT_LOGS, | ||||
|  | @ -253,4 +253,4 @@ export { | |||
|   TEMPLATE, | ||||
|   INVITE, | ||||
|   VOICE_REGIONS | ||||
| } | ||||
| ] | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue