This is an example code for searching gifs using Tenor api. By using gif- <search-term> you can search any gif available on tenor! You must go to https://tenor.com/developer/keyregistration first to retrieve your API key. When you select the Add to project button, you will be prompted to insert the key. Enjoy!
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
module.exports = async (event, context) => {
if (event.content.startsWith('gif-')) {
let keyword = event.content.split(' ').slice(1).join(' ');
if (!keyword) {
return lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: `Please provite a term to search!`,
});;
}
//go to https://tenor.com/developer/keyregistration and get a api key and add it to the Environment Variables for this to work!
let json = await lib.http.request['@1.1.6'].get({
url: `https://g.tenor.com/v1/search?q=${keyword}&key=${process.env.Tenor_key}&contentfilter=high`
});
await lib.discord.channels['@0.3.0'].messages.create({
channel_id: `${context.params.event.channel_id}`,
content: json.data.results[Math.floor(Math.random() * json.data.results.length)].url,
});
}
};