Skip to content

Commit 556017f

Browse files
committed
Start logging our slash command usage to see how popular it is
1 parent 952fe82 commit 556017f

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"dependencies": {
1111
"@sentry/node": "^6.4.1",
1212
"chrono-node": "^2.3.8",
13-
"detritus-client": "^0.17.0-beta.5",
13+
"detritus-client": "^0.17.0-beta.7",
1414
"emoji-aware": "^3.0.5",
1515
"juration": "^0.1.1",
1616
"moment": "^2.29.1",

src/api/raw.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ export async function createUserCommand(
242242
): Promise<RestResponsesRaw.CreateUserCommand> {
243243
const body = {
244244
channel_id: options.channelId,
245+
command_type: options.commandType,
245246
edited_timestamp: options.editedTimestamp,
246247
failed_reason: options.failedReason,
247248
guild_id: options.guildId,

src/api/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export namespace RestOptions {
4343

4444
export interface CreateUserCommand {
4545
channelId: string,
46+
commandType?: number,
4647
editedTimestamp?: null | number,
4748
failedReason?: string,
4849
guildId?: string,

src/commands/interactions/basecommand.ts

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {
1010
import { Embed, Markup } from 'detritus-client/lib/utils';
1111
import { Response } from 'detritus-rest';
1212

13-
import { EmbedColors, PermissionsText } from '../../constants';
13+
import { createUserCommand } from '../../api';
14+
import { CommandTypes, EmbedColors, PermissionsText } from '../../constants';
1415
import { DefaultParameters, Parameters, editOrReply } from '../../utils';
1516

1617

@@ -166,17 +167,69 @@ export class BaseInteractionCommand<ParsedArgsFinished = Interaction.ParsedArgs>
166167
].join('\n'));
167168
}
168169

169-
return context.editOrRespond({
170+
await context.editOrRespond({
170171
embed,
171172
flags: MessageFlags.EPHEMERAL,
172173
});
174+
175+
if (context.invoker && context.invoker.metadata) {
176+
let commandType: null | number = null;
177+
switch (context.command.type) {
178+
case ApplicationCommandTypes.CHAT_INPUT: commandType = CommandTypes.APPLICATION_SLASH; break;
179+
case ApplicationCommandTypes.MESSAGE: commandType = CommandTypes.APPLICATION_MENU_MESSAGE; break;
180+
case ApplicationCommandTypes.USER: commandType = CommandTypes.APPLICATION_MENU_USER; break;
181+
default: commandType = null;
182+
}
183+
184+
if (commandType !== null) {
185+
const metadata = context.invoker.metadata;
186+
const commandId = metadata.id || context.name.split(' ').join('.')
187+
await createUserCommand(
188+
context,
189+
context.userId,
190+
commandId,
191+
{
192+
commandType,
193+
channelId: context.channelId!,
194+
failedReason: String(error.message || error.stack).slice(0, 256),
195+
guildId: context.guildId,
196+
messageId: context.interactionId,
197+
responseId: context.responseId,
198+
},
199+
);
200+
}
201+
}
173202
}
174203

175-
/*
176204
async onSuccess(context: Interaction.InteractionContext, args: ParsedArgsFinished) {
177205
// log usage
206+
if (context.invoker && context.invoker.metadata) {
207+
let commandType: null | number = null;
208+
switch (context.command.type) {
209+
case ApplicationCommandTypes.CHAT_INPUT: commandType = CommandTypes.APPLICATION_SLASH; break;
210+
case ApplicationCommandTypes.MESSAGE: commandType = CommandTypes.APPLICATION_MENU_MESSAGE; break;
211+
case ApplicationCommandTypes.USER: commandType = CommandTypes.APPLICATION_MENU_USER; break;
212+
default: commandType = null;
213+
}
214+
215+
if (commandType !== null) {
216+
const metadata = context.invoker.metadata;
217+
const commandId = metadata.id || context.name.split(' ').join('.')
218+
await createUserCommand(
219+
context,
220+
context.userId,
221+
commandId,
222+
{
223+
commandType,
224+
channelId: context.channelId!,
225+
guildId: context.guildId,
226+
messageId: context.interactionId,
227+
responseId: context.responseId,
228+
},
229+
);
230+
}
231+
}
178232
}
179-
*/
180233
}
181234

182235

0 commit comments

Comments
 (0)