不和谐论坛"/>
将文件信息传输到不和谐论坛
我想做的是从我的本地硬盘驱动器获取文件并将它们传输到 discord 论坛,但是当我运行它时,我的终端没有收到任何错误消息来告诉我项目中是否有问题而且我不知道我是否设置了 ipc main 以正确收听渲染器,或者文件夹路径是否不正确。
// main.js
// Modules to control application life and create native browser window
const { channel } = require('diagnostics_channel')
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path')
const { Client, GatewayIntentBits } = require('discord.js')
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
// ...
]
})
const fs = require('fs')
const createWindow = () => {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
// Open the DevTools.
mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
client.login('token here')
createWindow()
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`)
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
ipcMain.on('upload-folder', (event, folderPath) => {
const channel = client.channels.cache.get('channel ID here')
if (!channel) return console.error('Invalid channel ID');
fs.readdir(folderPath, (err, files) => {
if (err) return event.reply('upload-folder-reply', err.message)
files.forEach((file) => {
const filePath = path.join(folderPath, file);
channel.send({
files: [filePath]
})
.then(() => {
const message = `Uploaded file: ${file}`
channel.send(message)
})
.catch((error) => event.reply('upload-folder-reply', error.message))
});
const message = `Uploaded ${files.length} file${files.length === 1 ? '' : 's'}`
channel.send(message)
})
})```
// 预加载.js
// All the Node.js APIs are available in the preload process.
// It has the same sandbox as a Chrome extension.
window.addEventListener('DOMContentLoaded', () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector)
if (element) element.innerText = text
}
for (const dependency of ['chrome', 'node', 'electron']) {
replaceText(`${dependency}-version`, process.versions[dependency])
}
})```
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<script src="./renderer.js"></script>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
We are using Node.js <span id="node-version"></span>,
Chromium <span id="chrome-version"></span>,
and Electron <span id="electron-version"></span>.
</body>
</html>
renderer.js
const { ipcRenderer } = require('electron');
const ipc = ipcRenderer;
ipcRenderer.invoke('upload-folder', 'Navcom/aar/')
.then(() => console.log('Folder uploaded successfully'))
.catch((error) => console.error(error))
应该发生的是 renderer.js 一次获取文件夹中的每个文件并将其上传到频道 ID 中的不和谐论坛但是当我测试它时它什么也没做没有错误消息什么都没有, bot 确实会上线,这是预期的,但它不会在 renderer.js 中获取选定的文件夹或在 ipc main 中上传它们
回答如下:更多推荐
将文件信息传输到不和谐论坛
发布评论