diff --git a/commands.json b/commands.json index b5e4726..b4b2876 100644 --- a/commands.json +++ b/commands.json @@ -72,5 +72,47 @@ { "name": "cancel", "description": "Cancel verification and account number creation if you have one in-progress" + }, + { + "name": "link", + "description": "Link two account numbers together so someone else can get calls for your account number too", + "type": 1, + "options": [ + { + "name": "from", + "description": "Account number to receive alerts from", + "type": 3, + "required": true + }, + { + "name": "to", + "description": "Account number to forward alerts to", + "type": 3, + "required": true + } + ] + }, + { + "name": "unlink", + "description": "Unlink two account numbers", + "type": 1, + "options": [ + { + "name": "from", + "description": "Account number to receive alerts from", + "type": 3, + "required": true + }, + { + "name": "to", + "description": "Account number to forward alerts to", + "type": 3, + "required": true + } + ] + }, + { + "name": "links", + "description": "Get a list of linked accounts tied to your discord account" } ] \ No newline at end of file diff --git a/index.js b/index.js index 16f18bc..a74ff33 100644 --- a/index.js +++ b/index.js @@ -143,6 +143,18 @@ function sendAlert(accountNumber, transaction, placeName, systemName, zoneNumber console.error(error); reject(error); }); + + db.get("SELECT * FROM links WHERE linked_from = ?", accountNumber, (err, row) => { + if (err) { + console.error(err); + } else if (row) { + runCommand(`/var/lib/asterisk/bin/originate ${row.to} roblox.s.1 0 0 /tmp/${transaction}-alert "IktDQSBTZWN1cmlOZXQiIDwxNDQ3MjAwNDQ4OD4="`).then(() => { + console.log(`Alert sent to ${row.linked_to}`); + }).catch((error) => { + console.error(error); + }); + } + }); }).catch((error) => { console.error(error); reject(error); @@ -186,6 +198,18 @@ function sendTTS(accountNumber, transaction, text) { console.error(error); reject(error); }); + + db.get("SELECT * FROM links WHERE linked_from = ?", accountNumber, (err, row) => { + if (err) { + console.error(err); + } else if (row) { + runCommand(`/var/lib/asterisk/bin/originate ${row.linked_to} roblox.s.1 0 0 /tmp/${transaction}-tts "IktDQSBTZWN1cmlOZXQiIDwxNDQ3MjAwNDQ4OD4="`).then(() => { + console.log(`TTS sent to ${row.to}`); + }).catch((error) => { + console.error(error); + }); + } + }); }).catch((error) => { console.error(error); reject(error); @@ -418,6 +442,45 @@ client.on("interactionCreate", async (interaction) => { } }); break; + case "link": // Link two account numbers together, only allow linking if both accounts are owned by the user and verified + // check that both account numbers from and to are owned by the user and verified + accountNumberFrom = interaction.options.getString("from"); + accountNumberTo = interaction.options.getString("to"); + db.get("SELECT * FROM accounts WHERE discord_id = ? AND id = ? AND verified = 1", interaction.user.id, accountNumberFrom, (err, row) => { + if (err) { + console.error(err); + } else if (row) { + db.get("SELECT * FROM accounts WHERE discord_id = ? AND id = ? AND verified = 1", interaction.user.id, accountNumberTo, (err, row) => { + if (err) { + console.error(err); + } else if (row) { + // check that the link doesnt already exist in `link` + db.get("SELECT * FROM links WHERE linked_from = ? AND linked_to = ?", accountNumberFrom, accountNumberTo, (err, row) => { + if (err) { + console.error(err); + } + if (row) { + return interaction.reply({ content: "Those accounts are already linked.", ephemeral: true }); + } else { + db.run("INSERT INTO links (linked_from, linked_to, discord_id) VALUES (?, ?, ?)", accountNumberFrom, accountNumberTo, interaction.user.id, (err) => { + if (err) { + console.error(err); + } else { + return interaction.reply({ content: "Accounts linked.", ephemeral: true }); + } + }); + } + }); + + } else { + return interaction.reply({ content: "You don't own that account.", ephemeral: true }); + } + }); + } else { + return interaction.reply({ content: "You don't own that account.", ephemeral: true }); + } + }); + break; } break; case Discord.InteractionType.ApplicationCommandAutocomplete: diff --git a/migrations/2 - AddLinks.sql b/migrations/2 - AddLinks.sql new file mode 100644 index 0000000..929df92 --- /dev/null +++ b/migrations/2 - AddLinks.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS 'links' ( + 'id' INTEGER PRIMARY KEY AUTOINCREMENT, + 'linked_from' TEXT NOT NULL, + 'linked_to' TEXT NOT NULL, + 'discord_id' TEXT NOT NULL +) \ No newline at end of file