Discord.js 我发出警告命令,使用 FS 写入文件
Posted
技术标签:
【中文标题】Discord.js 我发出警告命令,使用 FS 写入文件【英文标题】:Discord.js i make a warn command that write a file using FS 【发布时间】:2021-06-09 10:51:07 【问题描述】:因此,当我警告用户时,它可以正常工作,但是当我再次尝试警告某人时,它会替换旧字符串(原因),但我想让它添加并保留旧字符串。 (原因)我尝试了很多东西,但 fs 仍然自动替换旧字符串(原因)。
还有我的 javascript 代码:
const Discord = require("discord.js")
const fs = require("fs");
module.exports =
name: "warn",
description: "Warn command",
async execute( message)
let config = require("../config.json")
let lang = JSON.parse(fs.readFileSync("./langs.json", "utf8"));
if(!lang[message.guild.id])
lang[message.guild.id] =
lang: config.lang
;
let correct1 = lang[message.guild.id].lang;
const ping = require(`../lang/$correct1/warn.json`)
const response = ping
const messageArray = message.content.split(' ');
const args = messageArray.slice(1);
let sEmbed1 = new Discord.MessageEmbed()
.setColor("#FF0000")
.setTitle("Permission refused.")
.setDescription("<:dont:803357503412502588> | You don't have the permission for this command. (`MANAGE_ROLES`)");
if(!message.member.hasPermission("MANAGE_ROLES")) return message.reply(sEmbed1)
const user = message.mentions.users.first()
if(!user)
return message.channel.send("<:dont:803357503412502588> | You need to mention a user.")
if(message.mentions.users.first().bot)
return message.channel.send("<:dont:803357503412502588> | You can't warn a bot")
if(message.author.id === user.id)
return message.channel.send("<:dont:803357503412502588> | You can't warn yourself!")
const reason = args.slice(1).join(" ")
if(!reason)
return message.channel.send("<:dont:803357503412502588> | You need a reason")
const warnings =
reason: `$reason`,
guild_id: `$message.guild.id`,
user_id: `$user.id`,
moderator: `$message.author.username`
const jsonString = JSON.stringify(warnings)
fs.writeFile('./warnings.json', jsonString, err =>
if (err)
console.log('Error writing file', err)
else
console.log('Successfully wrote file')
)
【问题讨论】:
【参考方案1】:const jsonString = JSON.stringify(warnings) fs.writeFile('./warnings.json', jsonString, err => if (err) console.log('Error writing file', err) else console.log('Successfully wrote file') )
你能把第 2 行换成fs.writeFileSync("./warnings.json", jsonString, 'flags': 'a', err =>
【讨论】:
我做到了,但是当我第一次写它时尝试警告但当我再次尝试警告时它替换并删除旧的(第一次警告)以上是关于Discord.js 我发出警告命令,使用 FS 写入文件的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法在 discord.js 中发出 ping 命令? [复制]
当有人在 Discord.js 中键入未知命令时,如何发出“无效命令”消息?