第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

message.author.id 一直未定義

message.author.id 一直未定義

繁花不似錦 2023-06-15 15:57:38
我正在嘗試為我的測(cè)驗(yàn)命令做一個(gè)記分牌,以一種人運(yùn)行命令 !quiz 并回答問(wèn)題的方式。如果問(wèn)題回答正確,在一個(gè)名為 Storage 的特定文件中,她會(huì)得到一個(gè)額外的分?jǐn)?shù),從而為正確回答的數(shù)量制作一個(gè)記分牌。作為參考,這是我的代碼//Base...const Discord = require('discord.js');const client = new Discord.Client();//Quiz event...client.on('message', message => {? ? if (!message.content.startsWith(prefix) || message.author.bot) return;? ? if (message.channel instanceof Discord.DMChannel) return message.channel.send('Aqui não podem ser executado comandos!')? ? ? ? .then(msg => {? ? ? ? ? ? msg.delete({ timeout: 5000 })? ? ? ? })? ? const quiz = require('./quiz.json');? ? const item = quiz[Math.floor(Math.random() * quiz.length)];? ? const args = message.content.slice(prefix.length).trim().split(/ +/);? ? const command = args.shift().toLowerCase();? ? const filter = response => {? ? ? ? return item.answers.some(answer => answer.toLowerCase() === response.content.toLowerCase());? ? };? ? if (message.content === '!quiz')? ? ? ? message.channel.send(item.question).then(() => {? ? ? ? ? ? const userData = fs.readdirSync('./Storage').filter(file => file.endsWith('.json'));? ? ? ? ? ? fs.writeFile('Storage/userData.json', JSON.stringify(), (err) => {? ? ? ? ? ? ? ? if (err) console.error(err);? ? ? ? ? ? })? ? ? ? ? ? if (!userData[message.author.id]) userData[message.author.id] = {? ? ? ? ? ? ? ? rightQuizAnswers: 0? ? ? ? ? ??? ? ? ? ? ? ? ? });? ? ? ? });})測(cè)驗(yàn)工作正常,唯一的問(wèn)題是我支持的文件是這樣存儲(chǔ)點(diǎn)的:{(the author's ID:{rightQuizAnswers:(number of questions guessed correct)}}只得到回應(yīng):undefined甚至把它變成一個(gè)更糟糕的問(wèn)題,當(dāng)我用我的 ID 替換 'message.author.id' 以便它會(huì)添加一個(gè)點(diǎn)到 rightQuizAnswers 時(shí),它仍然返回undefined......
查看完整描述

1 回答

?
POPMUISE

TA貢獻(xiàn)1765條經(jīng)驗(yàn) 獲得超5個(gè)贊

我并沒(méi)有真正深入研究您的代碼,但在我看來(lái),JSON 文件可能有問(wèn)題,該文件至少應(yīng)包含{}起始內(nèi)容,或者文件讀取/保存有問(wèn)題。我試圖優(yōu)化讀取/保存,其他一切都是一樣的,所以這里是代碼:


const userDataPath = path.resolve(__dirname, './Storage/userData.json');


function loadUserData() {

  return JSON.parse(fs.readFileSync(userDataPath).toString());

}


function saveUserData(userData) {

  fs.writeFileSync(userDataPath, JSON.stringify(userData, null, 2));

}


message.channel.awaitMessages(filter, { max: 1, time: 30000, errors: ['time'] })

  .then(collected => {

    const userData = loadUserData();


    message.channel.send(`${collected.first().author} acertou!`);


    if (!userData[message.author.id]) {

      userData[message.author.id] = {

        rightQuizAnswers: 0

      };

    }


    userData[message.author.id].rightQuizAnswers += 1;

    saveUserData(userData);

  })

  .catch(collected => {

    message.channel.send('Parece que ninguém acertou :(  (Bando de buro)');

  });

在其他方面,如果您還沒(méi)有這樣做,您應(yīng)該像需要 fs 一樣需要“路徑”。


const fs = require('fs');

const path = require('path'); // Add this


查看完整回答
反對(duì) 回復(fù) 2023-06-15
  • 1 回答
  • 0 關(guān)注
  • 151 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)