-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
v9.ts
171 lines (152 loc) · 6.02 KB
/
v9.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
APIApplicationCommandDMInteraction,
APIApplicationCommandGuildInteraction,
APIApplicationCommandInteraction,
APIButtonComponent,
APIButtonComponentWithCustomId,
APIButtonComponentWithURL,
APIChatInputApplicationCommandInteraction,
APIContextMenuInteraction,
APIDMInteraction,
APIGuildInteraction,
APIInteraction,
APIMessageComponentButtonInteraction,
APIMessageComponentDMInteraction,
APIMessageComponentGuildInteraction,
APIMessageComponentInteraction,
APIMessageComponentSelectMenuInteraction,
ApplicationCommandType,
ButtonStyle,
ComponentType,
InteractionType,
} from '../payloads/v9/index';
// Interactions
/**
* A type-guard check for DM interactions
* @param interaction The interaction to check against
* @returns A boolean that indicates if the interaction was received in a DM channel
*/
export function isDMInteraction(interaction: APIInteraction): interaction is APIDMInteraction {
return Reflect.has(interaction, 'user');
}
/**
* A type-guard check for guild interactions
* @param interaction The interaction to check against
* @returns A boolean that indicates if the interaction was received in a guild
*/
export function isGuildInteraction(interaction: APIInteraction): interaction is APIGuildInteraction {
return Reflect.has(interaction, 'guild_id');
}
// ApplicationCommandInteractions
/**
* A type-guard check for DM application command interactions
* @param interaction The application command interaction to check against
* @returns A boolean that indicates if the application command interaction was received in a DM channel
*/
export function isApplicationCommandDMInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIApplicationCommandDMInteraction {
return isDMInteraction(interaction);
}
/**
* A type-guard check for guild application command interactions
* @param interaction The interaction to check against
* @returns A boolean that indicates if the application command interaction was received in a guild
*/
export function isApplicationCommandGuildInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIApplicationCommandGuildInteraction {
return isGuildInteraction(interaction);
}
// MessageComponentInteractions
/**
* A type-guard check for DM message component interactions
* @param interaction The message component interaction to check against
* @returns A boolean that indicates if the message component interaction was received in a DM channel
*/
export function isMessageComponentDMInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentDMInteraction {
return isDMInteraction(interaction);
}
/**
* A type-guard check for guild message component interactions
* @param interaction The interaction to check against
* @returns A boolean that indicates if the message component interaction was received in a guild
*/
export function isMessageComponentGuildInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentGuildInteraction {
return isGuildInteraction(interaction);
}
// Buttons
/**
* A type-guard check for buttons that have a `url` attached to them.
* @param component The button to check against
* @returns A boolean that indicates if the button has a `url` attached to it
*/
export function isLinkButton(component: APIButtonComponent): component is APIButtonComponentWithURL {
return component.style === ButtonStyle.Link;
}
/**
* A type-guard check for buttons that have a `custom_id` attached to them.
* @param button The button to check against
* @returns A boolean that indicates if the button has a `custom_id` attached to it
*/
export function isInteractionButton(component: APIButtonComponent): component is APIButtonComponentWithCustomId {
return component.style !== ButtonStyle.Link;
}
// Message Components
/**
* A type-guard check for message component interactions
* @param interaction The interaction to check against
* @returns A boolean that indicates if the interaction is a message component
*/
export function isMessageComponentInteraction(
interaction: APIInteraction,
): interaction is APIMessageComponentInteraction {
return interaction.type === InteractionType.MessageComponent;
}
/**
* A type-guard check for button message component interactions
* @param interaction The message component interaction to check against
* @returns A boolean that indicates if the message component is a button
*/
export function isMessageComponentButtonInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentButtonInteraction {
return interaction.data.component_type === ComponentType.Button;
}
/**
* A type-guard check for select menu message component interactions
* @param interaction The message component interaction to check against
* @returns A boolean that indicates if the message component is a select menu
*/
export function isMessageComponentSelectMenuInteraction(
interaction: APIMessageComponentInteraction,
): interaction is APIMessageComponentSelectMenuInteraction {
return interaction.data.component_type === ComponentType.SelectMenu;
}
// Application Commands
/**
* A type-guard check for chat input application commands.
* @param interaction The interaction to check against
* @returns A boolean that indicates if the interaction is a chat input application command
*/
export function isChatInputApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIChatInputApplicationCommandInteraction {
return interaction.data.type === ApplicationCommandType.ChatInput;
}
/**
* A type-guard check for context menu application commands.
* @param interaction The interaction to check against
* @returns A boolean that indicates if the interaction is a context menu application command
*/
export function isContextMenuApplicationCommandInteraction(
interaction: APIApplicationCommandInteraction,
): interaction is APIContextMenuInteraction {
return (
interaction.data.type === ApplicationCommandType.Message || interaction.data.type === ApplicationCommandType.User
);
}