-
Notifications
You must be signed in to change notification settings - Fork 2
add upload photos context menu command #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v2
Are you sure you want to change the base?
Changes from 4 commits
5f658ca
ad37379
b3bbb00
dad9d23
c383087
39105c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { google } from "googleapis" | ||
| import { authorize } from "../utilities/google-auth.js" | ||
| import fs from "fs" | ||
| import { ContextMenuCommandBuilder, ApplicationCommandType } from "discord.js" | ||
| import axios from "axios" | ||
| import path from "path" | ||
|
|
||
| export const data = new ContextMenuCommandBuilder().setName("upload photos").setType(ApplicationCommandType.Message) | ||
|
|
||
| export async function execute(interaction) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a bit dangerous to allow anyone to run - I would recommend adding a check to only allow those with Administrator permission (basically the officers) as otherwise anyone could cause the bot to download arbitrary files into our disk and then into drive. |
||
| if (!interaction.isMessageContextMenuCommand()) return | ||
| const message = await interaction.channel.messages.fetch(interaction.targetId) | ||
| await interaction.deferReply() // Discord requires an acknowledgement within 3 seconds. this allows the response to be deferred, with an "<application> is thinking..." response in the meantime | ||
|
|
||
| let auth = await authorize() | ||
| const drive = google.drive({ version: "v3", auth }) | ||
| const folderId = "[FOLDER ID]" // replace this with the id of the desired folder (the part of the url after "folders/") | ||
|
||
| const downloadDir = path.join(import.meta.dirname, "..", "downloads") | ||
|
||
| let count = 0 | ||
|
|
||
| for (const [key, value] of message["attachments"]) { | ||
| if (value["contentType"].substring(0, 5) !== "image") continue | ||
| /* credit to https://github.com/ZacTimTam/Upload-To-GDrive-Discord-Bot/tree/main for the WriteStream sections */ | ||
| try { | ||
| const filePath = path.join(downloadDir, value["name"]) | ||
| const response = await axios.get(value["url"], { responseType: "stream" }) | ||
| const writer = fs.createWriteStream(filePath) | ||
| response.data.pipe(writer) | ||
| await new Promise((resolve, reject) => { | ||
| writer.on("finish", resolve) | ||
| writer.on("error", reject) | ||
| }) | ||
|
|
||
| const res = await drive.files.create({ | ||
| requestBody: { | ||
| name: value["title"], | ||
| mimeType: value["contentType"], | ||
| parents: [folderId], | ||
| }, | ||
| media: { | ||
| mimeType: value["contentType"], | ||
| body: fs.createReadStream(filePath), | ||
| }, | ||
| }) | ||
| // console.log(res.data); | ||
| await fs.promises.unlink(filePath) | ||
| count++ | ||
| } catch (error) { | ||
| console.error(`Failed to process file ${value["name"]}:`, error) | ||
|
||
| } | ||
| } | ||
| if (count === 1) { | ||
| interaction.editReply(`1 photo uploaded!`) | ||
| } else { | ||
| interaction.editReply(`${count} photos uploaded!`) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { JWT } from "google-auth-library" | ||
| import credentials_jwt from "../credentials.json" with { type: "json" }; | ||
|
|
||
| const SCOPES = [ | ||
| "https://www.googleapis.com/auth/drive" | ||
| ]; | ||
|
|
||
| export async function authorize() { | ||
| return new JWT({ | ||
| email: credentials_jwt.client_email, | ||
| key: credentials_jwt.private_key, | ||
| scopes: SCOPES, | ||
| }); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably avoid hard-coded names here - maybe like "the infrastructure lead?"