Add support for discord chat bot to announce when images are set, with instructions on configuring for dev

This commit is contained in:
Scott Martin
2022-01-09 16:03:15 -05:00
parent 35fcd55229
commit 51ba64d13e
7 changed files with 125 additions and 8 deletions

View File

@@ -9,6 +9,7 @@
"axios": "^0.21.1",
"cloudinary": "^1.23.0",
"cors": "^2.8.5",
"discord.js": "^13.5.1",
"express": "^4.17.1",
"human-id": "^2.0.1",
"middy": "^0.36.0",
@@ -21,4 +22,4 @@
"concurrently": "^6.0.0",
"nodemon": "^2.0.7"
}
}
}

View File

@@ -0,0 +1,24 @@
import {Client, Intents} from "discord.js"
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]})
export async function sendChat(text: string) {
if (!client.isReady()) {
console.error(`Discord: client is not ready to send message ("${text}")`);
} else {
const channel = await client.channels.fetch(process.env.DISCORD_CHANNEL_ID);
channel.send(text);
}
}
client.on("ready", async () => {
console.log(`Discord: logged in as ${client.user.tag}`)
await sendChat("started");
})
if (!process.env.DISCORD_TOKEN || !process.env.DISCORD_CHANNEL_ID) {
console.warn("Discord bot not configured - please set process.env.DISCORD_TOKEN and process.env.DISCORD_CHANNEL_ID to send discord chats");
} else {
console.log(`Discord: logging in (token ${process.env.DISCORD_TOKEN})`);
client.login(process.env.DISCORD_TOKEN);
}

View File

@@ -12,6 +12,7 @@ import {
} from 'src/services/helpers'
import { requireAuth } from 'src/lib/auth'
import { requireOwnership, requireProjectOwnership } from 'src/lib/owner'
import { sendChat } from 'src/lib/discord'
export const projects = ({ userName }) => {
if (!userName) {
@@ -243,7 +244,7 @@ export const updateProjectImages = async ({
const [updatedProject] = await Promise.all([
projectPromise,
imageDestroyPromise,
])
]).then(() => sendChat(`project image updated: ${id}`));
return updatedProject
}