Editting Base and User class, Embed etc

This commit is contained in:
yky4589 2020-10-25 15:50:32 +09:00
parent 8cf02d2ba4
commit 55eeb8dd3f
13 changed files with 74 additions and 78 deletions

View file

@ -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)
}
}

View file

@ -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)
}
}
}

View file

@ -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 () {

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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) {

View file

@ -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

View file

@ -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

View file

@ -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 {

View file

@ -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

View file

@ -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

View file

@ -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 = <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: '+(<Member> cache.get('member', '668753256419426314:333432936390983680')).id)
}, 10000)
setInterval(async() => {
member.refresh(bot, {
cacheName: 'member',
endpoint: 'GUILD_MEMBER',
restURLfuncArgs: ['668753256419426314', '333432936390983680']
})
console.log(result)
}, 30000)
//refreshed
console.log('refreshed: ' + member.id)
}, 20000)