@diamondbot/joke-command
Version:
DiamondBot command to post dad jokes
110 lines (84 loc) • 3.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _core = require("@diamondbot/core");
var _nodeFetch = _interopRequireDefault(require("node-fetch"));
var _pickRandomWeighted = _interopRequireDefault(require("pick-random-weighted"));
var _microsoftCapitalize = _interopRequireDefault(require("microsoft-capitalize"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
class JokeCommand extends _core.ChatCommand {
constructor(options = {}) {
var _options$categories, _options$categoriesCh, _options$dadEmoji, _options$yomamaEmoji, _options$antiEmoji;
super(Object.assign({
name: 'joke',
description: 'Get some bot humor'
}, options));
this.categories = (_options$categories = options.categories) !== null && _options$categories !== void 0 ? _options$categories : ['dad', 'anti', 'yomama'];
this.categoriesChance = (_options$categoriesCh = options.categoriesChance) !== null && _options$categoriesCh !== void 0 ? _options$categoriesCh : null;
if (this.categoriesChance && this.categoriesChance.length !== this.categories.length) {
throw new Error('categoriesChance must be an array the same length as categories');
}
if (this.categoriesChance) {
this.weightedCategories = this.categories.map((c, i) => [c, this.categoriesChance[i]]);
} else {
this.weightedCategories = this.categories.map((c, i) => [c, 1]);
}
this.dadEmoji = (_options$dadEmoji = options.dadEmoji) !== null && _options$dadEmoji !== void 0 ? _options$dadEmoji : 'KEKW';
this.yomamaEmoji = (_options$yomamaEmoji = options.yomamaEmoji) !== null && _options$yomamaEmoji !== void 0 ? _options$yomamaEmoji : 'KEKW';
this.antiEmoji = (_options$antiEmoji = options.antiEmoji) !== null && _options$antiEmoji !== void 0 ? _options$antiEmoji : 'KEKWait';
}
getRandomCategory() {
return (0, _pickRandomWeighted.default)(this.weightedCategories);
}
async getDadJoke() {
const res = await (0, _nodeFetch.default)('https://icanhazdadjoke.com', {
headers: {
'Accept': 'application/json'
}
});
const data = await res.json();
if (data.status !== 200 || !data.joke) {
throw new Error(`Failed to get dad joke, ${JSON.stringify(data)}`);
}
return data.joke;
}
async getAntiJoke() {
const res = await (0, _nodeFetch.default)('https://generatorfun.com/code/model/generatorcontent.php?recordtable=generator&recordkey=109&gen=Y&itemnumber=1&randomoption=undefined&genimage=No&nsfw=No&keyword=undefined&tone=Normal');
const data = await res.text();
let joke = data.replace(/.*<p>(.+?)<\/p>.*/ims, '$1').trim();
if (!joke) {
throw new Error(`Failed to get anti joke, ${data}`);
}
joke = joke.replace(/\. See also .+\.$/, '').replace(/\.$/, '');
return (0, _microsoftCapitalize.default)(joke.toLowerCase());
}
async getYoMamaJoke() {
const res = await (0, _nodeFetch.default)('https://api.yomomma.info/');
const data = await res.json();
if (!data.joke) {
throw new Error(`Failed to get yomama joke, ${JSON.stringify(data)}`);
}
return data.joke;
}
async exec({
channel
}) {
const category = this.getRandomCategory();
let joke = 'Your mom';
let emoji;
if (category === 'dad') {
joke = await this.getDadJoke();
emoji = this.bot.emoji(this.dadEmoji);
} else if (category === 'yomama') {
joke = await this.getYoMamaJoke();
emoji = this.bot.emoji(this.yomamaEmoji);
} else if (category === 'anti') {
joke = await this.getAntiJoke();
emoji = this.bot.emoji(this.antiEmoji);
}
await channel.send(`${joke} ${emoji}`);
}
}
exports.default = JokeCommand;