irc-bot/spells/whois.js

70 lines
2.3 KiB
JavaScript

const axios = require("axios");
// For reason: we should get "tilde.TLD"!
module.exports = class Whois {
constructor(client, from, to, incantation) {
this.name = "Whois";
this.client = client;
this.summoner = from;
this.channel = to;
this.incantation = incantation;
this.spell = "kiu estas";
this.cheatCode = "whois";
this.varsUsed = 1;
this.casted = false;
this.locked = false;
}
test() {
if (this.incantation === this.spell) {
// Only the author can use these spells
if (this.locked && this.summoner !== this.client.config.author) {
const say = "Blasfemo! That spell is forbidden to you."
this.client.say(this.channel, say);
return false;
}
this.casted = true;
return this.cast.apply(this, arguments);
}
return { say: false };
}
cast(domain) {
let response = {
say: false,
debug: {},
content: ""
};
const apiKey = this.client.config.api.jsonwhoisapi;
const apiURL = "https://api.jsonwhoisapi.com/v1/whois";
const self = this;
axios.get(`${apiURL}?identifier=${domain}`, {
headers: {
"Authorization": `${apiKey}`
}
}).then((res) => {
// const data = JSON.stringify(res.data);
// console.log(`${self.name} :: ${data}`);
const nameservers = res.data.nameservers.length;
const registrar = res.data.registrar;
const registered = res.data.registered;
if (registered) {
const logic = registrar.id || nameservers > 0;
const status = logic ? "registered" : "available";
self.client.say(self.channel, `${domain} is "${status}"`);
} else {
const zipper = `${this.summoner}, vi zipro, ${domain}`;
self.client.say(self.channel, `${zipper} is not a domain!`);
}
}).catch((error) => {
const zipper = `${this.summoner}, vi zipro, ${domain}`;
self.client.say(self.channel, `${zipper} cannot exist!`);
});
return response;
}
}