This snippet serves as Webhookexample to fill in in the "Webhook URL" Field on top.gg. On a Vote, TopGG will send a trigger to the Webhook and trigger the sending of the Vote Confirmation. Thankyou via Serverchannel and DM Message. If you place the file under functions/events/webhook/topgg.js it webhook URL will be https://PROJECT_NAME.USERNAME.autocode.gg/events/webhook/topgg/
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
const {user: user_id, type, query, bot} = context.params;
let voteChannel = process.env.VOTE_CHANNEL_ID;
let botname = process.env.BOT_NAME;
if (type === 'upvote') {
//1. Send DM
//get the member from the userid
let member = await lib.discord.users['@release'].retrieve({
user_id: user_id,
});
const {id, username, discriminator, avatar_url} = member;
try {
await lib.discord.users.dms.create({
recipient_id: `${user_id}`,
content: ``,
embeds: [
{
type: 'rich',
title: `:tada: You voted for ${botname} on TopGG`,
description: `Thank you so much for supporting me, you're the best <3`,
color: 0xffff,
},
],
});
} catch (e) {
console.log(
`Couldnt send DM to Voter. Name: ${username}, Discord: ${username}#${discriminator}, ID: ${id}, Provider: ${p.name}`
);
}
//Message To Server
await lib.discord.channels['@release'].messages.create({
channel_id: `${voteChannel}`,
content: ``,
embeds: [
{
type: 'rich',
title: `:tada: New Vote on TopGG`,
description: `\`${username}#${discriminator}\` has **voted** for ${botname}!\nUse \`/vote\` or vote [here](https://top.gg/bot/${bot}/vote) directly.`,
color: 0xffff,
},
],
});
} else if (type === 'test') {
await lib.discord.channels['@release'].messages.create({
channel_id: `${voteChannel}`,
content: ``,
embeds: [
{
type: 'rich',
title: `:tada: Test was run`,
description: `TopGG Webhook test.`,
color: 0xffff,
},
],
});
}
return {
statusCode: 200,
body: context.params,
};