简体   繁体   English

在 discord 机器人中发出命令时 json.parse 出现问题

[英]Problems with json.parse when making a command in discord bot

I'm trying to code a Cursedimage subreddit command and I keep getting this error我正在尝试编写一个 Cursedimage subreddit 命令,但我不断收到此错误

(node:10488) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token o in JSON at position 1 (节点:10488)UnhandledPromiseRejectionWarning:SyntaxError:position 1 中 JSON 中的意外令牌 o

here is my code:这是我的代码:

case 'cursedimage':
                    fetch('https://www.reddit.com/r/cursedimages/random/.json').then(response => {
                    let content = JSON.parse(response.body);
                    var image = content[0].data.children[0].data.url;
                    let permalink = content[0].data.children[0].data.permalink;
                    let memeUrl = `https://reddit.com${permalink}`;
                    let memeImage = content[0].data.children[0].data.url;
                    let memeTitle = content[0].data.children[0].data.title;
                    let memeUpvotes = content[0].data.children[0].data.ups;
                    let memeNumComments = content[0].data.children[0].data.num_comments;
                    const cursedembed = new Discord.MessageEmbed()
                    .setColor('RANDOM')
                    .setTitle(content[0].data.children[0].data.title)
                    .setURL(`${memeUrl}`)
                    .setImage(image)
                    .setFooter(`👍 ${memeUpvotes} 💬 ${memeNumComments}`)
                    .setTimestamp()
                message.channel.send(cursedembed);
                });
                break

In response to the comment it prints:作为对它打印的评论的回应:

Gunzip {
  _writeState: Uint32Array(2) [ 0, 0 ],
  _readableState: ReadableState {
    objectMode: false,
    highWaterMark: 16384,
    buffer: BufferList { head: null, tail: null, length: 0 },
    length: 0,
    pipes: null,
    pipesCount: 0,
    flowing: null,
    ended: false,
    endEmitted: false,
    reading: false,
    sync: false,
    needReadable: false,
    emittedReadable: false,
    readableListening: false,
    resumeScheduled: false,
    emitClose: true,
    autoDestroy: false,
    destroyed: false,
    defaultEncoding: 'utf8',
    awaitDrainWriters: null,
    multiAwaitDrain: false,
    readingMore: false,
    decoder: null,
    encoding: null,
    [Symbol(kPaused)]: null
  },
  readable: true,
  _events: [Object: null prototype] {
    prefinish: [Function: prefinish],
    end: [Function: bound onceWrapper] { listener: [Function: bound _close] },
    unpipe: [Function: onunpipe],
    error: [ [Function: onerror], [Function] ],
    close: [Function: bound onceWrapper] { listener: [Function: onclose] },
    finish: [Function: bound onceWrapper] { listener: [Function: onfinish] }
  },
  _eventsCount: 6,
  _maxListeners: undefined,
  _writableState: WritableState {
    objectMode: false,
    highWaterMark: 16384,
    finalCalled: false,
    needDrain: false,
    ending: false,
    ended: false,
    finished: false,
    destroyed: false,
    decodeStrings: true,
    defaultEncoding: 'utf8',
    length: 0,
    writing: false,
    corked: 0,
    sync: true,
    bufferProcessing: false,
    onwrite: [Function: bound onwrite],
    writecb: null,
    writelen: 0,
    afterWriteTickInfo: null,
    bufferedRequest: null,
    lastBufferedRequest: null,
    pendingcb: 0,
    prefinished: false,
    errorEmitted: false,
    emitClose: true,
    autoDestroy: false,
    bufferedRequestCount: 0,
    corkedRequestsFree: {
      next: null,
      entry: null,
      finish: [Function: bound onCorkedFinish]
    }
  },
  writable: true,
  allowHalfOpen: true,
  _transformState: {
    afterTransform: [Function: bound afterTransform],
    needTransform: false,
    transforming: false,
    writecb: null,
    writechunk: null,
    writeencoding: null
  },
  _hadError: false,
  bytesWritten: 0,
  _handle: Zlib {
    onerror: [Function: zlibOnError],
    [Symbol(owner_symbol)]: [Circular]
  },
  _outBuffer: <Buffer 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... 16334 more bytes>,
  _outOffset: 0,
  _chunkSize: 16384,
  _defaultFlushFlag: 2,
  _finishFlushFlag: 2,
  _defaultFullFlushFlag: 3,
  _info: undefined,
  _level: -1,
  _strategy: 0,
  [Symbol(kCapture)]: false
}```

Can you try this piece of Code?你能试试这段代码吗?

 fetch('https://www.reddit.com/r/cursedimages/random/.json').then(response => response.json()).then(response => { let content = response.data; var image = content[0].data.children[0].data.url; let permalink = content[0].data.children[0].data.permalink; let memeUrl = `https://reddit.com${permalink}`; let memeImage = content[0].data.children[0].data.url; let memeTitle = content[0].data.children[0].data.title; let memeUpvotes = content[0].data.children[0].data.ups; let memeNumComments = content[0].data.children[0].data.num_comments; const cursedembed = new Discord.MessageEmbed().setColor('RANDOM').setTitle(content[0].data.children[0].data.title).setURL(`${memeUrl}`).setImage(image).setFooter(` ${memeUpvotes} ${memeNumComments}`).setTimestamp() message.channel.send(cursedembed); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM