-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dm messages not being emitted (#291)
* fix: dm messages not being emitted * upgrade ed25519
- Loading branch information
1 parent
9ad2a12
commit 95d52cb
Showing
4 changed files
with
55 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,5 @@ export { fetchAuto } from 'https://deno.land/x/[email protected]/mod.ts' | |
export { walk } from 'https://deno.land/[email protected]/fs/walk.ts' | ||
export { join } from 'https://deno.land/[email protected]/path/mod.ts' | ||
export { Mixin } from 'https://esm.sh/[email protected]' | ||
export { verify as edverify } from 'https://unpkg.com/@evan/[email protected]/target/ed25519/deno.js' | ||
export { decode as decodeHex } from 'https://deno.land/[email protected]/encoding/hex.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,17 +9,17 @@ import { | |
InteractionType | ||
} from '../types/interactions.ts' | ||
import { | ||
ApplicationCommandType, | ||
ApplicationCommandOptionType, | ||
ApplicationCommandType, | ||
InteractionApplicationCommandData | ||
} from '../types/applicationCommand.ts' | ||
import type { Client } from '../client/mod.ts' | ||
import { RESTManager } from '../rest/mod.ts' | ||
import { ApplicationCommandsModule } from './commandModule.ts' | ||
import { verify as edverify } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { edverify, decodeHex } from '../../deps.ts' | ||
import { User } from '../structures/user.ts' | ||
import { HarmonyEventEmitter } from '../utils/events.ts' | ||
import { encodeText, decodeText } from '../utils/encoding.ts' | ||
import { decodeText, encodeText } from '../utils/encoding.ts' | ||
import { ApplicationCommandsManager } from './applicationCommand.ts' | ||
import { Application } from '../structures/application.ts' | ||
import { Member } from '../structures/member.ts' | ||
|
@@ -101,8 +101,9 @@ export class InteractionsClient extends HarmonyEventEmitter<InteractionsClientEv | |
super() | ||
let id = options.id | ||
if (options.token !== undefined) id = atob(options.token?.split('.')[0]) | ||
if (id === undefined) | ||
if (id === undefined) { | ||
throw new Error('ID could not be found. Pass at least client or token') | ||
} | ||
this.id = id | ||
|
||
if (options.client !== undefined) { | ||
|
@@ -203,8 +204,9 @@ export class InteractionsClient extends HarmonyEventEmitter<InteractionsClientEv | |
typeof type === 'string' ? ApplicationCommandType[type] : type | ||
} | ||
|
||
if (handle.handler === undefined) | ||
if (handle.handler === undefined) { | ||
throw new Error('Invalid usage. Handler function not provided') | ||
} | ||
|
||
if ( | ||
(handle.type === undefined || | ||
|
@@ -215,8 +217,9 @@ export class InteractionsClient extends HarmonyEventEmitter<InteractionsClientEv | |
handle.group === undefined | ||
) { | ||
const parts = handle.name.split(/ +/).filter((e) => e !== '') | ||
if (parts.length > 3 || parts.length < 1) | ||
if (parts.length > 3 || parts.length < 1) { | ||
throw new Error('Invalid command name') | ||
} | ||
const root = parts.shift() as string | ||
const group = parts.length === 2 ? parts.shift() : undefined | ||
const sub = parts.shift() | ||
|
@@ -359,28 +362,34 @@ export class InteractionsClient extends HarmonyEventEmitter<InteractionsClientEv | |
} | ||
|
||
/** Verify HTTP based Interaction */ | ||
async verifyKey( | ||
verifyKey( | ||
rawBody: string | Uint8Array, | ||
signature: string | Uint8Array, | ||
timestamp: string | Uint8Array | ||
): Promise<boolean> { | ||
if (this.publicKey === undefined) | ||
): boolean { | ||
if (this.publicKey === undefined) { | ||
throw new Error('Public Key is not present') | ||
} | ||
|
||
const fullBody = new Uint8Array([ | ||
...(typeof timestamp === 'string' ? encodeText(timestamp) : timestamp), | ||
...(typeof rawBody === 'string' ? encodeText(rawBody) : rawBody) | ||
]) | ||
|
||
return edverify(signature, fullBody, this.publicKey).catch(() => false) | ||
return edverify( | ||
decodeHex(encodeText(this.publicKey)), | ||
decodeHex( | ||
signature instanceof Uint8Array ? signature : encodeText(signature) | ||
), | ||
fullBody | ||
) | ||
} | ||
|
||
/** | ||
* Verify [Deno Std HTTP Server Request](https://deno.land/std/http/server.ts) and return Interaction. | ||
* | ||
* **Data present in Interaction returned by this method is very different from actual typings | ||
* as there is no real `Client` behind the scenes to cache things.** | ||
* | ||
*/ | ||
async verifyServerRequest(req: { | ||
headers: Headers | ||
|
@@ -596,8 +605,9 @@ export class InteractionsClient extends HarmonyEventEmitter<InteractionsClientEv | |
const timestamp = req.headers.get('x-signature-timestamp') | ||
const contentLength = req.headers.get('content-length') | ||
|
||
if (signature === null || timestamp === null || contentLength === null) | ||
if (signature === null || timestamp === null || contentLength === null) { | ||
return false | ||
} | ||
|
||
const body = new Uint8Array(parseInt(contentLength)) | ||
await req.body.read(body) | ||
|