Discord.JS宣布命令问题

问题描述 投票:0回答:1

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

这是我的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);
};
module discord.js fs
1个回答
0
投票

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

command.execute(message, args);

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

channel

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

© www.soinside.com 2019 - 2024. All rights reserved.