最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

从文件夹上传文件到不和谐论坛

网站源码admin40浏览0评论

从文件夹上传文件到不和谐论坛

从文件夹上传文件到不和谐论坛

我想做的是从我的本地硬盘驱动器获取文件并将它们传输到 discord 论坛,但是当我运行它时,我的终端没有收到任何错误消息来告诉我项目中是否有问题而且我不知道我是否设置了 ipc main 以正确收听渲染器,或者文件夹路径是否不正确。

Main.js

`// 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('MTA5OTkwOTgxMjQ0NTI1MzcxMw.G-_G1e.H15Idb50a5hwvJUQuO83PrsqhKAIocQauPy440')
  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)
    })
  })``
```
Preload.js
```

  `  // preload.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))`
    ```


What should be happening is that the renderer.js takes every file one at a time within the folder and uploads it to a discord forum within the channel ID but when I was testing this it doesn't do anything no error messages nothing, the bot does go online which is expected but it doesn't grab the selected folder in renderer.js or uploads them within the ipc main
回答如下:
发布评论

评论列表(0)

  1. 暂无评论