English 中文(简体)
How can I make my bot works in multiple servers?
原标题:

I know that this question has been asked for discord.py, but couldn t find a discord.js one

Currently I m working on a small Discord Bot, and I would like to make it with possible that it can be used in multiple servers. I m used to the way a bot works in a single server, as I ve often made custom bots for a single server usage. But this time, as I m planning on making the bot public, I would like to make it multi-server enabled, so that multiple people can use it at the same time.

On a side note, I ve recently got access to Clyde AI on Discord, and so I used it to help me making this code :
src/deploy-commands.js

const { REST, Routes } = require( discord.js );
const { clientId, token } = require( ./../config.json );
const fs = require( node:fs );
const path = require( node:path );

const commands = [];
const commandsPath = path.join(\__dirname,  commands );
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith( .js ));

for (const file of commandFiles) {
    const filePath = path.join(commandsPath, file);
    const command = require(filePath);
    commands.push(command.data.toJSON());
}

const rest = new REST({ version:  10  }).setToken(token);

(async () => {
    try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);

        const data = await rest.put(
            Routes.applicationCommands(clientId),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    } catch (error) {
        console.error(error);
    }
})();

The problem that I ve with that code, is that it doesn t load any commands into the servers, it just says :
Started refreshing/reloaded 0 application (/) commands.

I tried to load the commands using node deploy-commands.js (which is the file that I pasted above) in the bot s servers, and I was expecting the console.log to tell me that it correctly loaded all the commands.

It s important to mention that I also removed my guildId from the config.json to enable multi-server usage.

Here are the other code snippets in case you want to see the whole code :

https://codefile.io/f/sKxpBG2uyU - index.js
https://codefile.io/f/YysJDHeJKA - track.js (slash command I m trying to load)

So to be honest, I m not sure where to look to fix my error, and where I can update my code to make it work on a multi-server usage.

If anyone of you has an idea, I will greatly take it.

I really hope someone is able to explain me how to make this work. Thanks in advance.

问题回答

fs.readdirSync does not recursively list directories. Therefore,

const commandsPath = path.join(\__dirname,  commands );
fs.readdirSync(commandsPath);

returns ["tracking"]. (from comments) You can retrieve files recursively with code like this:

const readdirSyncRecursively = (dir, files = []) => {
  const paths = fs.readdirSync(dir);
  const dirs = [];
  for (const path of paths) {
    const stats = fs.statSync(`${dir}/${path}`);
    if (stats.isDirectory()) {
      dirs.push(`${dir}/${path}`);
    } else {
      files.push(`${dir}/${path}`);
    }
  }
  for (const d of dirs) {
    files = readdirRecursively(d, files);
  }
  return files;
};

so,

const readdirSyncRecursively = (dir, files = []) => {
  const paths = fs.readdirSync(dir);
  const dirs = [];
  for (const path of paths) {
    const stats = fs.statSync(`${dir}/${path}`);
    if (stats.isDirectory()) {
      dirs.push(`${dir}/${path}`);
    } else {
      files.push(`${dir}/${path}`);
    }
  }
  for (const d of dirs) {
    files = readdirRecursively(d, files);
  }
  return files;
};

const commands = [];
const commandsPath = path.join(__dirname,  commands );
const commandFiles = readdirSyncRecursively(commandsPath).filter(file => file.endsWith( .js ));




相关问题
asp.net global resources problem

I have a weird problem with global resources in my asp.net mvc web application. I have some resources in separate project(not a web app project). Those resources have following settings: Build action:...

"Global variable" in Visual C#

I have made the Graph class, and i want to simulate a distribution network. The Graph works 100%. But, i want to use that same struct/class in all my application! For example: I have Form1 that shows ...

VB.NET - Imports namespace from other projects in Solutions

In my Solutions in Visual Studio, I have 3 projects. - Common (Root namespace: PodcastPlayer.Common) -PodcastPlayer (Root namespace: PodcastPlayer) This is the Silverlight project. -PodcastPlayer.Web (...

PHP Variable Scope

Is there a way to declare a variable so it is available in all functions. Basically I want to call: Global $varName; automatically for every function. And no, I can t use a constant. I don t think ...

global variable in ASPX C#

I write a ASPX c# page, and I MUST use a global variable like this; public static Decimal _Total; protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack == false) ...

热门标签