我目前有一个售票机器人,我要添加的一个命令是黑名单和取消黑名单。我不确定如何使机器人将这些人添加为黑名单,也不确定如何使机器人检查是否将其列入黑名单,也不确定如何使机器人从黑名单中删除他们的名字。我当时正在考虑制作一个.json文件,但我什至不知道该怎么做,或者这是正确的。任何帮助都很好!
使用json文件是一个好方法。您将使用fs模块(内置在node.js中)来读写json文件。您应该将黑名单存储为包含用户ID的数组。另外,请确保使用JSON.parse()
解析文件中的json数据,然后使用JSON.stringify()
const fs = require('fs')
const blacklists = JSON.parse(fs.readFileSync('./path/to/blacklists.json'))
// Your code so on and so fouth
// Insert a new blacklist
blacklists.push(<Message>.author.id)
// Save new blacklists
fs.writeFileSync('./path/to/blacklists.json', JSON.stringify(blacklists))
// Check if message author is blacklisted
// Insert code here for new ticket command
// Read blacklist file
const blacklists = JSON.parse(fs.readFileSync('./path/to/blacklists.json'));
// If message author is blacklisted, stop the command and say that they are not allowed to make tickets
if (blacklists.includes(<Message>.author.id)) return <Message>.channel.send("You are not allowed to make a ticket :(")
// Rest of your code