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(() => {
|
setTimeout(() => {
|
||||||
console.log("Ungraceful finish: running out of time");
|
console.log("Ungraceful finish: running out of time");
|
||||||
callback(null, response);
|
callback(null, response);
|
||||||
}, 5000);
|
}, 9500);
|
||||||
|
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
return callback(null, response);
|
return callback(null, response);
|
||||||
|
|||||||
@ -4,10 +4,13 @@
|
|||||||
|
|
||||||
const { Client, GatewayIntentBits } = require("discord.js");
|
const { Client, GatewayIntentBits } = require("discord.js");
|
||||||
|
|
||||||
|
// TODO: cache these at top level in handler code
|
||||||
let conn;
|
let conn;
|
||||||
|
let userCache = {};
|
||||||
|
|
||||||
exports.getDiscordClient = async (context) => {
|
exports.getDiscordClient = async (context) => {
|
||||||
if (!conn) {
|
if (!conn) {
|
||||||
|
console.log("[DiscordClientCache] cache miss for discord");
|
||||||
const client = new Client({
|
const client = new Client({
|
||||||
intents: [GatewayIntentBits.DirectMessages],
|
intents: [GatewayIntentBits.DirectMessages],
|
||||||
rest: {
|
rest: {
|
||||||
@ -17,6 +20,8 @@ exports.getDiscordClient = async (context) => {
|
|||||||
|
|
||||||
await client.login(context.DISCORD_BOT_TOKEN);
|
await client.login(context.DISCORD_BOT_TOKEN);
|
||||||
conn = client;
|
conn = client;
|
||||||
|
} else {
|
||||||
|
console.log("[DiscordClientCache] cache hit for discord");
|
||||||
}
|
}
|
||||||
|
|
||||||
return conn;
|
return conn;
|
||||||
@ -30,7 +35,13 @@ exports.sendMessageToUser = async (
|
|||||||
) => {
|
) => {
|
||||||
try {
|
try {
|
||||||
const client = await exports.getDiscordClient(context);
|
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);
|
return user.send(msg);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(e);
|
console.log(e);
|
||||||
|
|||||||
@ -93,7 +93,7 @@ exports.handler = async function(context, event, callback) {
|
|||||||
msg + u + ')'
|
msg + u + ')'
|
||||||
);
|
);
|
||||||
|
|
||||||
notifyDiscord(context, msgs, config.discordUsers, []);
|
await notifyDiscord(context, msgs, config.discordUsers, []);
|
||||||
|
|
||||||
let discordLock = false;
|
let discordLock = false;
|
||||||
let intervals = [];
|
let intervals = [];
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user