Skip to content

Commit

Permalink
Merge pull request #541 from hanshino:feature/whos-problem
Browse files Browse the repository at this point in the history
Update dependencies and add bot interaction features
  • Loading branch information
hanshino authored Nov 10, 2024
2 parents a7196ff + 528b763 commit 423ffb7
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 121 deletions.
17 changes: 17 additions & 0 deletions app/locales/zh_tw.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@
"lottery_ticket_auto_buy": "快選"
},
"message": {
"whos_problem": [
"{user} 在搞吧...",
"{user} 這個人在搞什麼...",
"{user} 這個人在搞什麼鬼...",
"{user} 的鍋!!!",
"{user} 出來背鍋",
"{user} 好了啦",
"{user} 超級可悲,就是你",
"{user} you",
"都 {user} 的錯",
"{user} 你是不是有點太過分了"
],
"whos_problem_only_one": [
"好了啦,就你在搞",
"就你在講話而已",
"老哥,就你在這而已"
],
"error_contact_admin": "如發現此訊息,請向管理員報告此問題\n使用者ID: {{ user_id }}\n錯誤類型: {{ error_key }}",
"user_own_god_stone": "你擁有的女神石: {{ god_stone }}",
"user_own_character_count": "你擁有的角色數量: {{ character_count }}",
Expand Down
8 changes: 4 additions & 4 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
"migrate": "knex migrate:latest"
},
"dependencies": {
"@sentry/node": "^8.36.0",
"@sentry/node": "^8.37.1",
"ajv": "^8.17.1",
"ajv-formats": "^3.0.1",
"axios": "1.7.7",
"bottender": "1.5.5",
"brotli": "^1.3.3",
"cheerio": "^1.0.0",
"config": "^3.3.12",
"cron": "^3.1.8",
"cron": "^3.1.9",
"date-format": "^4.0.14",
"express": "^4.21.1",
"express-rate-limit": "^7.4.1",
Expand All @@ -34,7 +34,7 @@
"md5": "^2.3.0",
"minimist": "^1.2.8",
"moment": "^2.30.1",
"mysql2": "^3.11.3",
"mysql2": "^3.11.4",
"redis": "^4.7.0",
"socket.io": "^4.8.1",
"sqlite3": "^5.1.7",
Expand All @@ -45,7 +45,7 @@
"@types/express": "^4.17.21",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"eslint": "^9.13.0",
"eslint": "^9.14.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.2.1",
"jest": "^29.7.0",
Expand Down
91 changes: 91 additions & 0 deletions app/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,44 @@ const NumberController = require("./controller/application/NumberController");
const JobController = require("./controller/application/JobController");
const { transfer } = require("./middleware/dcWebhook");
const redis = require("./util/redis");
const i18n = require("./util/i18n");
const traffic = require("./util/traffic");
const { showManagePlace } = require("./templates/application/Admin");
const { pushMessage } = require("./util/LineNotify");
const AdminModel = require("./model/application/Admin");
const axios = require("axios");
const pConfig = require("config");
const FetchGameData = require("../bin/FetchGameData");
const { get, sample } = require("lodash");

axios.defaults.timeout = 5000;

const askBot = (keyword, action) =>
route(context => {
if (context.event.isText === false) return false;
const mentionees = get(context, "event.message.mention.mentionees", []);
const isAskingBot = mentionees.some(mentionee => mentionee.isSelf === true);
if (!isAskingBot) return false;

if (keyword === undefined) {
throw new Error("Missing keyword");
}

if (typeof keyword === "string") {
return context.event.text.includes(keyword);
}

if (Array.isArray(keyword)) {
return keyword.some(k => context.event.text.includes(k));
}

if (keyword instanceof RegExp) {
return keyword.test(context.event.text);
}

return false;
}, action);

function showState(context) {
context.replyText(JSON.stringify(context.state));
}
Expand Down Expand Up @@ -339,6 +367,67 @@ async function CustomerOrderBased(context, { next }) {
if (detectResult === false) return next;
}

function interactWithBot(context) {
return router([
askBot("你好", context => context.replyText("你好啊!")),
askBot(["誰的問題", "誰在搞"], whosProblem),
]);
}

const recordLatestGroupUser = async (context, { next }) => {
if (context.event.source.type !== "group") return;
const { userId, groupId } = context.event.source;
const key = `latestGroupUser:${groupId}`;
await redis.zAdd(key, [
{
score: Date.now(),
value: userId,
},
]);
// 保留 timestamp 在 30 分鐘內的資料
await redis.zRemRangeByScore(key, 0, Date.now() - 30 * 60 * 1000);
return next;
};

/**
* 誰的問題
* @param {import("bottender").LineContext} context
*/
async function whosProblem(context) {
if (context.event.source.type !== "group") return;
const { groupId } = context.event.source;
const key = `latestGroupUser:${groupId}`;
const users = await redis.zRange(key, 0, -1);
const { quoteToken } = context.event.message;

if (users.length === 1) {
return context.replyText(sample(i18n.__("message.whos_problem_only_one")), { quoteToken });
}

const target = sample(users);
const replyMessage = [
{
type: "textV2",
text: sample(i18n.__("message.whos_problem")),
sender: {
name: "裁決者",
},
substitution: {
user: {
type: "mention",
mentionee: {
type: "user",
userId: target,
},
},
},
quoteToken,
},
];

context.reply(replyMessage);
}

function Nothing(context) {
switch (context.platform) {
case "line":
Expand All @@ -359,6 +448,7 @@ async function App(context) {
return chain([
setProfile, // 設置各式用戶資料
statistics, // 數據蒐集
recordLatestGroupUser, // 紀錄最近群組用戶
lineEvent, // 事件處理
config, // 設置群組設定檔
transfer, // Discord Webhook轉發
Expand All @@ -368,6 +458,7 @@ async function App(context) {
GlobalOrderBase, // 全群指令分析
OrderBased, // 指令分析
CustomerOrderBased, // 自訂指令分析
interactWithBot, // 標記機器人回應
Nothing, // 無符合事件
]);
}
Expand Down
Loading

0 comments on commit 423ffb7

Please sign in to comment.