Discord chat bot to announce projects (#590)
Add support for discord chat bot to announce when images are set, with instructions on configuring for dev. This uses the REST API instead of a websocket connection, which is needed for serverless deployment. Co-authored-by: Kurt Hutten <k.hutten@protonmail.ch>
This commit is contained in:
@@ -6,9 +6,10 @@
|
||||
"@redwoodjs/api": "^0.38.1",
|
||||
"@redwoodjs/graphql-server": "^0.38.1",
|
||||
"@sentry/node": "^6.5.1",
|
||||
"axios": "^0.21.1",
|
||||
"axios": "^0.25.0",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
34
app/api/src/lib/discord.ts
Normal file
34
app/api/src/lib/discord.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import axios from 'axios'
|
||||
import {Client, Intents, MessageAttachment} from "discord.js"
|
||||
|
||||
let inst = null;
|
||||
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 {
|
||||
inst = axios.create({
|
||||
baseURL: 'https://discord.com/api'
|
||||
});
|
||||
inst.defaults.headers.common['Authorization'] = `Bot ${process.env.DISCORD_TOKEN}`
|
||||
console.log(`Discord: using API token ${process.env.DISCORD_TOKEN}`);
|
||||
}
|
||||
|
||||
export async function sendDiscordMessage(text: string, url?: string) {
|
||||
if (!inst) {
|
||||
console.error(`Discord: not configured to send message ("${text}")`);
|
||||
} else {
|
||||
const API_URL = `/channels/${process.env.DISCORD_CHANNEL_ID}/messages`;
|
||||
if (url) {
|
||||
return inst.post(API_URL, { embeds: [{
|
||||
title: text,
|
||||
image: {
|
||||
url: url,
|
||||
},
|
||||
}] });
|
||||
} else {
|
||||
return inst.post(API_URL, {
|
||||
content: text,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,8 @@ import {
|
||||
} from 'src/services/helpers'
|
||||
import { requireAuth } from 'src/lib/auth'
|
||||
import { requireOwnership, requireProjectOwnership } from 'src/lib/owner'
|
||||
import { sendDiscordMessage } from 'src/lib/discord'
|
||||
|
||||
|
||||
export const projects = ({ userName }) => {
|
||||
if (!userName) {
|
||||
@@ -243,7 +245,19 @@ export const updateProjectImages = async ({
|
||||
const [updatedProject] = await Promise.all([
|
||||
projectPromise,
|
||||
imageDestroyPromise,
|
||||
])
|
||||
]).then(async (result) => {
|
||||
const { userName } = await db.user.findUnique({
|
||||
where: { id: project.userId },
|
||||
})
|
||||
sendDiscordMessage([
|
||||
`${userName} just added an image to their ${project.cadPackage} project:`,
|
||||
` => ${project.title}`,
|
||||
``,
|
||||
`Check it out, leave a comment, make them feel welcome!`,
|
||||
`https://cadhub.xyz/u/${userName}/${project.title}`
|
||||
].join('\n'), `https://res.cloudinary.com/irevdev/image/upload/c_scale,w_700/v1/${mainImage}`)
|
||||
return result
|
||||
})
|
||||
return updatedProject
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user