Discord.JS宣布命令问题

时间:2019-09-13 02:04:03

标签: module discord.js fs

我正在尝试使用丰富的嵌入为我的机器人构建一个公告命令。

这是我的announce.js文件:

const Discord = require('discord.js');

module.exports = {
    name: 'announce',
    description: 'Send an announcement.',
    guildOnly: true,
    execute(message, args) {
        console.log("embedding")


        const embed = new Discord.RichEmbed()
            .setTitle("Announcement")
            .setDescription("A Staff member has sent an announcement")
            .setColor(0x00AE86)
            .setFooter("Guardian", "https://raw.githubusercontent.com/phantomdev-github/Resources/master/Discord%20Bots/Guardian/src/avatar.png")
            .setThumbnail("https://raw.githubusercontent.com/phantomdev-github/Resources/master/Discord%20Bots/Guardian/src/avatar.png")
            .setTimestamp()
            .setURL("https://github.com/phantomdev-github/Resources/tree/master/Discord%20Bots/Guardian")
            .addBlankField(true)
            .addField("Announcement", "message contents here", false))
        message.channel.send({ embed });
    }
};

自发布以来,我对其进行了重新构建,花了我一段时间才回到这篇文章。我正在尝试将我的机器人中的所有消息重建为丰富的嵌入内容。从而不同的代码。我还简化了fs命令和事件处理程序。

indexjs

const fs = require('fs');
const Discord = require('discord.js');
const client = new Discord.Client();
const { token } = require('./token.json');

client.commands = new Discord.Collection();

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

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
    console.log(file,command)
}

fs.readdir('./events/', (err, files) => {
    if (err) return console.error(err);
    files.forEach(file => {
        if(!file.endsWith('.js')) return;
        const eventFunction = require(`./events/${file}`);
        console.log(eventFunction)
        eventFunction.execute(client)
    });
});

client.login(token);

message.js

const { prefix } = require('./prefix.json');

module.exports = {
    name: 'message',
    description: 'client message event.',
    execute:function(client) {
        client.on('message',message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).split(/ +/);
    const command = args.shift().toLowerCase();
        if (!client.commands.has(command)) return;

        try {
            client.commands.get(command).execute(message, args);
            } catch (error) {
                console.error(error);
                message.reply('there was an error trying to execute that command!');
                }
})

}};

基本上,我需要知道要为"message contents here"添加什么内容,以使其输入到#announcements通道中键入的消息。

我的问题是如何将公告消息放入richEmbed的.addField部分?

会不会像这样?

const Discord = require('discord.js');

module.exports = {
    name: 'announce',
    description: 'Send an announcement to the specified channel.',
    guildOnly: true,
    execute(message, args) {
        console.log("embedding")
    enter code here
        if(args.length < 2) return /* error message */;


    let channel = message.mentions.channels.first();
    if(!channel) return ;

    let announcement = args.slice(1).join(" ");

            const embed = new Discord.RichEmbed()
            .setTitle("Notice!")
            .setDescription("Announcememnt from PhantomDEV Staff!")
            .setColor(0x00AE86)
            .setFooter("Guardian", "https://raw.githubusercontent.com/phantomdev-github/Resources/master/Discord%20Bots/Guardian/src/avatar.png")
            .setThumbnail("https://raw.githubusercontent.com/phantomdev-github/Resources/master/Discord%20Bots/Guardian/src/avatar.png")
            .setTimestamp()    
            .setURL("https://github.com/phantomdev-github/Resources/tree/master/Discord%20Bots/Guardian")
            .addBlankField(true)
            .addField("Message", "", false);

        message.channel.send({ embed });
        .catch(console.error);
};

1 个答案:

答案 0 :(得分:0)

message事件结束时,使用此行调用命令的执行...

command.execute(message, args);

定义您的execute函数以使用所需的args参数。另外,Collection.first()是您在声明channel时要寻找的方法。您的功能应该看起来像这样...

execute: function(message, args) {
  if (args.length < 2) return /* error message */;

  // Careful using this; if just an announcement is provided
  // and it mentions a channel, that channel will be used.
  let channel = message.mentions.channels.first();
  if (!channel) return /* error message */;

  let announcement = args.slice(1).join(" ");

  channel.send(announcement)
    .catch(console.error);
}

无需在执行功能中检查该命令是否为“宣布”,因为只有在执行该命令时才会被调用。