Skip to content

Instantly share code, notes, and snippets.

@TobiasFeld22
Last active October 22, 2017 18:18
Show Gist options
  • Save TobiasFeld22/99529f0f0aec2104dad26e0a22f57400 to your computer and use it in GitHub Desktop.
Save TobiasFeld22/99529f0f0aec2104dad26e0a22f57400 to your computer and use it in GitHub Desktop.
Runkit discordbots.org update endpoint
MIT License
Copyright (c) 2017 Tobias Feld (tobias.feld@hotmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
//
// Made by Tobias feld | https://github.com/TobiasFeld22
// Last change: 22-10-2017 | 20:20 (CEST)
// This notebook is licensed with the MIT license,
// full file and the full license can be found in this gist:
// https://gist.github.com/TobiasFeld22/99529f0f0aec2104dad26e0a22f57400
var express = require("@runkit/runkit/express-endpoint/1.0.0");
var app = express(exports);
app.all("/:id/:token/:servers", (req, res) => {
if (req.params.id === null || isNaN(req.params.id) === true) {
return res.send("Your <strong>Bot id</strong> is not defined correctly, use the endpoint like this:<br><br>")
} else if (req.params.token === null || req.params.token.length <= 150) {
return res.send("Your <strong>Api token</strong> is not defined correctly, use the endpoint like this:<br><br>")
} else if (req.params.servers === null || isNaN(req.params.servers) === true) {
return res.send("Your <strong>Server amount</strong> is not defined correctly, use the endpoint like this:<br><br>" + process.env.RUNKIT_ENDPOINT_URL + "<strong>:bot_id</strong>/<strong>:api_token</strong>/<strong>:server_amount</strong>/")
}
update(req.params.servers, req.params.token, req.params.id, res)
})
app.all("*", (req, res) => {
res.send("To use this endpoint, send a request (get / post (doesn't matter)) to this endpoint like this:<br><br>" + process.env.RUNKIT_ENDPOINT_URL + "<strong>:bot_id</strong>/<strong>:api_token</strong>/<strong>:server_amount</strong>/<br><br>See raw code:<br><a href=\"https://gist.github.com/TobiasFeld22/99529f0f0aec2104dad26e0a22f57400\"> Click here </a>")
})
function update(servers, token, id, res) {
const snekfetch = require("snekfetch")
var servers = parseInt(servers)
snekfetch.post('https://discordbots.org/api/bots/' + id + '/stats').set('Authorization', token).send({
server_count: parseInt(servers)
}).then(r => {
res.sendStatus(200)
}).catch(e => {
var extra = "";
if ("" + e == "Error: 403 Forbidden") {
extra = extra + "<br><br>This error mostly happens when your id and token are not belonging together, or you mistyped either token or id. "
}
res.status(500).send("Error occurred while posting stats: \n" + e + extra)
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment