Add LRN Lookup

This commit is contained in:
Christopher Cookman 2025-05-28 10:34:30 -06:00
parent 3dab86510c
commit 379dc9f714

165
index.js
View file

@ -23,7 +23,7 @@ db.push
const commands = [ const commands = [
{ {
name: "lookup", name: "cid",
description: "Lookup a CallerID", description: "Lookup a CallerID",
contexts: [0, 1, 2], contexts: [0, 1, 2],
integration_types: [0, 1], integration_types: [0, 1],
@ -40,6 +40,32 @@ const commands = [
type: 5 type: 5
} }
] ]
},
{
name: "lrn",
description: "Lookup a Local Routing Number",
contexts: [0, 1, 2],
integration_types: [0, 1],
options: [
{
name: "did",
description: "The number to look up (11 digit US only!)",
type: 4,
required: true
},
{
name: "ani",
description: "The ANI to use for the lookup. i.e the caller's number (for routing info) (11 digit US only!)",
type: 4,
required: false
},
{
name: "show",
description: "Whether to show the response to everyone or not.",
type: 5,
required: false
}
]
} }
] ]
@ -57,36 +83,119 @@ Client.on("ready", async () => {
}) })
Client.on("interactionCreate", async (interaction) => { Client.on("interactionCreate", async (interaction) => {
switch(interaction.type) { switch (interaction.type) {
case Discord.InteractionType.ApplicationCommand: case Discord.InteractionType.ApplicationCommand:
let show = !interaction.options.getBoolean("show"); switch (interaction.commandName) {
if (show == null) show = true; case "cid":
let number = interaction.options.getInteger("number") let show = !interaction.options.getBoolean("show");
if (!/^\d{11}$/.test(number)) return interaction.reply({ephemeral: true, content: "<:invalid:1305271064993071185> That's not a valid number! (11 digit US only)"}); if (show == null) show = true;
if (await db.exists(`/cache/${number}`)) { let number = interaction.options.getInteger("number")
cached = await db.getData(`/cache/${number}`); if (!/^\d{11}$/.test(number)) return interaction.reply({ ephemeral: true, content: "<:invalid:1305271064993071185> That's not a valid number! (11 digit US only)" });
} else { if (await db.exists(`/cache/${number}`)) {
cached = {} cached = await db.getData(`/cache/${number}`);
} } else {
if (!cached.cnam || new Date() > new Date(cached?.expires)) { cached = {}
// Old, get from API }
interaction.reply({ephemeral: show, content: `<:checking:1305271116134092842> Looking up \`${number}\``}).then(() => { if (!cached.cnam || new Date() > new Date(cached?.expires)) {
axios.get(process.env.API_URL.replaceAll("%n", number)).then((response) => { // Old, get from API
db.push(`/cache/${number}`, { interaction.reply({ ephemeral: show, content: `<:checking:1305271116134092842> Looking up \`${number}\`` }).then(() => {
timestamp: new Date(), axios.get(process.env.CNAM_API_URL.replaceAll("%n", number)).then((response) => {
expires: new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000), db.push(`/cache/${number}`, {
cnam: response.data timestamp: new Date(),
expires: new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000),
cnam: response.data
});
interaction.editReply({ ephemeral: show, content: `<:success:1305271084806963220> CNAM for \`${number}\` is \`${response.data}\`` })
}).catch((err) => {
interaction.editReply({ ephemeral: show, content: `<:server_error:1305271074551758868> There was a server error when trying to get that!` })
})
}); });
interaction.editReply({ephemeral: show, content: `<:success:1305271084806963220> CNAM for \`${number}\` is \`${response.data}\``}) } else {
}).catch((err) => { // Spit out cached version
interaction.editReply({ephemeral: show, content: `<:server_error:1305271074551758868> There was a server error when trying to get that!`}) interaction.reply({ ephemeral: show, content: `<:cached:1305276187966443612> CNAM for \`${number}\` is \`${cached.cnam}\`\n-# This was cached <t:${Math.floor(cached.timestamp / 1000)}:f>` })
}) }
}); break;
} else {
// Spit out cached version case "lrn":
interaction.reply({ephemeral: show, content: `<:cached:1305276187966443612> CNAM for \`${number}\` is \`${cached.cnam}\`\n-# This was cached <t:${Math.floor(cached.timestamp / 1000)}:f>`}) let showLrn = !interaction.options.getBoolean("show");
if (showLrn == null) showLrn = true;
let did = interaction.options.getInteger("did")
if (!/^\d{11}$/.test(did)) return interaction.reply({ ephemeral: true, content: "<:invalid:1305271064993071185> That's not a valid number! (11 digit US only)" });
let ani = interaction.options.getInteger("ani") || null;
if (ani && !/^\d{11}$/.test(ani)) {
return interaction.reply({ ephemeral: true, content: "<:invalid:1305271064993071185> That's not a valid ANI! (11 digit US only)" });
}
if (await db.exists(`/lrncache/${did}`)) {
cached = await db.getData(`/lrncache/${did}`);
} else {
cached = {}
}
if (!cached.lrn || new Date() > new Date(cached?.expires)) {
// Old, get from API
interaction.reply({ ephemeral: showLrn, content: `<:checking:1305271116134092842> Looking up \`${did}\`` }).then(() => {
axios.get(process.env.LRN_API_URL.replaceAll("%did", did).replaceAll("%ani", ani || "")).then((response) => {
let lrn = response.data;
/*lrn data:
{"tn":"15809197945","lrn":"15809197945","ocn":"5813","lata":"536","city":"LAWTON","ratecenter":"LAWTON","province":"OK","jurisdiction":"INDETERMINATE","local":"INDETERMINATE","lec":"CELLCO PARTNERSHIP DBA VERIZON WIRELESS - OK","lectype":"WIRELESS","spid":"6006","activation":"1241582400"}
*/
db.push(`/lrncache/${did}`, {
timestamp: new Date(),
expires: new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000), // 7 days
lrn
});
interaction.editReply({
ephemeral: showLrn, embeds: [{
title: `LRN for ${did}`,
fields: [
{ name: "LRN", value: `\`${lrn.lrn}\`` || "`NULL`", inline: true },
{ name: "OCN", value: `\`${lrn.ocn}\`` || "`NULL`", inline: true },
{ name: "LATA", value: `\`${lrn.lata}\`` || "`NULL`", inline: true },
{ name: "City", value: `\`${lrn.city}\`` || "`NULL`", inline: true },
{ name: "Rate Center", value: `\`${lrn.ratecenter}\`` || "`NULL`", inline: true },
{ name: "Province", value: `\`${lrn.province}\`` || "`NULL`", inline: true },
{ name: "Jurisdiction", value: `\`${lrn.jurisdiction}\`` || "`NULL`", inline: true },
{ name: "Local", value: `\`${lrn.local}\`` || "`NULL`", inline: true },
{ name: "LEC", value: `\`${lrn.lec}\`` || "`NULL`", inline: true },
{ name: "LEC Type", value: `\`${lrn.lectype}\`` || "`NULL`", inline: true },
{ name: "SPID", value: `\`${lrn.spid}\`` || "`NULL`", inline: true },
{ name: "Activation Date", value: `<t:${Math.floor(lrn.activation)}>` }
],
footer: { text: "This data is provided by the LRN API" },
color: 0x00FF00
}]
})
}).catch((err) => {
console.error(err);
interaction.editReply({ ephemeral: showLrn, content: `<:server_error:1305271074551758868> There was a server error when trying to get that!` })
})
});
} else {
// Spit out cached version
let lrn = cached.lrn;
interaction.reply({
ephemeral: showLrn, embeds: [{
title: `LRN for ${did}`,
fields: [
{ name: "LRN", value: `\`${lrn.lrn}\`` || "`NULL`", inline: true },
{ name: "OCN", value: `\`${lrn.ocn}\`` || "`NULL`", inline: true },
{ name: "LATA", value: `\`${lrn.lata}\`` || "`NULL`", inline: true },
{ name: "City", value: `\`${lrn.city}\`` || "`NULL`", inline: true },
{ name: "Rate Center", value: `\`${lrn.ratecenter}\`` || "`NULL`", inline: true },
{ name: "Province", value: `\`${lrn.province}\`` || "`NULL`", inline: true },
{ name: "Jurisdiction", value: `\`${lrn.jurisdiction}\`` || "`NULL`", inline: true },
{ name: "Local", value: `\`${lrn.local}\`` || "`NULL`", inline: true },
{ name: "LEC", value: `\`${lrn.lec}\`` || "`NULL`", inline: true },
{ name: "LEC Type", value: `\`${lrn.lectype}\`` || "`NULL`", inline: true },
{ name: "SPID", value: `\`${lrn.spid}\`` || "`NULL`", inline: true },
{ name: "Activation Date", value: `<t:${Math.floor(lrn.activation)}>` }
],
footer: { text: "This is cached data." },
color: 0x00FF00
}]
})
}
break;
} }
break;
} }
}); });