【问题标题】:Discord Bot not correctly reading .json DatabaseDiscord Bot 未正确读取 .json 数据库
【发布时间】:2023-03-26 22:30:01
【问题描述】:

我正在尝试构建一个不和谐机器人,它的一个特点是它能够通过使用 json 数组数据库识别用户之前是否与机器人进行过交互。相反,它不会读取数组,而是将用户名写入文件,即使它已经在数据库中。

const Discord = require('discord.js');
const fs = require('fs');
const { json } = require('stream/consumers');
const {prefix, token} = require('./config.json');
const {recognizedusers} = require('./database.json');

//Read File
let db = fs.readFileSync('./database.json');
db = JSON.parse(db);

// New discord client
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });



//Client run once
client.once('ready', () => {
    console.log('Ready!');
});

//Client always run code
client.on('message', message => {
    let msg_content = message.content.toLowerCase()

    // Mixed message content response
    if (msg_content.includes('test 1') && msg_content.includes('test 2')) {
        message.channel.send('test 2: electric boogaloo')
    }

    if (msg_content.includes('test') && !message.author.username.includes(db.recognizedusers)) {

        message.channel.send('unrecognized name')

        //Edit Variable
        db.recognizedusers.push(message.author.username)

        // write file
        fs.writeFileSync('./database.json', JSON.stringify(db));
    }

});

client.login(token);

【问题讨论】:

  • 我想你提到了!db.recognizedusers.includes(message.author.username)
  • 那行得通@Elitezen。谢谢!

标签: json discord.js


【解决方案1】:

Elitezen 解决了我的问题。

我想你提到了!db.recognizedusers.includes(message.author.username)

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2015-07-25
  • 2020-12-20
  • 1970-01-01
  • 2020-09-26
  • 2019-08-12
  • 2021-06-06
  • 1970-01-01
  • 2022-11-17
  • 2021-10-08
相关资源
最近更新 更多