@@ -7,7 +7,8 @@ Top.gg Webhook
77
88* [ Webhook] ( #Webhook )
99 * [ new Webhook(authorization)] ( #new_Webhook_new )
10- * [ .middleware()] ( #Webhook+middleware )
10+ * [ .listener(fn)] ( #Webhook+listener ) ⇒
11+ * ~~ [ .middleware()] ( #Webhook+middleware ) ~~
1112
1213<a name =" new_Webhook_new " ></a >
1314
@@ -27,21 +28,49 @@ const { Webhook } = require(`@top-gg/sdk`)
2728const app = express ()
2829const wh = new Webhook (' webhookauth123' )
2930
30- app .post (' /dblwebhook' , wh .middleware (), ( req , res ) => {
31- // req. vote is your vote object e.g
32- console .log (req . vote .user ) // => 321714991050784770
33- })
31+ app .post (' /dblwebhook' , wh .listener ( vote => {
32+ // vote is your vote object e.g
33+ console .log (vote .user ) // => 321714991050784770
34+ }))
3435
3536app .listen (80 )
3637
3738// In this situation, your TopGG Webhook dashboard should look like
3839// URL = http://your.server.ip:80/dblwebhook
3940// Authorization: webhookauth123
4041```
42+ <a name =" Webhook+listener " ></a >
43+
44+ ### webhook.listener(fn) ⇒
45+ Listening function for handling webhook requests
46+
47+ ** Kind** : instance method of [ <code >Webhook</code >] ( #Webhook )
48+ ** Returns** : An express request handler
49+
50+ | Param | Description |
51+ | --- | --- |
52+ | fn | Vote handling function, this function can also throw an error to allow for the webhook to resend from Top.gg |
53+
54+ ** Example**
55+ ``` js
56+ app .post (' /webhook' , wh .listener ((vote ) => {
57+ console .log (vote .user ) // => 395526710101278721
58+ }))
59+ ```
60+ ** Example**
61+ ``` js
62+ // Throwing an error to resend the webhook
63+ app .post (' /webhook/' , wh .listener ((vote ) => {
64+ // for example, if your bot is offline, you should probably not handle votes and try again
65+ if (bot .offline ) throw new Error (' Bot offline' )
66+ }))
67+ ```
4168<a name =" Webhook+middleware " ></a >
4269
43- ### webhook.middleware()
44- Middleware function to pass to express, sets req.vote to the payload
70+ ### ~~ webhook.middleware()~~
71+ *** Deprecated***
72+
73+ (Use the new .listener() function) Middleware function to pass to express, sets req.vote to the payload
4574
4675** Kind** : instance method of [ <code >Webhook</code >] ( #Webhook )
4776** Example**
0 commit comments