所以我有一个不和谐的机器人来生成 key ,您可以通过命令兑换它。我想要一个从keys.txt 中抓取一行的命令,并向命令的发送者发送dms。
我自己尝试过,但没有成功。我对 javascript 不太熟悉,所以这对我来说很难。
我还没有任何代码。
我希望它通过 DM 方式发送命令的发送者从 keys.json 中获取的 key
最佳答案
读取和处理文件...
const fs = require('fs'); // Included in Node.js
const keysPath = './keys.txt'; // Change to the relative path of the txt file
try {
// The next line reads the file and returns an array containing each line
const keys = fs.readFileSync(keysPath).replace(/\r/g, '').split(/\n/);
} catch(err) {
console.error(err);
}
发送和处理 key ...
message.author.send(`Here's your key: ||${keys[0]}||`)
.then(() => {
keys.splice(0, 1); // Removes the first key that was just given out
try {
fs.writeFileSync(keys.join('\n')); // Puts the updated 'keys' array back into the file
} catch(err) {
console.error(err);
}
})
.catch(console.error);
关于javascript - 我正在尝试创建一个discord.js命令,从json文件中获取一行,然后将其发送到用户DM中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57243626/