118 lines
3.9 KiB
JavaScript
Raw Permalink Normal View History

2025-04-24 16:26:27 +08:00
// app.js
2025-04-24 10:11:12 +08:00
// 關掉 webhook專心用 polling 模式
// 執行以下指令取消 webhook只需要做一次
// curl -F "url=" https://api.telegram.org/bot7916405163:AAGllDyNHNRTPDJ4gjwFPDi-TFDyFwD4foA/setWebhook
require('dotenv').config(); // 加這行載入 .env 檔案
const TelegramBot = require('node-telegram-bot-api');
2025-04-24 16:26:27 +08:00
const { connectDB, saveUser, getGroupUsers } = require('./db');
connectDB(); // 建立資料庫連線
2025-04-24 10:11:12 +08:00
const token = process.env.BOT_TOKEN; // 從環境變數讀取 token
//括號裡面的內容需要改為在第5步獲得的Token
const bot = new TelegramBot(token, { polling: true });
//使用Long Polling的方式與Telegram伺服器建立連線
//收到Start訊息時會觸發這段程式
bot.onText(/\/start/, function (msg) {
const chatId = msg.chat.id; //用戶的ID
const resp = '你好'; //括號裡面的為回應內容,可以隨意更改
bot.sendMessage(chatId, resp); //發送訊息的function
});
bot.onText(/\/help/, function (msg) {
const chatId = msg.chat.id;
2025-04-25 10:30:26 +08:00
const helpText = `
🤖 Bot 指令清單
/help -
/roll [XdY] - xy/roll 2d6
/draw [1, 2, 3] -
/everyone - username 使使
🎲 小提醒/roll 的最大支援為 100 顆骰最大面數為 1000
📌 你的發話紀錄會自動儲存以便 /everyone 使用
🛠 更多功能開發中...
`;
if (msg.from.id === 6218259358) {
const resp = '才不告訴逆雷';
bot.sendMessage(chatId, resp);
} else {
bot.sendMessage(chatId, helpText);
}
2025-04-24 10:11:12 +08:00
});
2025-04-25 10:19:33 +08:00
/**
* 骰子系統
*/
2025-04-24 10:11:12 +08:00
bot.onText(/\/roll(?:@[\w_]+)?(?:\s+(\d*)d(\d+))?/i, function (msg, match) {
// console.log(`✅ Bot roll`);
const chatId = msg.chat.id;
let count = parseInt(match[1]) || 1;
let sides = parseInt(match[2]) || 6;
if (count > 100 || sides > 1000) {
return bot.sendMessage(chatId, '⚠️ 數字過大,請保持理性!');
}
const rolls = Array.from({ length: count }, () => Math.floor(Math.random() * sides) + 1);
const total = rolls.reduce((sum, v) => sum + v, 0);
const resp = `🎲 ${count}d${sides} 擲出:\n🎯 ${rolls.join(' + ')} = ${total}`;
bot.sendMessage(chatId, resp);
2025-04-24 15:31:46 +08:00
});
2025-04-25 10:19:33 +08:00
/**
* 抽籤系統
*/
2025-04-25 10:30:26 +08:00
bot.onText(/\/draw(?:@[\w_]+)?\s*\[(.+)\]/i, function (msg, match) {
2025-04-25 10:19:33 +08:00
const chatId = msg.chat.id;
2025-04-25 10:30:26 +08:00
if (!match[1]) {
2025-04-25 10:19:33 +08:00
return bot.sendMessage(chatId, '⚠️ 請提供正確的選項格式,例如:/draw [香蕉, 你個, 芭樂]');
}
2025-04-25 10:30:26 +08:00
const options = match[1].split(',').map(o => o.trim()).filter(o => o.length > 0);
if (options.length === 0) {
return bot.sendMessage(chatId, '⚠️ 沒有有效的選項');
}
const choice = options[Math.floor(Math.random() * options.length)];
bot.sendMessage(chatId, `🎯 從 [${options.join(', ')}] 中抽出:\n👉 ${choice}`);
2025-04-25 10:19:33 +08:00
});
2025-04-24 15:31:46 +08:00
bot.onText(/\/everyone/, async (msg) => {
2025-04-24 16:26:27 +08:00
if (msg.chat.type !== 'group' && msg.chat.type !== 'supergroup') {
return bot.sendMessage(msg.chat.id, '⚠️ 此指令僅限群組使用');
}
const groupId = msg.chat.id;
const users = await getGroupUsers(groupId);
2025-04-24 15:31:46 +08:00
if (users.length === 0) {
2025-04-24 16:26:27 +08:00
return bot.sendMessage(groupId, '😅 沒有人講過話,無法標記');
2025-04-24 15:31:46 +08:00
}
2025-04-24 16:26:27 +08:00
const mentions = users
.filter(u => !!u.username)
.map(u => `@${u.username}`)
2025-04-24 15:31:46 +08:00
.join(' ');
2025-04-24 16:26:27 +08:00
if (!mentions) {
return bot.sendMessage(groupId, '⚠️ 沒有人有公開 username無法標記');
}
bot.sendMessage(groupId, `📣 呼叫所有人:\n${mentions}`);
2025-04-24 15:31:46 +08:00
});
bot.on('message', async (msg) => {
2025-04-24 16:26:27 +08:00
if (!msg.from || !msg.chat || !msg.chat.id || !msg.from.id) return;
if (msg.chat.type !== 'group' && msg.chat.type !== 'supergroup') return;
await saveUser(msg.from, msg.chat.id);
2025-04-24 10:11:12 +08:00
});