k.yr
k.yrβ€’2y ago

Discord Bot - Issues responding to interactions

Hello πŸ‘‹ I can't seem to get my Convex endpoint to respond to my interactions and I have no idea why. Going by the below code any interaction should be met with a message response of Blah however, I just get an empty response (shown in attached image).
const discordRoute = httpAction(async (ctx, request) => {
console.log("Received request");
const { isValid, interaction } = await verifyDiscordRequest(request);

console.log("Is request valid", isValid);

if (!isValid || !interaction)
return new Response("Bad request signature.", { status: 401 });

if (interaction.type === InteractionType.Ping)
return new Response(
JSON.stringify({
type: InteractionResponseType.Pong,
}),
{ status: 200 },
);

console.log("Got here");
console.log("interaction type", interaction.type);

if (interaction.type === InteractionType.ApplicationCommand) {
return new Response(
JSON.stringify({
type: InteractionResponseType.ChannelMessageWithSource,
data: { content: "Blah" },
}),
{ headers: { "Content-Type": "application/json" }, status: 200 },
);
}
return new Response("Unknown request", { status: 500 });
});

http.route({
path: "/discord",
method: "POST",
handler: discordRoute,
});
const discordRoute = httpAction(async (ctx, request) => {
console.log("Received request");
const { isValid, interaction } = await verifyDiscordRequest(request);

console.log("Is request valid", isValid);

if (!isValid || !interaction)
return new Response("Bad request signature.", { status: 401 });

if (interaction.type === InteractionType.Ping)
return new Response(
JSON.stringify({
type: InteractionResponseType.Pong,
}),
{ status: 200 },
);

console.log("Got here");
console.log("interaction type", interaction.type);

if (interaction.type === InteractionType.ApplicationCommand) {
return new Response(
JSON.stringify({
type: InteractionResponseType.ChannelMessageWithSource,
data: { content: "Blah" },
}),
{ headers: { "Content-Type": "application/json" }, status: 200 },
);
}
return new Response("Unknown request", { status: 500 });
});

http.route({
path: "/discord",
method: "POST",
handler: discordRoute,
});
My log output looks like,
'Received request'
log
'Is request valid' true
log
'Got here'
log
'interaction type' 2
'Received request'
log
'Is request valid' true
log
'Got here'
log
'interaction type' 2
I cannot see any issues with my Discord bot permissions either. Any help would be much appreciated!
No description
5 Replies
sshader
sshaderβ€’2y ago
If you’re using the JS discord-interactions library, I think the interaction types are InteractionType.APPLICATION_COMMAND. In terms of debugging, adding some more log lines (maybe right before returning the response) could help?
mikeysee
mikeyseeβ€’2y ago
@k.yr is it possible to "curl" or use the logs to see the raw HTTP output from your convex function and then compare that to the output from a known good HTTP output and see where the difference lies? could it possibly be a CORS issue?
k.yr
k.yrOPβ€’2y ago
Thanks for the help all! @sshader I'm using the discord-api-types library which has different case for their types. I added another log line but still no luck :/ @mikeysee I added some CORS headers and still no luck. I will try and poke the Convex endpoint a bit more so I can see the HTTP outputs like you suggested πŸ€” If I just post a message to a channel that works fine its responding to the interaction that is the issue I think? For example the below code correctly posts a message of "Blah" in the channel the interaction is instigated
const sendMeAMessage = httpAction(async (ctx, request) => {
console.log("Received request");

const interaction: APIBaseInteraction<any, any> = await request.json();

if (interaction.type === InteractionType.ApplicationCommand)
await discordRequest({
endpoint: DiscordAPI.createChannelMessage(
interaction.channel?.id as string,
),
method: "POST",
body: { content: "Blah" },
});

return new Response(null, { status: 200 });
});

http.route({
path: "/discord",
method: "POST",
handler: sendMeAMessage, //discordRoute,
});
const sendMeAMessage = httpAction(async (ctx, request) => {
console.log("Received request");

const interaction: APIBaseInteraction<any, any> = await request.json();

if (interaction.type === InteractionType.ApplicationCommand)
await discordRequest({
endpoint: DiscordAPI.createChannelMessage(
interaction.channel?.id as string,
),
method: "POST",
body: { content: "Blah" },
});

return new Response(null, { status: 200 });
});

http.route({
path: "/discord",
method: "POST",
handler: sendMeAMessage, //discordRoute,
});
mikeysee
mikeyseeβ€’2y ago
hmmm.. I dont know then 😦
k.yr
k.yrOPβ€’2y ago
I changed the interactions endpoint of another working bot to my new convex one and it worked. Well turns out I somehow had managed to include 'Discord' in the Name of the new bot...removing 'Discord' from the name resolved this issue sigh

Did you find this page helpful?