Search autocode snippets inside discord. Uses: "!search-snippet <search term>"
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
let query = context.params.event.content.split(' ')?.slice(1)?.join('%20');
if (!query)
return lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Please provide a query to search`,
message_reference: {
message_id: `${context.params.event.id}`,
},
});
let request = await lib.http.request['@1.1.6'].get({
url: `https://autocode.com/snippet/?q=${query}&prefetch`,
}).then((s) => {
return s.data;
});
if (!request.data.snippets.length)
return lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `No result found`,
message_reference: {
message_id: `${context.params.event.id}`,
},
});
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Here's the result for \`${request.data.searchParam}\``,
message_reference: {
message_id: `${context.params.event.id}`,
},
embeds: request.data.snippets.slice(0, 10).map((s) => ({
title: `${s.title}`,
url: `https://autocode.com/snippet/${s.user.username}/${s.sid}`,
description: `${s.description}`,
color: 0x00ff00,
thumbnail: {
url: `${s.user.full_image_url}`,
},
fields: [
{
name: 'Views',
value: `\`${s.view_count}\``,
inline: true,
},
{
name: 'Installs',
value: `\`${s.install_count}\``,
inline: true,
},
{
name: 'Last updated',
value: ``,
inline: true,
},
{
name: 'Endpoint',
value: `\`${s.snippets[0].endpoints[0].eventDefinition ? `${s.snippets[0].endpoints[0].eventDefinition.eventSource.name}-${s.snippets[0].endpoints[0].eventDefinition.name}` : 'HTTP(S)'}\``,
inline: true,
},
{
name: 'Tags',
value: `\`${s.topicTags.length ? s.topicTags.map((t) => t.name).join(', ') : '- -'}\``,
inline: true,
},
{
name: 'API tags',
value: `\`${s.snippets[0].apiTags.length? s.snippets[0].apiTags.map((t) => t.user.username).join(', '): '- -'}\``,
inline: true,
},
],
})),
});