Last active
May 22, 2017 06:20
-
-
Save chheller/d063bb4e94627df37272ba046810111f to your computer and use it in GitHub Desktop.
A first draft for a runescape discord bot.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const Discord = require('discord.js'); | |
const client = new Discord.Client(); | |
const rsapi = require('runescape-api'); | |
const fs = require('fs'); | |
let mainChan; | |
let idNameMap = {}; | |
client.on('ready', () => { | |
init(); | |
console.log('Phileas, at your service'); | |
mainChan = client.channels.get(process.env.GENFAGID); | |
}); | |
function nameToId(name) { | |
var minDist = 1000; | |
var closestItem, closestIds; | |
name = name.toLowerCase().replace(/\s/g,''); | |
if (idNameMap[name] !== undefined) | |
return idNameMap[name]; | |
//Find and return the next closest item | |
for (var namekey in idNameMap) { | |
var val = dziemba_levenshtein(name, namekey); | |
if ( val < minDist) { | |
minDist = val; | |
closestItem = idNameMap[namekey][0].name; | |
closestIds = idNameMap[namekey]; | |
} | |
} | |
mainChan.send("I'm sorry, but I could not find that item. Were you looking for " + closestItem + "?"); | |
return closestIds; | |
} | |
function init() { | |
//Get that JSON into a crazy fuckin array or something I don't know | |
let itemIdMap = JSON.parse(fs.readFileSync('./items.json', 'utf-8')); | |
for (id in itemIdMap) { | |
let name = itemIdMap[id].name.toLowerCase().replace(/\s/g,''); | |
if (!(name in idNameMap)) { | |
idNameMap[name] = []; | |
} if (!({id, name} in idNameMap[name])) { | |
//cause fuck whatever makes sense | |
idNameMap[name].push({id : id, name :itemIdMap[id].name}); | |
} | |
} | |
} | |
client.on('message', (message) => { | |
var body = message.content; | |
if (body.includes("!help")) { | |
mainChan.send("I can show you GE prices. Format your query as <pr name of item>"); | |
} | |
if (body.indexOf('<pr') != -1 && body.indexOf('>') != -1 && message.author.username != 'Phileas') { | |
let itemName = body.substring(body.indexOf('<pr')+3, body.indexOf('>')); | |
let itemId = nameToId(itemName); | |
if (itemId !== undefined) { | |
console.log(itemId); | |
let iter = 0; | |
let found = false; | |
console.log(itemId.length); | |
while(!found && iter < itemId.length) { | |
console.log(iter); | |
rsapi.osrs.ge.itemInformation(itemId[iter].id).then(function(item) { | |
let embed = new Discord.RichEmbed() | |
.setColor((item.item.today.price > 0) ?0x00AE86 : 0xFF0000) | |
.setDescription( | |
item.item.name + ": " + (item.item.current.price) + "gp\n" | |
+ ((item.item.members == 'true') ? 'Members' : 'Free') + "\n" | |
+ item.item.today.price + "gp" | |
).setThumbnail(item.item.icon_large) | |
; | |
found = true; | |
message.channel.send({embed}); | |
}).catch(console.error); | |
iter++; | |
} | |
} | |
} | |
}); | |
//Derek Ziemba via https://gist.github.com/andrei-m/982927 | |
function dziemba_levenshtein(a, b){ | |
var tmp; | |
if (a.length === 0) { return b.length; } | |
if (b.length === 0) { return a.length; } | |
if (a.length > b.length) { tmp = a; a = b; b = tmp; } | |
var i, j, res, alen = a.length, blen = b.length, row = Array(alen); | |
for (i = 0; i <= alen; i++) { row[i] = i; } | |
for (i = 1; i <= blen; i++) { | |
res = i; | |
for (j = 1; j <= alen; j++) { | |
tmp = row[j - 1]; | |
row[j - 1] = res; | |
res = b[i - 1] === a[j - 1] ? tmp : Math.min(tmp + 1, Math.min(res + 1, row[j] + 1)); | |
} | |
} | |
return res; | |
} | |
client.login(process.env.TOKEN); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment