如何使用命令更改 discord.js 中的文件?

Posted

技术标签:

【中文标题】如何使用命令更改 discord.js 中的文件?【英文标题】:How to change files in discord.js by using commands? 【发布时间】:2020-06-29 07:11:18 【问题描述】:

我尝试了很多东西。但我无法使用该命令更改配置文件。我想在该配置中添加频道 ID

const fs = require('fs');
const config = require('../config.json')
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports.run = async (client, message, args) => 

let owner = process.env.OWNER.split(',')
if(!owner.includes(message.author.id)) 
  return message.reply("This command is not made for everyone")

   if (message.channel.type === "dm" || message.author.bot || message.author === client.user) return; // Checks if we're on DMs, or the Author is a Bot, or the Author is our Bot, stop.

 fs.writeFile('./config.json', args[0], (err) => 
         if (err) console.log(err)
    )
  message.channel.send("Done")


exports.help = 
  name: "wtp",
  category: "General",
  description: "Add this channel into WTP",
  usage: "wtp <channel_id>"
;```

【问题讨论】:

【参考方案1】:

您也可以这样做,添加/更改配置变量然后保存。

const fs = require('fs');
const config = require('../config.json')
const Discord = require('discord.js');
const client = new Discord.Client();
module.exports.run = async (client, message, args) => 

    let owner = process.env.OWNER.split(',')
    if(!owner.includes(message.author.id)) return message.reply("This command is not made for everyone")


    if (message.channel.type === "dm" || message.author.bot || message.author === client.user) return;  // Checks if we're on DMs, or the Author is a Bot, or the Author is our Bot, stop.

    if (args.length === 0) return message.reply('Pls mention a channell')
    let targetChannel = message.mentions.channels.first() || message.guild.channels.get(args[0])
    if (!targetChannel) return message.reply('Cant fin`d channel')
    config.channed_id = targetChannel.id


 fs.writeFile('./config.json',JSON.stringify(config) , (err) => 
         if (err) console.log(err);
         message.channel.send("Done")
    )


exports.help = 
  name: "wtp",
  category: "General",
  description: "Add this channel into WTP",
  usage: "wtp <channel_id>"
;

【讨论】:

以上是关于如何使用命令更改 discord.js 中的文件?的主要内容,如果未能解决你的问题,请参考以下文章

Discord.JS 命令处理程序子文件夹

如何添加 ID 保存在 JSON 文件 discord.js v12 中的角色?

错误:在 Heroku 上找不到模块 Discord.js

如何自动更改不和谐机器人的昵称。 (Javascript)(Discord.js)

Discord.js - 允许特定频道中的命令[关闭]

如何删除公会 discord.js 中的所有频道