discord.js-commando宣布命令

时间:2020-09-13 02:12:22

标签: discord.js

announce.js命令遇到一些小问题。

我已经尝试了几种不同的方法,但是对于突击队来说通常是新手。

这是我目前拥有的。

Announce.js

const discord = require('discord.js');
const Commando = require('discord.js-commando');

module.exports = class AnnounceCommand extends Commando.Command {
  constructor(client) {
    super(client, {
      name: 'announce',
      aliases: ['ann'],
      group: 'moderation',
      memberName: 'announce',
      userPermissions: ['MANAGE_MESSAGES', 'MANAGE_CHANNELS'],
      description: 'Send an announcement to the specified channel',
      examples: ['announce Hello, world!'],
      args: [
        {
          key: 'text',
          prompt: 'What would you like the bot to announce?',
          type: 'string',
        },
      ],
    });
  }

  run(msg, { text }) {
    let channel = msg.mentions.channels.first();
    if (!channel) return;
    const embed = new discord.RichEmbed()
      .setAuthor(`Megumin`, `https://cdn.discordapp.com/avatars/735785909420556370/3db81cc7fb540907d7a147fb87118217.png?size=2048`)
      .setThumbnail(`https://cdn.discordapp.com/avatars/735785909420556370/3db81cc7fb540907d7a147fb87118217.png?size=2048`)
      .setColor(0xffcc00)
      .addField(`Announcement`, (text), false)
      .setFooter(`Megumin`, `https://cdn.discordapp.com/avatars/735785909420556370/3db81cc7fb540907d7a147fb87118217.png?size=2048`)
      .setTimestamp();
    return channel.send(embed);
  }
};

现在它将通知发送到指定的频道,但是有两个小问题。

  1. 它不会发送到公告频道
  2. 它提到了消息中的频道。

如果有人可以向我解释我将如何解决这两个问题,我将不胜感激。

如果有帮助,这是我的index.js

const { Client } = require('discord.js-commando');
const path = require('path');
const {token, owner_id, prefix} = require("./config.json");

const client = new Client({
    commandPrefix: prefix,
    owner: owner_id,
    invite: 'https://discord.io/NewHorizonDevelopment',
})

client.registry
    .registerDefaultTypes()
    .registerGroups([
        ['misc', 'Misc'],
        ['moderation', 'Moderation'],
        ['fun', 'Fun']
    ])
    .registerDefaultGroups()
    .registerDefaultCommands()
    .registerCommandsIn(path.join(__dirname, 'commands'))

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}(${client.user.id})`)
    client.user.setActivity(`${prefix}help`, {
        type: "STREAMING",
        url: "https://www.twitch.tv/discord"
    });
})

client.on('guildMemberAdd', (guildMember) => {
    guildMember.addRole(guildMember.guild.roles.find(role => role.name === "Member"));
});

client.on('error', console.error)


client.login(token)

我正在使用node.js 12.x,如果有帮助,请使用discord.js-commando

1 个答案:

答案 0 :(得分:0)

没关系,我设法将其修复。

这是我的工作代码,适合那些可能想要自己尝试的人:

Announce.js

const discord = require('discord.js');
const Commando = require('discord.js-commando');

module.exports = class AnnounceCommand extends Commando.Command {
  constructor(client) {
    super(client, {
      name: 'announce',
      aliases: ['ann'],
      group: 'moderation',
      memberName: 'announce',
      userPermissions: ['MANAGE_MESSAGES', 'MANAGE_CHANNELS'],
      description: 'Send an announcement to the specified channel',
      examples: ['announce Hello, world!'],
      args: [
        {
          key: 'text',
          prompt: 'What would you like the bot to announce?',
          type: 'string',
        },
      ],
    });
  }

  run(msg, { text }) {
    let channel = msg.guild.channels.find(c => c.name === "updates")
    const embed = new discord.RichEmbed()
      .setAuthor(`Megumin`, `https://cdn.discordapp.com/avatars/735785909420556370/3db81cc7fb540907d7a147fb87118217.png?size=2048`)
      .setThumbnail(`https://cdn.discordapp.com/avatars/735785909420556370/3db81cc7fb540907d7a147fb87118217.png?size=2048`)
      .setColor(0xffcc00)
      .addField(`Announcement`, (text), false)
      .setFooter(`Megumin`, `https://cdn.discordapp.com/avatars/735785909420556370/3db81cc7fb540907d7a147fb87118217.png?size=2048`)
      .setTimestamp();
    return channel.send(embed);
  }
};