【发布时间】:2020-01-14 21:46:30
【问题描述】:
我正在尝试使用丰富的嵌入为我的机器人构建一个宣布命令。
这是我的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.exports.execute()不使用command或args参数,并且这些变量未在函数中的任何位置定义。message.mentions.channels()也不是方法;message.mentions.channels是一个返回集合的属性,所以也许你的意思是message.mentions.channels.first()。考虑一下Discord.js documentation 的概述,以帮助您入门。 -
我已经看到的第一个问题是我的不使用
module.exports.execute。它只需要module.exports = {任何使用不同格式的尝试都会破坏命令。所以我必须使它与module.exports = {一起工作,我的命令和args 变量也通过我的index.js文件调用。它设置为使用fs调用该部分,以缩短命令本身使用的代码。 -
execute被定义为module.exports的属性,因此可以表示为module.exports.execute。
标签: module discord.js fs