service worker
This commit is contained in:
parent
010a48c7f0
commit
5fc58bd901
|
@ -93,8 +93,8 @@ function createSlashOption(
|
||||||
data.choices === undefined
|
data.choices === undefined
|
||||||
? undefined
|
? undefined
|
||||||
: data.choices.map((e) =>
|
: data.choices.map((e) =>
|
||||||
typeof e === 'string' ? { name: e, value: e } : e
|
typeof e === 'string' ? { name: e, value: e } : e
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -138,15 +138,15 @@ export type SlashOptionCallable = (o: typeof SlashOption) => SlashCommandOption
|
||||||
export type SlashBuilderOptionsData =
|
export type SlashBuilderOptionsData =
|
||||||
| Array<SlashCommandOption | SlashOptionCallable>
|
| Array<SlashCommandOption | SlashOptionCallable>
|
||||||
| {
|
| {
|
||||||
[name: string]:
|
[name: string]:
|
||||||
| {
|
| {
|
||||||
description: string
|
description: string
|
||||||
type: SlashCommandOptionType
|
type: SlashCommandOptionType
|
||||||
options?: SlashCommandOption[]
|
options?: SlashCommandOption[]
|
||||||
choices?: SlashCommandChoice[]
|
choices?: SlashCommandChoice[]
|
||||||
}
|
|
||||||
| SlashOptionCallable
|
|
||||||
}
|
}
|
||||||
|
| SlashOptionCallable
|
||||||
|
}
|
||||||
|
|
||||||
function buildOptionsArray(
|
function buildOptionsArray(
|
||||||
options: SlashBuilderOptionsData
|
options: SlashBuilderOptionsData
|
||||||
|
@ -154,10 +154,10 @@ function buildOptionsArray(
|
||||||
return Array.isArray(options)
|
return Array.isArray(options)
|
||||||
? options.map((op) => (typeof op === 'function' ? op(SlashOption) : op))
|
? options.map((op) => (typeof op === 'function' ? op(SlashOption) : op))
|
||||||
: Object.entries(options).map((entry) =>
|
: Object.entries(options).map((entry) =>
|
||||||
typeof entry[1] === 'function'
|
typeof entry[1] === 'function'
|
||||||
? entry[1](SlashOption)
|
? entry[1](SlashOption)
|
||||||
: Object.assign(entry[1], { name: entry[0] })
|
: Object.assign(entry[1], { name: entry[0] })
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Slash Command Builder */
|
/** Slash Command Builder */
|
||||||
|
@ -267,8 +267,8 @@ export class SlashCommandsManager {
|
||||||
guild === undefined
|
guild === undefined
|
||||||
? this.rest.api.applications[this.slash.getID()].commands
|
? this.rest.api.applications[this.slash.getID()].commands
|
||||||
: this.rest.api.applications[this.slash.getID()].guilds[
|
: this.rest.api.applications[this.slash.getID()].guilds[
|
||||||
typeof guild === 'string' ? guild : guild.id
|
typeof guild === 'string' ? guild : guild.id
|
||||||
].commands
|
].commands
|
||||||
|
|
||||||
const payload = await route.post(data)
|
const payload = await route.post(data)
|
||||||
|
|
||||||
|
@ -276,8 +276,8 @@ export class SlashCommandsManager {
|
||||||
typeof guild === 'object'
|
typeof guild === 'object'
|
||||||
? guild
|
? guild
|
||||||
: guild === undefined
|
: guild === undefined
|
||||||
? undefined
|
? undefined
|
||||||
: await this.slash.client?.guilds.get(guild)
|
: await this.slash.client?.guilds.get(guild)
|
||||||
|
|
||||||
const cmd = new SlashCommand(this, payload, _guild)
|
const cmd = new SlashCommand(this, payload, _guild)
|
||||||
cmd._guild =
|
cmd._guild =
|
||||||
|
@ -296,8 +296,8 @@ export class SlashCommandsManager {
|
||||||
guild === undefined
|
guild === undefined
|
||||||
? this.rest.api.applications[this.slash.getID()].commands[id]
|
? this.rest.api.applications[this.slash.getID()].commands[id]
|
||||||
: this.rest.api.applications[this.slash.getID()].guilds[
|
: this.rest.api.applications[this.slash.getID()].guilds[
|
||||||
typeof guild === 'string' ? guild : guild.id
|
typeof guild === 'string' ? guild : guild.id
|
||||||
].commands[id]
|
].commands[id]
|
||||||
|
|
||||||
await route.patch(data)
|
await route.patch(data)
|
||||||
return this
|
return this
|
||||||
|
@ -312,8 +312,8 @@ export class SlashCommandsManager {
|
||||||
guild === undefined
|
guild === undefined
|
||||||
? this.rest.api.applications[this.slash.getID()].commands[id]
|
? this.rest.api.applications[this.slash.getID()].commands[id]
|
||||||
: this.rest.api.applications[this.slash.getID()].guilds[
|
: this.rest.api.applications[this.slash.getID()].guilds[
|
||||||
typeof guild === 'string' ? guild : guild.id
|
typeof guild === 'string' ? guild : guild.id
|
||||||
].commands[id]
|
].commands[id]
|
||||||
|
|
||||||
await route.delete()
|
await route.delete()
|
||||||
return this
|
return this
|
||||||
|
@ -325,8 +325,8 @@ export class SlashCommandsManager {
|
||||||
guild === undefined
|
guild === undefined
|
||||||
? this.rest.api.applications[this.slash.getID()].commands[id]
|
? this.rest.api.applications[this.slash.getID()].commands[id]
|
||||||
: this.rest.api.applications[this.slash.getID()].guilds[
|
: this.rest.api.applications[this.slash.getID()].guilds[
|
||||||
typeof guild === 'string' ? guild : guild.id
|
typeof guild === 'string' ? guild : guild.id
|
||||||
].commands[id]
|
].commands[id]
|
||||||
|
|
||||||
const data = await route.get()
|
const data = await route.get()
|
||||||
|
|
||||||
|
@ -334,8 +334,8 @@ export class SlashCommandsManager {
|
||||||
typeof guild === 'object'
|
typeof guild === 'object'
|
||||||
? guild
|
? guild
|
||||||
: guild === undefined
|
: guild === undefined
|
||||||
? undefined
|
? undefined
|
||||||
: await this.slash.client?.guilds.get(guild)
|
: await this.slash.client?.guilds.get(guild)
|
||||||
|
|
||||||
return new SlashCommand(this, data, _guild)
|
return new SlashCommand(this, data, _guild)
|
||||||
}
|
}
|
||||||
|
@ -349,8 +349,8 @@ export class SlashCommandsManager {
|
||||||
guild === undefined
|
guild === undefined
|
||||||
? this.rest.api.applications[this.slash.getID()].commands
|
? this.rest.api.applications[this.slash.getID()].commands
|
||||||
: this.rest.api.applications[this.slash.getID()].guilds[
|
: this.rest.api.applications[this.slash.getID()].guilds[
|
||||||
typeof guild === 'string' ? guild : guild.id
|
typeof guild === 'string' ? guild : guild.id
|
||||||
].commands
|
].commands
|
||||||
|
|
||||||
await route.put(cmds)
|
await route.put(cmds)
|
||||||
|
|
||||||
|
@ -430,8 +430,8 @@ export class SlashClient {
|
||||||
options.client === undefined
|
options.client === undefined
|
||||||
? options.rest === undefined
|
? options.rest === undefined
|
||||||
? new RESTManager({
|
? new RESTManager({
|
||||||
token: this.token
|
token: this.token
|
||||||
})
|
})
|
||||||
: options.rest
|
: options.rest
|
||||||
: options.client.rest
|
: options.client.rest
|
||||||
|
|
||||||
|
@ -481,20 +481,20 @@ export class SlashClient {
|
||||||
const groupMatched =
|
const groupMatched =
|
||||||
e.group !== undefined && e.parent !== undefined
|
e.group !== undefined && e.parent !== undefined
|
||||||
? i.options
|
? i.options
|
||||||
.find(
|
.find(
|
||||||
(o) =>
|
(o) =>
|
||||||
o.name === e.group &&
|
o.name === e.group &&
|
||||||
o.type === SlashCommandOptionType.SUB_COMMAND_GROUP
|
o.type === SlashCommandOptionType.SUB_COMMAND_GROUP
|
||||||
)
|
)
|
||||||
?.options?.find((o) => o.name === e.name) !== undefined
|
?.options?.find((o) => o.name === e.name) !== undefined
|
||||||
: true
|
: true
|
||||||
const subMatched =
|
const subMatched =
|
||||||
e.group === undefined && e.parent !== undefined
|
e.group === undefined && e.parent !== undefined
|
||||||
? i.options.find(
|
? i.options.find(
|
||||||
(o) =>
|
(o) =>
|
||||||
o.name === e.name &&
|
o.name === e.name &&
|
||||||
o.type === SlashCommandOptionType.SUB_COMMAND
|
o.type === SlashCommandOptionType.SUB_COMMAND
|
||||||
) !== undefined
|
) !== undefined
|
||||||
: true
|
: true
|
||||||
const nameMatched1 = e.name === i.name
|
const nameMatched1 = e.name === i.name
|
||||||
const parentMatched = hasGroupOrParent ? e.parent === i.name : true
|
const parentMatched = hasGroupOrParent ? e.parent === i.name : true
|
||||||
|
@ -549,11 +549,11 @@ export class SlashClient {
|
||||||
async verifyServerRequest(req: {
|
async verifyServerRequest(req: {
|
||||||
headers: Headers
|
headers: Headers
|
||||||
method: string
|
method: string
|
||||||
body: Deno.Reader
|
body: Deno.Reader | Uint8Array,
|
||||||
respond: (options: {
|
respond: (options: {
|
||||||
status?: number
|
status?: number
|
||||||
headers?: Headers
|
headers?: Headers
|
||||||
body?: string | Uint8Array
|
body?: string | Uint8Array | FormData
|
||||||
}) => Promise<void>
|
}) => Promise<void>
|
||||||
}): Promise<false | Interaction> {
|
}): Promise<false | Interaction> {
|
||||||
if (req.method.toLowerCase() !== 'post') return false
|
if (req.method.toLowerCase() !== 'post') return false
|
||||||
|
@ -562,7 +562,7 @@ export class SlashClient {
|
||||||
const timestamp = req.headers.get('x-signature-timestamp')
|
const timestamp = req.headers.get('x-signature-timestamp')
|
||||||
if (signature === null || timestamp === null) return false
|
if (signature === null || timestamp === null) return false
|
||||||
|
|
||||||
const rawbody = await Deno.readAll(req.body)
|
const rawbody = req.body instanceof Uint8Array ? req.body : await Deno.readAll(req.body)
|
||||||
const verify = await this.verifyKey(rawbody, signature, timestamp)
|
const verify = await this.verifyKey(rawbody, signature, timestamp)
|
||||||
if (!verify) return false
|
if (!verify) return false
|
||||||
|
|
||||||
|
@ -584,13 +584,13 @@ export class SlashClient {
|
||||||
channels: {}
|
channels: {}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
res._httpRespond = async (d: InteractionResponsePayload) =>
|
res._httpRespond = async (d: InteractionResponsePayload | FormData) =>
|
||||||
await req.respond({
|
await req.respond({
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: new Headers({
|
headers: new Headers({
|
||||||
'content-type': 'application/json'
|
'content-type': d instanceof FormData ? 'multipart/form-data' : 'application/json'
|
||||||
}),
|
}),
|
||||||
body: JSON.stringify(d)
|
body: d instanceof FormData ? d : JSON.stringify(d)
|
||||||
})
|
})
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
@ -599,6 +599,26 @@ export class SlashClient {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Verify FetchEvent (for Service Worker usage) and return Interaction if valid */
|
||||||
|
async verifyFetchEvent({ request: req, respondWith }: { respondWith: CallableFunction, request: Request }): Promise<false | Interaction> {
|
||||||
|
if (req.bodyUsed === true) throw new Error('Request Body already used')
|
||||||
|
if (req.body === null) return false
|
||||||
|
const body = (await req.body.getReader().read()).value
|
||||||
|
if (body === undefined) return false
|
||||||
|
|
||||||
|
return await this.verifyServerRequest({
|
||||||
|
headers: req.headers,
|
||||||
|
body,
|
||||||
|
method: req.method,
|
||||||
|
respond: async (options) => {
|
||||||
|
await respondWith(new Response(options.body, {
|
||||||
|
headers: options.headers,
|
||||||
|
status: options.status,
|
||||||
|
}))
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
async verifyOpineRequest(req: any): Promise<boolean> {
|
async verifyOpineRequest(req: any): Promise<boolean> {
|
||||||
const signature = req.headers.get('x-signature-ed25519')
|
const signature = req.headers.get('x-signature-ed25519')
|
||||||
const timestamp = req.headers.get('x-signature-timestamp')
|
const timestamp = req.headers.get('x-signature-timestamp')
|
||||||
|
|
Loading…
Reference in New Issue