discord.js 12 有什么方法可以将数据从 json 文件发送到频道?
discord.js 12 any way to send data from json file to channel?
我有 json 文件,其中包含一些玩家的信息,看起来像这样
{
"208505383361314816": {
"warns": 8,
"reason": "test"
},
"776387838350196756": {
"warns": 99,
"reason": ""
}
}
然后我根据警告的数量对信息进行排序。它完美运行,但我不知道如何发送消息。
client.on('message', message => {
if (message.content.startsWith('>topw')) {
const sorted = [];
const keys = Object.keys(warns)
for (let user in warns) {
const warny = warn[user].warns;
const entry = {
[keys[sorted.length]]: warns[user]
}
if (sorted.length === 0) {
sorted.push(entry);
continue;
}
let i = 0;
while (sorted[i] !== undefined && sorted[i][Object.keys(sorted[i])].warns > warny) {
i++;
}
sorted.splice(i, 0, entry)
}
console.log(sorted)
}
})
它应该看起来像一个“排行榜”,但有很多警告
e.g:
name: Bob
warns: 20
reason: test
欢迎使用 Whosebug! :)
关于您的 JSON 文件
首先,关于您存储数据的方式的快速建议:您真的、真的应该将用户信息存储在一个数组中,它看起来像这样:
[
{
"id": "00000000",
"warns": 10,
"reason": "test"
}
]
准备好数据
话虽这么说,让我们来回答你的问题。
我猜你喜欢使用 MessageEmbed
s,在阅读代码之前先看看文档,这可能会有所帮助。我也会重写你的代码。
重新格式化
为了让我们的生活更轻松,我们将重新格式化数据以便对其进行排序。
// It's just your JSON file here
const oldWarns = {
"208505383361314816": {
"warns": 8,
"reason": "test"
},
"776387838350196756": {
"warns": 99,
"reason": ""
}
}
let newWarns = Object.keys(oldWarns).map((e) => ({
id: e,
warns: oldWarns[e].warns,
reason: oldWarns[e].reason
}))
console.log(newWarns)
正在排序
现在我们已经有了这个漂亮的数组,让我们对它进行排序。它比你想象的要简单得多!
// Precedently generated data
let newWarns = [{
"id": "208505383361314816",
"warns": 8,
"reason": "test"
},
{
"id": "776387838350196756",
"warns": 99,
"reason": ""
}
]
newWarns.sort((a, b) => (a.warns > b.warns ? 1 : -1))
console.log(newWarns)
正在发送数据
您实际要求的内容。
使用嵌入渲染它
好的,一切就绪,我们可以发送数据了。以下是如何在 Discord.js.
中使用嵌入
const warnBoard = new Discord.MessageEmbed()
.setTitle("Warnboard")
.setColor("#FF0000")
.addFields(newWarns.flatMap((e, i) => ( // flatMap will delete all the [] of the array after mapping it
i >= 25 ? [] : { // the max number of fields in embeds is 25
name: e.id,
value: e.warns,
inline: true // changes the layout
}
)))
我只是使用了 MessageEmbed 的一些功能。它们可以非常好看,所以随意摆弄它的道具!
尽可能简单
嵌入太多了?您也可以只发送一个字符串。
const stringifiedData = JSON.stringify(newWarns, null, 4)
const warnBoard = "```json\n"+ stringifiedData +"\n```"
发送
无论您选择的解决方案是字符串还是嵌入,这都是您发送数据的方式。
message.channel.send(warnBoard)
我有 json 文件,其中包含一些玩家的信息,看起来像这样
{
"208505383361314816": {
"warns": 8,
"reason": "test"
},
"776387838350196756": {
"warns": 99,
"reason": ""
}
}
然后我根据警告的数量对信息进行排序。它完美运行,但我不知道如何发送消息。
client.on('message', message => {
if (message.content.startsWith('>topw')) {
const sorted = [];
const keys = Object.keys(warns)
for (let user in warns) {
const warny = warn[user].warns;
const entry = {
[keys[sorted.length]]: warns[user]
}
if (sorted.length === 0) {
sorted.push(entry);
continue;
}
let i = 0;
while (sorted[i] !== undefined && sorted[i][Object.keys(sorted[i])].warns > warny) {
i++;
}
sorted.splice(i, 0, entry)
}
console.log(sorted)
}
})
它应该看起来像一个“排行榜”,但有很多警告
e.g:
name: Bob
warns: 20
reason: test
欢迎使用 Whosebug! :)
关于您的 JSON 文件
首先,关于您存储数据的方式的快速建议:您真的、真的应该将用户信息存储在一个数组中,它看起来像这样:
[
{
"id": "00000000",
"warns": 10,
"reason": "test"
}
]
准备好数据
话虽这么说,让我们来回答你的问题。
我猜你喜欢使用 MessageEmbed
s,在阅读代码之前先看看文档,这可能会有所帮助。我也会重写你的代码。
重新格式化
为了让我们的生活更轻松,我们将重新格式化数据以便对其进行排序。
// It's just your JSON file here
const oldWarns = {
"208505383361314816": {
"warns": 8,
"reason": "test"
},
"776387838350196756": {
"warns": 99,
"reason": ""
}
}
let newWarns = Object.keys(oldWarns).map((e) => ({
id: e,
warns: oldWarns[e].warns,
reason: oldWarns[e].reason
}))
console.log(newWarns)
正在排序
现在我们已经有了这个漂亮的数组,让我们对它进行排序。它比你想象的要简单得多!
// Precedently generated data
let newWarns = [{
"id": "208505383361314816",
"warns": 8,
"reason": "test"
},
{
"id": "776387838350196756",
"warns": 99,
"reason": ""
}
]
newWarns.sort((a, b) => (a.warns > b.warns ? 1 : -1))
console.log(newWarns)
正在发送数据
您实际要求的内容。
使用嵌入渲染它
好的,一切就绪,我们可以发送数据了。以下是如何在 Discord.js.
中使用嵌入const warnBoard = new Discord.MessageEmbed()
.setTitle("Warnboard")
.setColor("#FF0000")
.addFields(newWarns.flatMap((e, i) => ( // flatMap will delete all the [] of the array after mapping it
i >= 25 ? [] : { // the max number of fields in embeds is 25
name: e.id,
value: e.warns,
inline: true // changes the layout
}
)))
我只是使用了 MessageEmbed 的一些功能。它们可以非常好看,所以随意摆弄它的道具!
尽可能简单
嵌入太多了?您也可以只发送一个字符串。
const stringifiedData = JSON.stringify(newWarns, null, 4)
const warnBoard = "```json\n"+ stringifiedData +"\n```"
发送
无论您选择的解决方案是字符串还是嵌入,这都是您发送数据的方式。
message.channel.send(warnBoard)