mirror of
https://github.com/syuilo/ai.git
synced 2025-03-25 21:12:56 +00:00
ごはんさーち
This commit is contained in:
parent
9030b376de
commit
9d7f82d117
3 changed files with 68 additions and 0 deletions
|
@ -20,10 +20,12 @@
|
|||
"autobind-decorator": "2.4.0",
|
||||
"canvas": "2.8.0",
|
||||
"chalk": "4.1.1",
|
||||
"jsdom": "19.0.0",
|
||||
"lokijs": "1.5.12",
|
||||
"memory-streams": "0.1.3",
|
||||
"misskey-reversi": "0.0.5",
|
||||
"module-alias": "2.2.2",
|
||||
"node-fetch": "2.6.7",
|
||||
"promise-retry": "2.0.1",
|
||||
"random-seed": "0.3.0",
|
||||
"reconnecting-websocket": "4.4.0",
|
||||
|
@ -40,8 +42,10 @@
|
|||
"devDependencies": {
|
||||
"@koa/router": "9.4.0",
|
||||
"@types/jest": "26.0.23",
|
||||
"@types/jsdom": "16.2.14",
|
||||
"@types/koa": "2.13.1",
|
||||
"@types/koa__router": "8.0.4",
|
||||
"@types/node-fetch": "3.0.3",
|
||||
"@types/websocket": "1.0.2",
|
||||
"jest": "26.6.3",
|
||||
"koa": "2.13.1",
|
||||
|
|
|
@ -35,6 +35,7 @@ import NotingModule from './modules/noting';
|
|||
import PollModule from './modules/poll';
|
||||
import ReminderModule from './modules/reminder';
|
||||
import DicModule from './modules/dic';
|
||||
import menuModule from './modules/menu';
|
||||
import GetColorModule from './modules/color';
|
||||
|
||||
console.log(' __ ____ _____ ___ ');
|
||||
|
@ -91,6 +92,7 @@ promiseRetry(retry => {
|
|||
new PollModule(),
|
||||
new ReminderModule(),
|
||||
new DicModule(),
|
||||
new menuModule(),
|
||||
new GetColorModule(),
|
||||
]);
|
||||
}).catch(e => {
|
||||
|
|
62
src/modules/menu/index.ts
Normal file
62
src/modules/menu/index.ts
Normal file
|
@ -0,0 +1,62 @@
|
|||
import autobind from 'autobind-decorator';
|
||||
import Module from '@/module';
|
||||
import Message from '@/message';
|
||||
import fetch from 'node-fetch';
|
||||
import { JSDOM } from 'jsdom';
|
||||
import { title } from 'process';
|
||||
|
||||
export default class extends Module {
|
||||
public readonly name = 'menu';
|
||||
|
||||
@autobind
|
||||
public install() {
|
||||
return {
|
||||
mentionHook: this.mentionHook
|
||||
};
|
||||
}
|
||||
|
||||
@autobind
|
||||
private async mentionHook(msg: Message) {
|
||||
if (msg.text && msg.text.includes('ごはん')) {
|
||||
// 1~2535111の適当な数字を取得
|
||||
const random_number = Math.floor(Math.random() * 2535111) + 1;
|
||||
const url = `https://cookpad.com/recipe/${random_number}`;
|
||||
//testUrlして、200以外なら再取得
|
||||
const res = await fetch(url);
|
||||
if (res.status !== 200) {
|
||||
return this.mentionHook(msg);
|
||||
} else {
|
||||
//jsdomを利用してレシピのタイトルを取得
|
||||
const dom = new JSDOM(await res.text());
|
||||
//@ts-ignore
|
||||
let title = dom.window.document.querySelector('h1.recipe-title').textContent;
|
||||
// titleから改行を除去
|
||||
title = title!.replace(/\n/g, '');
|
||||
msg.reply(`こんなのどう?> [${title}](${url})`, {
|
||||
immediate: true
|
||||
});
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function testUrl(url: string) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const xhr = new XMLHttpRequest()
|
||||
xhr.open('GET', url)
|
||||
xhr.onload = () => {
|
||||
if (xhr.status === 200) {
|
||||
resolve(true)
|
||||
} else {
|
||||
reject(false)
|
||||
}
|
||||
}
|
||||
xhr.onerror = () => {
|
||||
reject(false)
|
||||
}
|
||||
xhr.send()
|
||||
})
|
||||
}
|
Loading…
Reference in a new issue