【问题标题】:Discord.js GuildOnly commandDiscord.js GuildOnly 命令
【发布时间】:2022-01-27 07:15:33
【问题描述】:

我怎样才能像这样创建一个命令 guildOnly? https://cdn.discordapp.com/attachments/824411059443204127/925437730895306802/unknown.png

这就是我的命令处理程序的样子

import { Command } from './@types'
let commands: Map<string, any> = new Map()
let aliases: Map<string, any> = new Map()
const commandFolder = fs.readdirSync(path.resolve(__dirname, `./commands`))
for (const folder of commandFolder) {
  const commandFile = fs.readdirSync(path.resolve(__dirname, `./commands/${folder}`)).filter(file => file.endsWith('.ts'));
  for (const file of commandFile) {
    const cmd = require(`./commands/${folder}/${file}`) as Command;
    console.log(`${cmd.name} in ${folder}`);
    commands.set(cmd.name, cmd);
    if (!cmd.aliases) continue;
    if (cmd.aliases.length > 0) for (const alias of cmd.aliases) { console.log(`${alias} configed to ${cmd.name}`); aliases.set(alias, cmd); }
  }
}

【问题讨论】:

  • 您使用的是基于文本的命令,而不是较新的斜杠命令,对吧?
  • 我还有一个斜杠命令处理程序``` let slashes: Discord.Collection = new Discord.Collection(); client.on('ready', () => { fs.readdirSync(${__dirname}/slash-commands).forEach((dir) => { fs.readdirSync(${__dirname}/slash-commands/${dir}).forEach(async (cmd) => { 常量文件= await import(${__dirname}/slash-commands/${dir}/${cmd}); await client.guilds.cache.get("875064681079898122")?.commands.create(file) await client.application?.commands.create(file); slashes.set(file.name , 文件) }) }) }) ````
  • if(cmd.guild &amp;&amp; message.guild.id !== GUILD_ID) return; 如果没有互动。
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: node.js typescript discord.js


【解决方案1】:

如果使用基于client#messaageCreate 事件的基于message 的命令,简单的方法是在guildOnly 设置为真时断言message?.guild 存在:

if (command.guildOnly && !message.guild) {
    return
}

使用斜杠命令时可以直接注册guild slash commands。或者在创建global slash commands时,可以在options param中设置channel_types,使channel_types只在公会中显示

【讨论】:

    猜你喜欢
    • 2020-12-04
    • 2021-06-01
    • 2021-03-29
    • 2022-01-25
    • 2021-05-20
    • 2021-04-18
    • 2021-06-11
    • 2020-04-11
    • 2017-07-31
    相关资源
    最近更新 更多