Skip to content

Instantly share code, notes, and snippets.

@nicholastay
Created October 6, 2017 02:18
Show Gist options
  • Save nicholastay/086923aaa6c1f024f8aecd97643dbbc2 to your computer and use it in GitHub Desktop.
Save nicholastay/086923aaa6c1f024f8aecd97643dbbc2 to your computer and use it in GitHub Desktop.
get-osu-requester: request osu with customapi links like nightbot (poc)
{
"presets": [
"es2015"
]
}

get-osu-requester

For use with bots such as Nightbot which allow customapi links as commands.

Setup

(Ensure you have babel-cli installed)

$ git clone https://github.com/nicholastay/get-osu-requester.git && cd get-osu-requester
$ cp config.json.template config.json
$ vi config.json # or any other text editor
$ npm install
$ babel-node index.es6.js

Nightbot custom command example:
!r - $(customapi http://server.tld/api/osu_request?user=Nexerq&requester=$(user)&map=$(1))
With 'Nexerq' as the osu! username that is taking the requests

Compile for production:

$ npm run compile # Ensure you have babel installed
$ npm start # To start
{
"allowedUsers": ["Nexerq", "allowed_usernames_for_osu (their osu names that theyll put in &user=)"],
"osu": {
"chat": {
"username": "osu! username",
"password": "osu.ppy.sh/p/ircauth"
},
"api": {
"apiKey": "osu.ppy.sh/p/api"
}
}
}
import express from 'express';
import Osu from 'nodesu';
import {RateLimiter} from 'limiter';
import config from './config.json';
let app = express();
// osu! stuff
let osuChat = new Osu.chat(config.osu.chat);
let osuApi = new Osu.api(config.osu.api);
osuChat.on('connected', () => {
console.log('Connected to osu! bancho chat.');
});
osuChat.on('error', (err) => {
console.log('osu! chat ERROR - ' + err);
});
// Rate limit osu! chat
let osuChatLimiter = new RateLimiter(1, 'second'); // idk, but Tillerino is on this, but he may be on special terms
// Express handlers
app.get('/', (req, res) => {
res.send('get-osu-requester v1');
});
let osuMapRegex = /osu.ppy.sh\/(s|b)\/(\d+)/i;
app.get('/api/osu_request', (req, res) => {
// Initial checks
if (!req.query.user || !req.query.requester) return res.send('The custom URL handler is not setup properly...'); // No user defined or requester name not here
if (config.allowedUsers.indexOf(req.query.user) === -1) return res.send('Access Denied'); // Not in allowed users.
if (!req.query.map) return res.send('Invalid osu! map.'); // No map given
console.log(`Received API request: user - ${req.query.user}, requester - ${req.query.requester}, map - ${req.query.map}`);
// Kinda get started...
let osuMapMatch = osuMapRegex.exec(req.query.map);
if (!osuMapMatch) return res.send('Invalid osu! map.'); // Did not meet regex requirements
// Now lookup map
let mapType = osuMapMatch[1]; // /s|b
let mapID = osuMapMatch[2];
osuApi.getBeatmaps(osuApi.beatmap.byLetter(mapID, mapType), osuApi.mode.all, (err, resp) => {
if (err) {
console.log('osu! api get ERROR - ' + err);
return res.send('There is currently an issue with the osu! servers, please try again later.');
}
if (resp.length < 1) return res.send('Invalid osu! map.'); // No map, blank response
let map = resp[0];
console.log(`Found osu! map for '${req.query.requester}' [${req.query.user}] -- ${map.artist} - ${map.title}`);
let rankedStatus = osuApi.beatmap.approvalStatus[map.approved];
let msgToSend = `${req.query.requester} > [${rankedStatus}] ${map.artist} - ${map.title} [${map.version}] (mapped by ${map.creator}) <${(+map.difficultyrating).toFixed(2)}★ ${map.bpm}BPM>`;
let osuToSend = `${req.query.requester} > [${rankedStatus}] [http://osu.ppy.sh/${mapType}/${mapID} ${map.artist} - ${map.title} [${map.version}]] (mapped by ${map.creator}) <${(+map.difficultyrating).toFixed(2)}★ ${map.bpm}BPM>`;
osuChatLimiter.removeTokens(1, (err, remainingRequests) => {
osuChat.client.say(req.query.user, osuToSend);
});
res.send(msgToSend);
});
});
// Start express server
app.listen(3000, () => {
console.log('Listening on port 3000');
});
// Connect to osu! chat
osuChat.connect()
{
"name": "get-osu-requester",
"version": "1.0.0",
"description": "A HTTP server which can take osu! link requests, for use with bots such as NightBot which can do custom API calls",
"main": "index.es6.js",
"scripts": {
"start": "node index.es5.js",
"compile": "babel -o index.es5.js index.es6.js"
},
"author": "Nicholas Tay <nexerq@gmail.com>",
"license": "ISC",
"dependencies": {
"express": "^4.13.3",
"limiter": "^1.1.0",
"nodesu": "^0.2.1"
},
"devDependencies": {
"babel-preset-es2015": "^6.3.13"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment