如何制作从消息中读取数据的嵌入?
How to make Embeds which reads the data from the message?
我正在尝试制作一个用于发布公告的 Discord 机器人。我想创建一个命令,它将从消息中读取数据并将其转换为嵌入式消息。
示例命令:!announce Title, Description, Link, Image
const Discord = require('discord.js');
const bot = new Discord.Client();
//listener
bot.on('ready', () => {
bot.user.setGame('Hello!')
});
bot.on('message', (message) => {
if(message.content == 'text') {
const embed = new Discord.RichEmbed()
.setTitle("Title!")
.setDescription("Description")
.setImage("https://i.imgur.com/xxxxxxxx.png")
.setURL("https://google.com")
.addField("Text", true)
//Nope
.setThumbnail("https://i.imgur.com/Rmiwd1j.png")
.setColor(0x00AE86)
.setFooter("Footer", "https://i.imgur.com/xxxxxxxx.png")
.setTimestamp()
/*
* Blank field, useful to create some space.
*/
message.channel.send({embed});
}});
bot.login('token');
我希望嵌入根据文本进行更改。
我该怎么做?
首先,您需要检测命令:您可以使用 String.startsWith()
来检测 !announce
:
if (message.content.startsWith('!announce')) {...}
然后你必须通过拆分字符串来获得命令的其他部分:你可以用逗号分隔它们,就像你做的那样(标题,描述,...),或者你想要的任何字符(我将在示例中使用逗号)。
String.slice()
will help you get rid of the !announce
part, while String.split()
将与其他部分一起创建一个数组。
此代码将从命令 !announce Title, Description, http://example.com/, http://www.hardwarewhore.com/wp-content/uploads/2018/03/dcord.png
生成类似 this 的嵌入
client.on("message", message => {
if (message.content.startsWith('!announce')) {
let rest_of_the_string = message.content.slice('!announce'.length); //removes the first part
let array_of_arguments = rest_of_the_string.split(','); //[title, description, link, image]
let embed = new Discord.RichEmbed()
.setTitle(array_of_arguments[0])
.setDescription(array_of_arguments[1])
.setImage(array_of_arguments[3])
.setURL(array_of_arguments[2])
.addField("Text", true)
.setThumbnail("https://i.imgur.com/Rmiwd1j.png")
.setColor(0x00AE86)
.setFooter("Footer", array_of_arguments[3])
.setTimestamp();
message.channel.send({ embed });
}
});
我建议使用文本而不是描述,并提醒 .addField()
的第二个参数是文本,而不是内联值
我正在尝试制作一个用于发布公告的 Discord 机器人。我想创建一个命令,它将从消息中读取数据并将其转换为嵌入式消息。
示例命令:!announce Title, Description, Link, Image
const Discord = require('discord.js');
const bot = new Discord.Client();
//listener
bot.on('ready', () => {
bot.user.setGame('Hello!')
});
bot.on('message', (message) => {
if(message.content == 'text') {
const embed = new Discord.RichEmbed()
.setTitle("Title!")
.setDescription("Description")
.setImage("https://i.imgur.com/xxxxxxxx.png")
.setURL("https://google.com")
.addField("Text", true)
//Nope
.setThumbnail("https://i.imgur.com/Rmiwd1j.png")
.setColor(0x00AE86)
.setFooter("Footer", "https://i.imgur.com/xxxxxxxx.png")
.setTimestamp()
/*
* Blank field, useful to create some space.
*/
message.channel.send({embed});
}});
bot.login('token');
我希望嵌入根据文本进行更改。 我该怎么做?
首先,您需要检测命令:您可以使用 String.startsWith()
来检测 !announce
:
if (message.content.startsWith('!announce')) {...}
然后你必须通过拆分字符串来获得命令的其他部分:你可以用逗号分隔它们,就像你做的那样(标题,描述,...),或者你想要的任何字符(我将在示例中使用逗号)。
String.slice()
will help you get rid of the !announce
part, while String.split()
将与其他部分一起创建一个数组。
此代码将从命令 !announce Title, Description, http://example.com/, http://www.hardwarewhore.com/wp-content/uploads/2018/03/dcord.png
client.on("message", message => {
if (message.content.startsWith('!announce')) {
let rest_of_the_string = message.content.slice('!announce'.length); //removes the first part
let array_of_arguments = rest_of_the_string.split(','); //[title, description, link, image]
let embed = new Discord.RichEmbed()
.setTitle(array_of_arguments[0])
.setDescription(array_of_arguments[1])
.setImage(array_of_arguments[3])
.setURL(array_of_arguments[2])
.addField("Text", true)
.setThumbnail("https://i.imgur.com/Rmiwd1j.png")
.setColor(0x00AE86)
.setFooter("Footer", array_of_arguments[3])
.setTimestamp();
message.channel.send({ embed });
}
});
我建议使用文本而不是描述,并提醒 .addField()
的第二个参数是文本,而不是内联值