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

编程入门 行业动态 更新时间:2024-10-06 04:11:29

从<a href=https://www.elefans.com/category/jswz/34/1771393.html style=文件夹上传文件到不和谐论坛"/>

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

我想做的是从我的本地硬盘驱动器获取文件并将它们传输到 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
回答如下:

更多推荐

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

本文发布于:2024-05-30 08:18:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770292.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   上传文件   不和谐   论坛

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!