forked from ChrisChrome/weather-bot
Compare commits
3 commits
main
...
debug-10-2
Author | SHA1 | Date | |
---|---|---|---|
|
ac4d7c86e7 | ||
|
60f7c22083 | ||
|
443c82fcd9 |
|
@ -91,4 +91,6 @@
|
|||
"zsechat@conference.weather.im",
|
||||
"zdcchat@conference.weather.im",
|
||||
"znychat@conference.weather.im"
|
||||
|
||||
|
||||
]
|
|
@ -1345,7 +1345,7 @@
|
|||
},
|
||||
"WSW": {
|
||||
"text": "Winter Storm Warning",
|
||||
"priority": 4
|
||||
"priority": 5
|
||||
},
|
||||
"WWA": {
|
||||
"priority": 1,
|
||||
|
@ -1524,7 +1524,7 @@
|
|||
"priority": 3
|
||||
},
|
||||
"PIR": {
|
||||
"text": "Pilot Reports",
|
||||
"priority": 1
|
||||
"priority": 0,
|
||||
"_comment": "This is an unknown event type, ignore it for now until we know more info"
|
||||
}
|
||||
}
|
||||
|
|
1
funcs.js
1
funcs.js
|
@ -1,4 +1,3 @@
|
|||
const geolib = require("geolib");
|
||||
// Use OSM API to get coordinates https://nominatim.openstreetmap.org/search?q=search+query&format=json&limit=1
|
||||
const getCoordinates = async (location) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
|
101
index.js
101
index.js
|
@ -21,7 +21,6 @@ satMessages = {};
|
|||
|
||||
// Setup Discord
|
||||
const discord = new Discord.Client({
|
||||
|
||||
intents: [
|
||||
"Guilds",
|
||||
"GuildVoiceStates",
|
||||
|
@ -36,6 +35,7 @@ const rest = new REST({
|
|||
version: '10'
|
||||
}).setToken(config.discord.token);
|
||||
|
||||
|
||||
// Setup SQlite DB
|
||||
const db = new sqlite3.Database("channels.db", (err) => {
|
||||
if (err) {
|
||||
|
@ -356,20 +356,12 @@ xmpp.on("stanza", (stanza) => {
|
|||
// get product id from "x" tag
|
||||
var evt = events[product_id.pil.substring(0, 3)];
|
||||
|
||||
if (evt.priority === 0) return; // This is an event that we don't care about
|
||||
|
||||
if (!evt) {
|
||||
evt = { name: "Unknown", priority: 3 }
|
||||
console.log(`${colors.red("[ERROR]")} Unknown event type: ${product_id.pil.substring(0, 3)}. Fix me`);
|
||||
console.log(`${colors.magenta("[DEBUG]")} ${bodyData.string}`)
|
||||
const logChannel = discord.guilds.cache.get(config.discord.mainGuild).channels.cache.get(config.discord.logChannel);
|
||||
logChannel.send({
|
||||
embeds: [
|
||||
{
|
||||
title: "Unknown Event Type",
|
||||
description: `Unknown event type: ${product_id.pil.substring(0, 3)}. Please check the logs for more details.`,
|
||||
color: 0xff0000
|
||||
}
|
||||
]
|
||||
});
|
||||
}
|
||||
|
||||
evt.code = product_id.pil.substring(0, 3);
|
||||
|
@ -457,16 +449,18 @@ xmpp.on("stanza", (stanza) => {
|
|||
console.log(`${colors.red("[ERROR]")} ${err.message}`);
|
||||
}
|
||||
if (!rows) return; // No channels to alert
|
||||
rows.forEach(async (row) => {
|
||||
rows.forEach((row) => {
|
||||
// Get Filters as arrays
|
||||
if (!row.filterEvt) row.filterEvt = "";
|
||||
if (!row.filter) row.filter = "";
|
||||
let filterEvt = row.filterEvt.toLowerCase().split(",");
|
||||
let filters = row.filter.toLowerCase().split(",");
|
||||
filterEvt = filterEvt.map(filter => filter.startsWith('!') ? filter.substring(1) : filter);
|
||||
filters = filters.map(filter => filter.startsWith('!') ? filter.substring(1) : filter);
|
||||
if (evt.priority < row.minPriority) return;
|
||||
// If the event type is not in th filter, ignore it. Make sure filterEvt isnt null
|
||||
if (!filterEvt[0]) filterEvt = [];
|
||||
if (!filterEvt.includes(evt.code.toLowerCase()) && !filterEvt.length == 0) return;
|
||||
if (filterEvt.some(filter => filter.startsWith('!') ? evt.code.toLowerCase() !== filter.substring(1) : !filter.includes(evt.code.toLowerCase())) && filterEvt.length > 0) return;
|
||||
|
||||
let channel = discord.channels.cache.get(row.channelid);
|
||||
if (!channel) return console.log(`${colors.red("[ERROR]")} Channel ${row.channelid} not found`);
|
||||
|
@ -483,53 +477,6 @@ xmpp.on("stanza", (stanza) => {
|
|||
console.error(err);
|
||||
}).then((msg) => {
|
||||
if (msg.channel.type === Discord.ChannelType.GuildAnnouncement) msg.crosspost();
|
||||
}).catch((err) => {
|
||||
console.log(`${colors.yellow("[WARN]")} Failed to send message to ${channel.guild.name}/${channel.name} (${channel.guild.id}/${channel.id})`);
|
||||
const logChannel = discord.guilds.cache.get(config.discord.mainGuild).channels.cache.get(config.discord.logChannel);
|
||||
logChannel.send({
|
||||
embeds: [
|
||||
{
|
||||
title: "Failed to send message",
|
||||
description: `There is likely an issue with permissions. Please notify the server owner if possible.
|
||||
Guild: ${channel.guild.name} (${channel.guild.id})
|
||||
Channel: ${channel.name} (${channel.id})
|
||||
Guild Owner: <@${channel.guild.ownerId}> (${channel.guild.ownerId})
|
||||
Sub Info: \`\`\`json\n${JSON.stringify(row)}\`\`\``,
|
||||
color: 0xff0000
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
discord.users.fetch(channel.guild.ownerId).then((user) => {
|
||||
user.send({
|
||||
embeds: [
|
||||
{
|
||||
title: "Issue with your subscribed channel.",
|
||||
description: `There is likely an issue with permissions. Please check that I can send messages in <#${channel.id}>\nYour subscription has been removed, and you will need to resubscribe to get alerts.`,
|
||||
color: 0xff0000,
|
||||
fields: [
|
||||
{
|
||||
name: "Guild",
|
||||
value: `${channel.guild.name} (${channel.guild.id})`
|
||||
},
|
||||
{
|
||||
name: "Channel",
|
||||
value: `${channel.name} (${channel.id})`
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}).catch((err) => {
|
||||
console.log(`${colors.red("[ERROR]")} Failed to send message to ${channel.guild.ownerId}`);
|
||||
}).then(() => {
|
||||
db.run(`DELETE FROM channels WHERE channelid = ? AND iemchannel = ?`, [channel.id, fromChannel], (err) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
}
|
||||
console.log(`${colors.cyan("[INFO]")} Deleted channel ${channel.id} from database`);
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
}).catch((err) => {
|
||||
|
@ -556,12 +503,15 @@ xmpp.on("stanza", (stanza) => {
|
|||
if (!row.filter) row.filter = "";
|
||||
let filterEvt = row.filterEvt.toLowerCase().split(",");
|
||||
let filters = row.filter.toLowerCase().split(",");
|
||||
filterEvt = filterEvt.map(filter => filter.startsWith('!') ? filter.substring(1) : filter);
|
||||
filters = filters.map(filter => filter.startsWith('!') ? filter.substring(1) : filter);
|
||||
|
||||
|
||||
// If priority is less than the min priority, ignore it
|
||||
if (evt.priority < row.minPriority) return;
|
||||
// If the event type is not in th filter, ignore it. Make sure filterEvt isnt null
|
||||
if (!filterEvt[0]) filterEvt = [];
|
||||
if (!filterEvt.includes(evt.code.toLowerCase()) && !filterEvt.length == 0) return;
|
||||
if (filterEvt.some(filter => filter.startsWith('!') ? evt.code.toLowerCase() === filter.substring(1) : !filter.includes(evt.code.toLowerCase())) && filterEvt.length > 0) return;
|
||||
let user = discord.users.cache.get(row.userid);
|
||||
if (!user) return console.log(`${colors.red("[ERROR]")} User ${row.userid} not found`);
|
||||
|
||||
|
@ -842,35 +792,30 @@ discord.on("interactionCreate", async (interaction) => {
|
|||
filterEvt = interaction.options.getString("filterevt") || null;
|
||||
message = interaction.options.getString("message") || null;
|
||||
if (interaction.inGuild()) {
|
||||
interaction.channel.send("Permission check").then((msg) => {
|
||||
msg.delete();
|
||||
db.get(`SELECT * FROM channels WHERE channelid = ? AND iemchannel = ?`, [interaction.channel.id, room], (err, row) => {
|
||||
db.get(`SELECT * FROM channels WHERE channelid = ? AND iemchannel = ?`, [interaction.channel.id, room], (err, row) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
interaction.reply({ content: "Failed to subscribe to room", ephemeral: true });
|
||||
} /*else if (row) {
|
||||
return interaction.reply({ content: `Already subscribed to \`${getWFOByRoom(room).location}\`\nIf you want to update a subscribtion, please unsubscribe and resubscribe. This will be made a command eventually.`, ephemeral: true });
|
||||
}*/
|
||||
db.run(`INSERT INTO channels (channelid, iemchannel, custommessage, filter, filterEvt, minPriority) VALUES (?, ?, ?, ? ,? ,?)`, [interaction.channel.id, room, message, filter, filterEvt, minPriority], (err) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
interaction.reply({ content: "Failed to subscribe to room", ephemeral: true });
|
||||
} else if (row) {
|
||||
return interaction.reply({ content: `Already subscribed to \`${getWFOByRoom(room).location}\`\nIf you want to update a subscribtion, please unsubscribe and resubscribe. This will be made a command eventually.`, ephemeral: true });
|
||||
} else {
|
||||
interaction.reply({ content: `Subscribed to \`${getWFOByRoom(room).location}\``, ephemeral: true });
|
||||
}
|
||||
db.run(`INSERT INTO channels (channelid, iemchannel, custommessage, filter, filterEvt, minPriority) VALUES (?, ?, ?, ? ,? ,?)`, [interaction.channel.id, room, message, filter, filterEvt, minPriority], (err) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
interaction.reply({ content: "Failed to subscribe to room", ephemeral: true });
|
||||
} else {
|
||||
interaction.reply({ content: `Subscribed to \`${getWFOByRoom(room).location}\``, ephemeral: true });
|
||||
}
|
||||
});
|
||||
});
|
||||
}).catch((err) => {
|
||||
interaction.reply({ content: "Failed to subscribe to room. Bot does not have send message permissions here!", ephemeral: true });
|
||||
});
|
||||
} else { // We're in a DM
|
||||
db.get(`SELECT * FROM userAlerts WHERE userid = ? AND iemchannel = ?`, [interaction.user.id, room], (err, row) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
interaction.reply({ content: "Failed to subscribe to room", ephemeral: true });
|
||||
} else if (row) {
|
||||
} /*else if (row) {
|
||||
return interaction.reply({ content: `Already subscribed to \`${getWFOByRoom(room).location}\`\nIf you want to update a subscribtion, please unsubscribe and resubscribe. This will be made a command eventually.`, ephemeral: true });
|
||||
}
|
||||
}*/
|
||||
db.run(`INSERT INTO userAlerts (userid, iemchannel, custommessage, filter, filterEvt, minPriority) VALUES (?, ?, ?, ? ,?, ?)`, [interaction.user.id, room, message, filter, filterEvt, minPriority], (err) => {
|
||||
if (err) {
|
||||
console.error(err.message);
|
||||
|
|
Loading…
Reference in a new issue