所以我想知道是否可以做到这一点,以便如果我在config.json文件中更改并保存一个字符串,它将自动更新丰富的嵌入。我正在为服务器的队列系统执行此操作。
我已经在discord.js
服务器上尝试过,他们说的是可以的,并且没有给我进一步的帮助。
config.json
文件:
{
"prefix": "dsc",
"token": "im not showing you",
"server": "1 Day",
"bot": "",
"icon": ""
}
main.js
文件:
const Discord = require("discord.js");
const client = new Discord.Client();
const config = require("./config.json");
const { Client, MessageEmbed } = require("discord.js");
const prefix = "config.prefix";
const fs = require("fs");
client.commands = new Discord.Collection();
client.once("ready", () => {
console.log("DSC BOT is now online!");
});
client.on("message", (message) => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(" ");
const command = args.shift().toLowerCase();
//command code
if (command === "ping") {
message.channel.send("Pong!");
} else if (command === "test") {
message.channel.send(config.server);
}
});
client.on("message", (message) => {
if (message.content === "dsc test") {
message.channel.send(
"Hello There, this is a test!",
config.server,
"It should say yolo <<"
);
}
});
client.on("message", (message) => {
if (message.content === "dsc updates") {
const updates = new MessageEmbed()
.setTitle("DSC Updates")
.setColor("#0099ff")
.addFields(
{ name: "Server:", value: config.server },
{ name: "Regular field title", value: "Some value here" }
)
.setFooter("DSC");
message.channel.send(updates);
}
});
client.login(config.token);
答案 0 :(得分:0)
除了使用常量字符串设置漫游器外,还需要输入config.json
并将其添加到嵌入中来使用它。这样的示例如下:
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.json');
const { Client, MessageEmbed } = require('discord.js');
const prefix = config.prefix; // Don't add '', it will turn it into a constant string.
const fs = require('fs');
client.commands = new Discord.Collection(); // Learning Command Handlers? If so, I will leave this be.
client.on('ready', () => {
// `client.once('', () => {})` Doesn't work.
console.log('DSC BOT is now online!');
});
client.on('message', (message) => {
// Just define once and put everything inside.
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content
.slice(prefix.length)
.trim()
.split(' ');
const command = args.shift().toLowerCase();
// Command code //
if (command === 'ping') {
message.channel.send('Pong!');
} else if (command === 'test') {
message.channel.send(config.server);
}
if (message.content === 'dsc test') {
message.channel.send(
'Hello There, this is a test!',
config.server,
'It should say yolo <<'
);
}
if (message.content === 'dsc updates') {
const updates = new MessageEmbed()
.setTitle('DSC Updates')
.setColor('#0099ff')
.addFields(
{ name: 'Server:', value: config.server || 'Server is not set' },
{ name: 'Prefix', value: prefix || 'No prefix Set' }
)
.setFooter('DSC');
// Make the calls directly to the config and not using constant variables unless they reroute to config.
message.channel.send(updates);
}
});
client.login(config.token);