diff --git a/src/structures/base.ts b/src/structures/base.ts index b156aa2..a37f3f8 100644 --- a/src/structures/base.ts +++ b/src/structures/base.ts @@ -3,6 +3,7 @@ import * as cache from '../models/cache.ts' import endpoint from '../types/endpoint.ts' interface IInit { + useCache?: boolean cacheName: string endpoint: string, restURLfuncArgs: string[] @@ -10,18 +11,20 @@ interface IInit { export class Base { client: Client - static useCache = true + static useCache?: boolean = true static restFunc: ((...restURLfuncArgs: any) => string)[] - constructor (client: Client) { + constructor (client: Client, _data: any) { this.client = client } static async autoInit (client: Client, init: IInit) { + this.useCache = init.useCache; + const cacheID = init.restURLfuncArgs.join(':') if (this.useCache) { const cached = cache.get( init.cacheName, - init.restURLfuncArgs[0] + cacheID ) if (cached !== undefined) { return cached @@ -30,7 +33,7 @@ export class Base { this.restFunc = endpoint.filter(v => v.name === init.endpoint) - const resp = await fetch(this.restFunc[0](init.restURLfuncArgs), { + const resp = await fetch(this.restFunc[0](init.restURLfuncArgs[0], init.restURLfuncArgs[1], init.restURLfuncArgs[2], init.restURLfuncArgs[3]), { headers: { Authorization: `Bot ${client.token}` } @@ -38,21 +41,21 @@ export class Base { const jsonParsed = await resp.json() - cache.set(init.cacheName, init.restURLfuncArgs[0], jsonParsed) + cache.set(init.cacheName, cacheID, new this(client, jsonParsed)) - return jsonParsed + return new this(client, jsonParsed) } - static async refresh (client: Client, target: any, init: IInit) { - this.restFunc = endpoint.filter(v => v.name !== init.endpoint) + async refresh (client: Client, init: IInit) { + const restFunc: ((...restURLfuncArgs: any) => string)[] = endpoint.filter(v => v.name === init.endpoint) - const resp = await fetch(this.restFunc[0](init.restURLfuncArgs), { + const resp = await fetch(restFunc[0](init.restURLfuncArgs[0], init.restURLfuncArgs[1], init.restURLfuncArgs[3], init.restURLfuncArgs[4]), { headers: { Authorization: `Bot ${client.token}` } }) const jsonParsed = await resp.json() - return Object.assign(target, jsonParsed) + Object.assign(this, jsonParsed) } } diff --git a/src/structures/channel.ts b/src/structures/channel.ts index 407bb6c..2f12522 100644 --- a/src/structures/channel.ts +++ b/src/structures/channel.ts @@ -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,30 +32,4 @@ export class Channel extends Base { get mention () { return `<#${this.id}>` } - - 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) - } - } } diff --git a/src/structures/embed.ts b/src/structures/embed.ts index 8092dcf..8ab5ee7 100644 --- a/src/structures/embed.ts +++ b/src/structures/embed.ts @@ -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 () { diff --git a/src/structures/emoji.ts b/src/structures/emoji.ts index 38949bd..132a77d 100644 --- a/src/structures/emoji.ts +++ b/src/structures/emoji.ts @@ -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 diff --git a/src/structures/groupChannel.ts b/src/structures/groupChannel.ts index 8ffbb5f..245d22c 100644 --- a/src/structures/groupChannel.ts +++ b/src/structures/groupChannel.ts @@ -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 diff --git a/src/structures/guild.ts b/src/structures/guild.ts index 975a309..8bf144e 100644 --- a/src/structures/guild.ts +++ b/src/structures/guild.ts @@ -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 diff --git a/src/structures/guildnewsChannel.ts b/src/structures/guildnewsChannel.ts index 0a67386..7da22db 100644 --- a/src/structures/guildnewsChannel.ts +++ b/src/structures/guildnewsChannel.ts @@ -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) { diff --git a/src/structures/member.ts b/src/structures/member.ts index 6689c1c..5827950 100644 --- a/src/structures/member.ts +++ b/src/structures/member.ts @@ -7,20 +7,20 @@ 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 { + user: 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.user = this 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 diff --git a/src/structures/role.ts b/src/structures/role.ts index 3f9966b..848c233 100644 --- a/src/structures/role.ts +++ b/src/structures/role.ts @@ -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 diff --git a/src/structures/textChannel.ts b/src/structures/textChannel.ts index 7c04967..1bc6873 100644 --- a/src/structures/textChannel.ts +++ b/src/structures/textChannel.ts @@ -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 { diff --git a/src/structures/user.ts b/src/structures/user.ts index 4f69808..4997982 100644 --- a/src/structures/user.ts +++ b/src/structures/user.ts @@ -27,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 diff --git a/src/structures/voicestate.ts b/src/structures/voicestate.ts index 8bdfe23..d19b035 100644 --- a/src/structures/voicestate.ts +++ b/src/structures/voicestate.ts @@ -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 diff --git a/src/test/index.ts b/src/test/index.ts index e2cf681..d958db8 100644 --- a/src/test/index.ts +++ b/src/test/index.ts @@ -2,22 +2,37 @@ 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" +import { User } from "../structures/user.ts" +import endpoint from "../types/endpoint.ts" +import { Base } from "../structures/base.ts" +import { GuildChannel } from "../structures/guildChannel.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, { - cacheName: 'guild', - endpoint: 'GUILD', - restURLfuncArgs: [''] -}).then((a) => console.log(a)) -setTimeout(async () => { - const result = Guild.autoInit(bot, { - cacheName: 'guild', - endpoint: 'GUILD', - restURLfuncArgs: [''] +const member = await Member.autoInit(bot, { + cacheName: 'member', + endpoint: 'GUILD_MEMBER', + restURLfuncArgs: ['668753256419426314', '333432936390983680'] +}) +console.log('getted (cached) ' + member.id) +setInterval(async () => { + //refreshed check + console.log('refreshed check: ' + member.id) + //cached + console.log('cache: '+( cache.get('member', '668753256419426314:333432936390983680')).id) +}, 10000) + +setInterval(async() => { + member.refresh(bot, { + cacheName: 'member', + endpoint: 'GUILD_MEMBER', + restURLfuncArgs: ['668753256419426314', '333432936390983680'] }) - console.log(result) -}, 30000) \ No newline at end of file + //refreshed + console.log('refreshed: ' + member.id) +}, 20000) \ No newline at end of file