【问题标题】:How to delete slash commands in Discord.js v13如何在 Discord.js v13 中删除斜杠命令
【发布时间】:2021-11-09 06:18:31
【问题描述】:

const { glob } = require("glob");
const { promisify } = require("util");
const { Client } = require("discord.js");
const mongoose = require("mongoose");

const globPromise = promisify(glob);

/**
 * @param {Client} portle
 */
module.exports = async (portle) => {
    const commandFiles = await globPromise(`${process.cwd()}/commands/**/*.js`);
    commandFiles.map((value) => {
        const file = require(value);
        const splitted = value.split("/");
        const directory = splitted[splitted.length - 2];

        if (file.name) {
            const properties = { directory, ...file };
            portle.commands.set(file.name, properties);
        }
    });

    const eventFiles = await globPromise(`${process.cwd()}/events/*.js`);
    eventFiles.map((value) => require(value));

    const slashCommands = await globPromise(
        `${process.cwd()}/slash-cmds/*/*.js`
    );

    const arrayOfSlashCommands = [];
    slashCommands.map((value) => {
        const file = require(value);
        if (!file?.name) return;
        portle.slashCommands.set(file.name, file);

        if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
        arrayOfSlashCommands.push(file);
    });

    portle.on("ready", async () => {
        await portle.guilds.cache
            .get("884380331170484244")
            .commands.set(arrayOfSlashCommands);
    });

    const mongooseURI = process.env.URI;
    if (!mongooseURI) throw new Error("Unspecified mongoose connection string!");

    mongoose.connect(mongooseURI).then(() => console.log('Connected to mongodb'));
};

我刚开始学习如何制作斜杠命令,当我制作一个时,我重新启动了我的机器人Image of my commands,现在我的一个斜杠命令被复制了。我将如何删除重复项?

下面的命令和事件处理程序代码,我在 yt 教程中找到的。

【问题讨论】:

  • 欢迎来到 SO。请附上实际代码。

标签: discord.js


【解决方案1】:

我怀疑如果您按照他们的教程进行操作,您希望直接通过 REST 客户端执行此操作 - 这是一种删除特定公会的所有斜杠命令的方法。

require('dotenv').config();

const { SlashCommandBuilder } = require('@discordjs/builders');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');

const token = process.env.TOKEN;
const clientId = process.env.CLIENT_ID;
const guildId = process.env.TEST_GUILD_ID;
    
const rest = new REST({ version: '9' }).setToken(token);
rest.get(Routes.applicationGuildCommands(clientId, guildId))
    .then(data => {
        const promises = [];
        for (const command of data) {
            const deleteUrl = `${Routes.applicationGuildCommands(clientId, guildId)}/${command.id}`;
            promises.push(rest.delete(deleteUrl));
        }
        return Promise.all(promises);
    });

要为您的全局命令执行此操作,只需使用 Routes.applicationCommands(clientId) 而不是 Routes.applicationGuildCommands(clientId, guildId)

【讨论】:

  • note promise.all 通常不太好用,但可以很好地用于测试目的 - 如果您要集成到构建管道中,则需要对此进行改进
  • 为什么promise.all不好?
  • 我想在这里和许多应用程序中都可以@LuisAFK,但我倾向于谨慎使用它,通常只用于读取操作,因为它在第一次失败时返回并且不会等待剩余的结果引导你到一个潜在的未知状态。如果发生这种情况时您处于数据库事务中,您将无法非常轻松地处理故障,因为其他操作仍在进行中,回滚可能会失败。
【解决方案2】:

您可以简单地使用ApplicationCommand#delete 方法来删​​除您的斜杠命令,如何?首先让我们像这样获取command / ApplicationCommand 对象:


    client.application.commands.fetch('123456789012345678') // id of your command
      .then( (command) => {
    console.log(`Fetched command ${command.name}`)
    // further delete it like so:
    command.delete()
    console.log(`Deleted command ${command.name}`)
    }).catch(console.error);

这也存在于ApplicationCommandManager#delete 方法中,更简单!您可以简单地获取命令的 ID 并将其传递给管理器的方法,如下所示:

<guild>.commands.delete('123456789012345678')

【讨论】:

  • 如何获取斜杠命令的 id?
  • @Lumins 它是一个映射函数,但你可以这样做: guild.commands.cache.forEach((value, key) => { }) 键是你的 id 或做 value.id
  • 嗨,我使用这个库来获取命令 ID npmjs.com/package/discord-slash-commands-client
  • 最佳答案 imo
  • 要获取斜线命令的 ID,它的 interaction.commandId
猜你喜欢
  • 2022-01-07
  • 2022-01-09
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 2021-11-17
  • 2021-12-15
  • 1970-01-01
  • 2022-07-05
相关资源
最近更新 更多