-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(APIInteraction): add type-check utilities
BREAKING CHANGE: This commit removes the `guild_id` property from `APIDMInteraction` which allows type-checks to work with the `in` operator. Because of that, we also provide utility functions that help with those type checks. Use them in your code by importing the `Utils` object, or by directly importing them. Check the README for examples
- Loading branch information
1 parent
9e1a73f
commit 3307201
Showing
15 changed files
with
199 additions
and
28 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
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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This file exports all the utility functions available in the recommended API / Gateway version | ||
// Thereby, things MAY break in the future. Try sticking to imports from a specific version | ||
|
||
export * from './v8.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
APIApplicationCommandDMInteraction, | ||
APIApplicationCommandGuildInteraction, | ||
APIApplicationCommandInteraction, | ||
APIDMInteraction, | ||
APIGuildInteraction, | ||
APIInteraction, | ||
} from '../payloads.ts'; | ||
|
||
/** | ||
* A type-guard check for guild interactions. | ||
* @param interaction The interaction to check against the | ||
* @returns A boolean that indicates if the interaction was received from a guild | ||
*/ | ||
export function isGuildInteraction(interaction: APIInteraction): interaction is APIGuildInteraction { | ||
return Reflect.has(interaction, 'guild_id'); | ||
} | ||
|
||
/** | ||
* A type-guard check for DM interactions. | ||
* @param interaction The interaction to check against | ||
* @returns A boolean that indicates if the interaction was received from a direct message | ||
*/ | ||
export function isDMInteraction(interaction: APIInteraction): interaction is APIDMInteraction { | ||
return !isGuildInteraction(interaction); | ||
} | ||
|
||
/** | ||
* A type-guard check for guild application command interactions. | ||
* @param interaction The interaction to check against | ||
* @returns A boolean that indicates if the command interaction was received from a guild | ||
*/ | ||
export function isApplicationCommandGuildInteraction( | ||
interaction: APIApplicationCommandInteraction, | ||
): interaction is APIApplicationCommandGuildInteraction { | ||
return isGuildInteraction(interaction); | ||
} | ||
|
||
/** | ||
* A type-guard check for direct message application command interactions. | ||
* @param interaction The interaction to check against | ||
* @returns A boolean that indicates if the command interaction was received from a direct message | ||
*/ | ||
export function isApplicationCommandDMInteraction( | ||
interaction: APIApplicationCommandInteraction, | ||
): interaction is APIApplicationCommandDMInteraction { | ||
return !isGuildInteraction(interaction); | ||
} |
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
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
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 |
---|---|---|
|
@@ -22,6 +22,7 @@ | |
"rest/**/*.ts", | ||
"rpc/**/*.ts", | ||
"shortcuts/**/*.ts", | ||
"utils/**/*.ts", | ||
"voice/**/*.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// This file exports all the utility functions available in the recommended API / Gateway version | ||
// Thereby, things MAY break in the future. Try sticking to imports from a specific version | ||
|
||
export * from './v8'; |
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 |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
APIApplicationCommandDMInteraction, | ||
APIApplicationCommandGuildInteraction, | ||
APIApplicationCommandInteraction, | ||
APIDMInteraction, | ||
APIGuildInteraction, | ||
APIInteraction, | ||
} from '../payloads'; | ||
|
||
/** | ||
* A type-guard check for guild interactions. | ||
* @param interaction The interaction to check against the | ||
* @returns A boolean that indicates if the interaction was received from a guild | ||
*/ | ||
export function isGuildInteraction(interaction: APIInteraction): interaction is APIGuildInteraction { | ||
return Reflect.has(interaction, 'guild_id'); | ||
} | ||
|
||
/** | ||
* A type-guard check for DM interactions. | ||
* @param interaction The interaction to check against | ||
* @returns A boolean that indicates if the interaction was received from a direct message | ||
*/ | ||
export function isDMInteraction(interaction: APIInteraction): interaction is APIDMInteraction { | ||
return !isGuildInteraction(interaction); | ||
} | ||
|
||
/** | ||
* A type-guard check for guild application command interactions. | ||
* @param interaction The interaction to check against | ||
* @returns A boolean that indicates if the command interaction was received from a guild | ||
*/ | ||
export function isApplicationCommandGuildInteraction( | ||
interaction: APIApplicationCommandInteraction, | ||
): interaction is APIApplicationCommandGuildInteraction { | ||
return isGuildInteraction(interaction); | ||
} | ||
|
||
/** | ||
* A type-guard check for direct message application command interactions. | ||
* @param interaction The interaction to check against | ||
* @returns A boolean that indicates if the command interaction was received from a direct message | ||
*/ | ||
export function isApplicationCommandDMInteraction( | ||
interaction: APIApplicationCommandInteraction, | ||
): interaction is APIApplicationCommandDMInteraction { | ||
return !isGuildInteraction(interaction); | ||
} |