簡體   English   中英

如何使用 discord.js 將命令部署到所有公會?

[英]How can I deploy commands to all guilds with discord.js?

我正在編寫一個 discord 機器人,並且我有一個 deploy-commands.js 文件。 在當前的 state 中,它只能將命令部署到一個特定的公會。 我希望它向機器人所在的所有公會部署命令。我應該怎么做?

這是 deploy-commands.js 文件:

const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { clientId, guildId, token } = require('./config.json');

const commands = [];
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

console.log("Loading commands...");

for (const file of commandFiles) {
    const command = require(`./commands/${file}`)
    console.log("Loaded commands : " + command.data.name + "...");
    commands.push(command.data.toJSON());
}

console.log("Commands loaded...");

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

console.log('Successfully registered application commands to :');

rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
    .then(() => console.log(guild))
    .catch(console.error);

console.log('Done registering application commands!');```

只需更換此燈

  rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })

經過

  rest.put(Routes.applicationCommands(clientId), { body: commands })

通過刪除Guild, guildId ,您現在無需將命令發布到特定的公會,而是將它們全局發布到您的機器人上。

PS:整個服務器大約需要一個小時才能訪問這些斜杠命令,所以在部署它們之后,稍等一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM