Editting Base and User class, Embed etc
This commit is contained in:
parent
8cf02d2ba4
commit
55eeb8dd3f
|
@ -3,6 +3,7 @@ import * as cache from '../models/cache.ts'
|
||||||
import endpoint from '../types/endpoint.ts'
|
import endpoint from '../types/endpoint.ts'
|
||||||
|
|
||||||
interface IInit {
|
interface IInit {
|
||||||
|
useCache?: boolean
|
||||||
cacheName: string
|
cacheName: string
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
restURLfuncArgs: string[]
|
restURLfuncArgs: string[]
|
||||||
|
@ -10,18 +11,20 @@ interface IInit {
|
||||||
|
|
||||||
export class Base {
|
export class Base {
|
||||||
client: Client
|
client: Client
|
||||||
static useCache = true
|
static useCache?: boolean = true
|
||||||
static restFunc: ((...restURLfuncArgs: any) => string)[]
|
static restFunc: ((...restURLfuncArgs: any) => string)[]
|
||||||
|
|
||||||
constructor (client: Client) {
|
constructor (client: Client, _data: any) {
|
||||||
this.client = client
|
this.client = client
|
||||||
}
|
}
|
||||||
|
|
||||||
static async autoInit (client: Client, init: IInit) {
|
static async autoInit (client: Client, init: IInit) {
|
||||||
|
this.useCache = init.useCache;
|
||||||
|
const cacheID = init.restURLfuncArgs.join(':')
|
||||||
if (this.useCache) {
|
if (this.useCache) {
|
||||||
const cached = cache.get(
|
const cached = cache.get(
|
||||||
init.cacheName,
|
init.cacheName,
|
||||||
init.restURLfuncArgs[0]
|
cacheID
|
||||||
)
|
)
|
||||||
if (cached !== undefined) {
|
if (cached !== undefined) {
|
||||||
return cached
|
return cached
|
||||||
|
@ -30,7 +33,7 @@ export class Base {
|
||||||
|
|
||||||
this.restFunc = endpoint.filter(v => v.name === init.endpoint)
|
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: {
|
headers: {
|
||||||
Authorization: `Bot ${client.token}`
|
Authorization: `Bot ${client.token}`
|
||||||
}
|
}
|
||||||
|
@ -38,21 +41,21 @@ export class Base {
|
||||||
|
|
||||||
const jsonParsed = await resp.json()
|
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) {
|
async refresh (client: Client, init: IInit) {
|
||||||
this.restFunc = endpoint.filter(v => v.name !== init.endpoint)
|
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: {
|
headers: {
|
||||||
Authorization: `Bot ${client.token}`
|
Authorization: `Bot ${client.token}`
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
const jsonParsed = await resp.json()
|
const jsonParsed = await resp.json()
|
||||||
|
|
||||||
return Object.assign(target, jsonParsed)
|
Object.assign(this, jsonParsed)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ export class Channel extends Base {
|
||||||
static cacheArgIndex = 0
|
static cacheArgIndex = 0
|
||||||
|
|
||||||
constructor (client: Client, data: ChannelPayload) {
|
constructor (client: Client, data: ChannelPayload) {
|
||||||
super(client)
|
super(client, data)
|
||||||
this.type = data.type
|
this.type = data.type
|
||||||
this.id = data.id
|
this.id = data.id
|
||||||
}
|
}
|
||||||
|
@ -32,30 +32,4 @@ export class Channel extends Base {
|
||||||
get mention () {
|
get mention () {
|
||||||
return `<#${this.id}>`
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ import {
|
||||||
EmbedVideo
|
EmbedVideo
|
||||||
} from '../types/channelTypes.ts'
|
} from '../types/channelTypes.ts'
|
||||||
|
|
||||||
export class Embed extends Base {
|
export class Embed {
|
||||||
title?: string
|
title?: string
|
||||||
type?: EmbedTypes
|
type?: EmbedTypes
|
||||||
description?: string
|
description?: string
|
||||||
|
@ -26,21 +26,22 @@ export class Embed extends Base {
|
||||||
provider?: EmbedProvider
|
provider?: EmbedProvider
|
||||||
author?: EmbedAuthor
|
author?: EmbedAuthor
|
||||||
fields?: EmbedField[]
|
fields?: EmbedField[]
|
||||||
constructor (client: Client, data: EmbedPayload) {
|
constructor (client: Client, data?: EmbedPayload) {
|
||||||
super(client)
|
if(data) {
|
||||||
this.title = data.title
|
this.title = data.title
|
||||||
this.type = data.type
|
this.type = data.type
|
||||||
this.description = data.description
|
this.description = data.description
|
||||||
this.url = data.url
|
this.url = data.url
|
||||||
this.timestamp = data.timestamp
|
this.timestamp = data.timestamp
|
||||||
this.color = data.color
|
this.color = data.color
|
||||||
this.footer = data.footer
|
this.footer = data.footer
|
||||||
this.image = data.image
|
this.image = data.image
|
||||||
this.thumbnail = data.thumbnail
|
this.thumbnail = data.thumbnail
|
||||||
this.video = data.video
|
this.video = data.video
|
||||||
this.provider = data.provider
|
this.provider = data.provider
|
||||||
this.author = data.author
|
this.author = data.author
|
||||||
this.fields = data.fields
|
this.fields = data.fields
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
toJSON () {
|
toJSON () {
|
||||||
|
|
|
@ -20,7 +20,7 @@ export class Emoji extends Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor (client: Client, data: EmojiPayload) {
|
constructor (client: Client, data: EmojiPayload) {
|
||||||
super(client)
|
super(client, data)
|
||||||
this.id = data.id
|
this.id = data.id
|
||||||
this.name = data.name
|
this.name = data.name
|
||||||
this.roles = data.roles
|
this.roles = data.roles
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
import { Client } from '../models/client.ts'
|
import { Client } from '../models/client.ts'
|
||||||
import { GroupDMChannelPayload } from '../types/channelTypes.ts'
|
import { GroupDMChannelPayload } from '../types/channelTypes.ts'
|
||||||
|
import { Base } from "./base.ts"
|
||||||
import { Channel } from './channel.ts'
|
import { Channel } from './channel.ts'
|
||||||
|
|
||||||
export class GroupDMChannel extends Channel {
|
export class GroupDMChannel extends Channel{
|
||||||
name: string
|
name: string
|
||||||
icon?: string
|
icon?: string
|
||||||
ownerID: string
|
ownerID: string
|
||||||
|
|
|
@ -61,7 +61,7 @@ export class Guild extends Base {
|
||||||
approximatePresenceCount?: number
|
approximatePresenceCount?: number
|
||||||
|
|
||||||
constructor (client: Client, data: GuildPayload) {
|
constructor (client: Client, data: GuildPayload) {
|
||||||
super(client)
|
super(client, data)
|
||||||
this.id = data.id
|
this.id = data.id
|
||||||
this.name = data.name
|
this.name = data.name
|
||||||
this.icon = data.icon
|
this.icon = data.icon
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { Client } from '../models/client.ts'
|
import { Client } from '../models/client.ts'
|
||||||
import { Channel } from './channel.ts'
|
import { Channel } from './channel.ts'
|
||||||
import { GuildNewsChannelPayload } from '../types/channelTypes.ts'
|
import { GuildNewsChannelPayload } from '../types/channelTypes.ts'
|
||||||
|
import { Base } from "./base.ts"
|
||||||
|
|
||||||
export class NewsChannel extends Channel {
|
export class NewsChannel extends Channel {
|
||||||
constructor (client: Client, data: GuildNewsChannelPayload) {
|
constructor (client: Client, data: GuildNewsChannelPayload) {
|
||||||
|
|
|
@ -7,20 +7,20 @@ import { Guild } from './guild.ts'
|
||||||
import { Role } from './role.ts'
|
import { Role } from './role.ts'
|
||||||
import { User } from './user.ts'
|
import { User } from './user.ts'
|
||||||
|
|
||||||
export class Member extends Base {
|
export class Member extends User {
|
||||||
user: UserPayload
|
user: User
|
||||||
nick?: string
|
nick?: string
|
||||||
roles: RolePayload[]
|
roles: Role[]
|
||||||
joinedAt: string
|
joinedAt: string
|
||||||
premiumSince?: string
|
premiumSince?: string
|
||||||
deaf: boolean
|
deaf: boolean
|
||||||
mute: boolean
|
mute: boolean
|
||||||
|
|
||||||
constructor (client: Client, data: MemberPayload) {
|
constructor (client: Client, data: MemberPayload) {
|
||||||
super(client)
|
super(client, data.user)
|
||||||
this.user = data.user
|
this.user = this
|
||||||
this.nick = data.nick
|
this.nick = data.nick
|
||||||
this.roles = data.roles
|
this.roles = data.roles.map(v => new Role(client, v))
|
||||||
this.joinedAt = data.joined_at
|
this.joinedAt = data.joined_at
|
||||||
this.premiumSince = data.premium_since
|
this.premiumSince = data.premium_since
|
||||||
this.deaf = data.deaf
|
this.deaf = data.deaf
|
||||||
|
|
|
@ -17,7 +17,7 @@ export class Role extends Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor (client: Client, data: RolePayload) {
|
constructor (client: Client, data: RolePayload) {
|
||||||
super(client)
|
super(client, data)
|
||||||
this.id = data.id
|
this.id = data.id
|
||||||
this.name = data.name
|
this.name = data.name
|
||||||
this.color = data.color
|
this.color = data.color
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Client } from '../models/client.ts'
|
import { Client } from '../models/client.ts'
|
||||||
import { TextChannelPayload } from '../types/channelTypes.ts'
|
import { TextChannelPayload } from '../types/channelTypes.ts'
|
||||||
|
import { Base } from "./base.ts"
|
||||||
import { Channel } from './channel.ts'
|
import { Channel } from './channel.ts'
|
||||||
import { Embed } from './embed.ts'
|
import { Embed } from './embed.ts'
|
||||||
export class TextChannel extends Channel {
|
export class TextChannel extends Channel {
|
||||||
|
|
|
@ -27,7 +27,7 @@ export class User extends Base {
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor (client: Client, data: UserPayload) {
|
constructor (client: Client, data: UserPayload) {
|
||||||
super(client)
|
super(client, data)
|
||||||
this.id = data.id
|
this.id = data.id
|
||||||
this.username = data.username
|
this.username = data.username
|
||||||
this.discriminator = data.discriminator
|
this.discriminator = data.discriminator
|
||||||
|
|
|
@ -19,7 +19,7 @@ export class VoiceState extends Base {
|
||||||
suppress: boolean
|
suppress: boolean
|
||||||
|
|
||||||
constructor (client: Client, data: VoiceStatePayload) {
|
constructor (client: Client, data: VoiceStatePayload) {
|
||||||
super(client)
|
super(client, data)
|
||||||
this.channelID = data.channel_id
|
this.channelID = data.channel_id
|
||||||
this.sessionID = data.session_id
|
this.sessionID = data.session_id
|
||||||
this.userID = data.user_id
|
this.userID = data.user_id
|
||||||
|
|
|
@ -2,22 +2,37 @@ import { Client } from '../models/client.ts'
|
||||||
import { Guild } from '../structures/guild.ts'
|
import { Guild } from '../structures/guild.ts'
|
||||||
import { GatewayIntents } from '../types/gatewayTypes.ts'
|
import { GatewayIntents } from '../types/gatewayTypes.ts'
|
||||||
import { TOKEN } from './config.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()
|
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 member = <Member> await Member.autoInit(bot, {
|
||||||
const result = Guild.autoInit(bot, {
|
cacheName: 'member',
|
||||||
cacheName: 'guild',
|
endpoint: 'GUILD_MEMBER',
|
||||||
endpoint: 'GUILD',
|
restURLfuncArgs: ['668753256419426314', '333432936390983680']
|
||||||
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', '668753256419426314:333432936390983680')).id)
|
||||||
|
}, 10000)
|
||||||
|
|
||||||
|
setInterval(async() => {
|
||||||
|
member.refresh(bot, {
|
||||||
|
cacheName: 'member',
|
||||||
|
endpoint: 'GUILD_MEMBER',
|
||||||
|
restURLfuncArgs: ['668753256419426314', '333432936390983680']
|
||||||
})
|
})
|
||||||
console.log(result)
|
//refreshed
|
||||||
}, 30000)
|
console.log('refreshed: ' + member.id)
|
||||||
|
}, 20000)
|
Loading…
Reference in New Issue