await discord notify send at top level
This commit is contained in:
parent
a76d61f789
commit
6cd44c2667
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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 = [];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user