forked from Purgatoria/meshtastic-bot-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeprem.js
More file actions
83 lines (68 loc) · 2.96 KB
/
deprem.js
File metadata and controls
83 lines (68 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const axios = require('axios');
const API_URL = "https://api.orhanaydogdu.com.tr/deprem/afad/live";
const MAGNITUDE_THRESHOLD = 4.0;
function formatEarthquakeShort(eq, prefix = "🚨 AFAD DEPREM") {
const title = eq.title || "Bilinmeyen Konum";
const mag = eq.mag || "0.0";
const depth = eq.depth || "0";
const dateTime = eq.date_time || "";
const time = dateTime.split(" ")[1] || "";
// Veritabanı limitine takılmaması için (191 karakter) çok kısa tutuyoruz
return `${prefix}\n📍 ${title}\n📊 Büyüklük: ${mag} (M)\n⏬ Derinlik: ${depth}km\n⏱️ Saat: ${time}`.trim();
}
module.exports = {
name: 'deprem',
description: 'AFAD depremlerini bildirir',
run: async (message, utils) => {
console.log(`[Deprem] Gelen mesaj: ${message}`);
const args = message.substring(utils.PREFIX.length).split(" ");
const command = args[0].toLowerCase();
if (command === 'deprem') {
console.log("[Deprem] Komut algılandı, veriler çekiliyor...");
try {
const response = await axios.get(API_URL);
const earthquakes = response.data.result || [];
if (earthquakes.length > 0) {
const latest = earthquakes[0];
const msg = formatEarthquakeShort(latest, "🔍 En Son Deprem:");
utils.publishMessage(4294967295, msg);
utils.sendDiscord(msg);
}
} catch (err) {
console.error("[Deprem Modülü] Manuel sorgu hatası:", err.message);
}
}
},
init: (utils) => {
let seenIds = new Set();
let isFirstRun = true;
const checkEarthquakes = async () => {
try {
const response = await axios.get(API_URL);
const earthquakes = response.data.result || [];
if (earthquakes.length === 0) return;
if (isFirstRun) {
earthquakes.forEach(eq => {
if (eq.earthquake_id) seenIds.add(eq.earthquake_id);
});
isFirstRun = false;
return;
}
const newEarthquakes = earthquakes.filter(eq => eq.earthquake_id && !seenIds.has(eq.earthquake_id));
for (const eq of newEarthquakes.reverse()) {
seenIds.add(eq.earthquake_id);
const mag = parseFloat(eq.mag || 0);
if (mag >= MAGNITUDE_THRESHOLD) {
const mesaj = formatEarthquakeShort(eq);
utils.publishMessage(4294967295, mesaj);
utils.sendDiscord(mesaj);
}
}
} catch (error) {
console.error("[Deprem Modülü] API hatası:", error.message);
}
};
setInterval(checkEarthquakes, 120000);
checkEarthquakes();
}
};