Creating top.gg voting system with AOI.js
Required packages
Fisrt of all, we are going to import what we are going to use.
const { AoiClient, CustomEvent } = require('aoi.js');
const { Webhook } = require('@top-gg/sdk');
const app = require('express')();
Now we start an instance of the CustomEvent class.
const event = new CustomEvent(client); // "client" depends on how you declared your AoiClient.
Now lets create the vote listener.
const webhook = new Webhook('WEBHOOK_KEY');
app.post("/vote", webhook.listener((vote) => {
event.emit('topgg', vote); // Emits the event called "topgg".
})
);
Now go to your bot in top.gg: Edit > Webhooks
In the "Webhook URL" option you must add the link of your server, example:
https://aoibot.cyberghxst.repl.co/vote
In the "Authorization" option, put what you want as the authorization key, example: WEBHOOK_KEY
Now let's create the event command.
event.command({
listen: 'topgg',
code: `
$sendDM[Hi, thanks for voting!;$get[user]]
$djsEval[
d.vars['user'] = d.data.eventData[0].user
d.data.vars = d.vars
]
`
});
event.listen('topgg'); // Listens the event called "topgg".
So, final code will look like this:
const { AoiClient, CustomEvent } = require('aoi.js');
const { Webhook } = require('@top-gg/sdk');
const app = require('express')();
const bot = new AoiClient(...);
const webhook = new Webhook('YOUSHALLNOTPASS');
const event = new CustomEvent(bot);
event.command({
listen: 'topgg',
code: `
$sendDM[Hi, thanks for voting!;$get[user]]
$djsEval[
d.vars['user'] = d.data.eventData[0].user
d.data.vars = d.vars
]
`
});
event.listen('topgg');
app.post("/vote", webhook.listener((vote) => {
event.emit('topgg', vote);
})
);
app.listen(5000);
You can customize the code of the event.command so that the bot does what you want.