await discord notify send at top level
All checks were successful
Build and push image for doorman-homeassistant / docker (push) Successful in 25s
Build and push Doorman UI / API / docker (push) Successful in 1m56s
Build and push image for doorman-homeassistant / deploy-gitainer (push) Successful in 22s

This commit is contained in:
Martin Dimitrov 2024-11-11 19:47:33 -08:00
parent a76d61f789
commit 6cd44c2667
3 changed files with 14 additions and 3 deletions

View File

@ -39,7 +39,7 @@ exports.handler = async function(context, event, callback) {
setTimeout(() => {
console.log("Ungraceful finish: running out of time");
callback(null, response);
}, 5000);
}, 9500);
await Promise.all(promises);
return callback(null, response);

View File

@ -4,10 +4,13 @@
const { Client, GatewayIntentBits } = require("discord.js");
// TODO: cache these at top level in handler code
let conn;
let userCache = {};
exports.getDiscordClient = async (context) => {
if (!conn) {
console.log("[DiscordClientCache] cache miss for discord");
const client = new Client({
intents: [GatewayIntentBits.DirectMessages],
rest: {
@ -17,6 +20,8 @@ exports.getDiscordClient = async (context) => {
await client.login(context.DISCORD_BOT_TOKEN);
conn = client;
} else {
console.log("[DiscordClientCache] cache hit for discord");
}
return conn;
@ -30,7 +35,13 @@ exports.sendMessageToUser = async (
) => {
try {
const client = await exports.getDiscordClient(context);
const user = await client.users.fetch(userId);
if (userCache[userId] === undefined) {
console.log("[UserCache] cache miss for", userId);
userCache[userId] = await client.users.fetch(userId);
} else {
console.log("[UserCache] cache hit for", userId);
}
const user = userCache[userId];
return user.send(msg);
} catch (e) {
console.log(e);

View File

@ -93,7 +93,7 @@ exports.handler = async function(context, event, callback) {
msg + u + ')'
);
notifyDiscord(context, msgs, config.discordUsers, []);
await notifyDiscord(context, msgs, config.discordUsers, []);
let discordLock = false;
let intervals = [];