SyntaxError: Unexpected identifier for "fetch"
SyntaxError: Unexpected identifier for "fetch"
我正在使用 NodeJS 制作一个不和谐的机器人,每次我启动机器人时它都会显示这个错误:
headers = (await fetch(filtered.fileUrl, { method: 'HEAD' })).headers
^^^^^
SyntaxError: Unexpected identifier
我所知道的是,fetch
需要节点获取。起初,我认为问题是我使用的是不支持 const fetch = require('node-fetch')
的 node-fetch@3.0.0,但即使我将其降级为 node-fetch@2.6.1,这个问题仍然存在。
完整代码:
const fetch = require('node-fetch')
const Booru = require('booru')
const Discord = require('discord.js')
const { escapeMarkdown } = Discord.Util
const path = require('path')
const Color = `RANDOM`;
module.exports = {
info: {
name: "booru",
description: "booru image scraper",
cooldown: 30,
},
async execute(client, message, args, Discord) {
const tag_query = args.join(' ');
if (!message.content.includes('help')) {
if (!message.content.includes('something')) {
const hornyEmbed = new Discord.MessageEmbed()
.setTitle('embed cut to preserve space')
if (!message.channel.nsfw) return message.channel.send(hornyEmbed)
Booru.search('gelbooru', tag_query, {
limit: 1,
random: true
})
.then(posts => {
const filtered = posts.blacklist(['blacklist, of course.'])
if (filtered.length === 0) {
const notfoundEmbed = new Discord.MessageEmbed()
.setDescription("embed cut to preserve space")
message.channel.send(notfoundEmbed)
}
let tags =
filtered.tags.join(', ').length < 50
? Discord.Util.escapeMarkdown(filtered.tags.join(', '))
: Discord.Util.escapeMarkdown(filtered.tags.join(', ').substr(0, 50))
+ `... [See All](https://giraffeduck.com/api/echo/?w=${Discord.Util
.escapeMarkdown(filtered.tags.join(',').replace(/(%20)/g, '_'))
.replace(/([()])/g, '\')
.substring(0, 1200)})`
let headers
let tooBig = false
let imgError = false
try {
headers = (await fetch(filtered.fileUrl, {
method: 'HEAD'
})).headers
} catch (e) {
imgError = true
}
if (headers) {
tooBig = parseInt(headers.get('content-length'), 10) / 1000000 > 10
}
for (let post of posts) {
embed_nsfw = new Discord.MessageEmbed()
.setTitle('you horny')
.setColor('#FFC0CB')
.setAuthor('-')
.setDescription(`-` +
`**Provided by:** Gelbooru.com | `, +`[**Booru Page**](${filtered.postView}) | ` +
`**Rating:** ${filtered.rating.toUpperCase()} | ` +
`**File:** ${path.extname(filtered.file_url).toLowerCase()}, ${headers ? fileSizeSI(headers.get('content-length')) : '? kB'}\n` +
`**Tags:** ${tags}` +
(!['.jpg', '.jpeg', '.png', '.gif'].includes(
path.extname(filtered.fileURL).toLowerCase(),
) ?
'`The file will probably not embed.`' :
'') +
(tooBig ? '\n`The image is over 10MB and will not embed.`' : '') +
(imgError ? '\n`I got an error while trying to get the image.`' : ''),
)
.setImage(filtered.sampleUrl)
message.channel.send(embed_nsfw);
}
})
}
if (message.content.includes('something')) {
const helpEmbed = new Discord.MessageEmbed()
.setTitle('cut to preserver space')
message.channel.send(helpEmbed)
}
}
if (message.content.includes('help')) {
const helpEmbed = new Discord.MessageEmbed()
.setTitle('cut to preserver space')
message.channel.send(helpEmbed)
}
}
}
我知道这段代码非常混乱,因为它基本上是我发现的不同代码拼凑而成的弗兰肯斯坦代码。因此,除了主要主题的解决方案外,我也将不胜感激关于另一个我没有注意到的问题的指示。
NodeJS 版本 16.13.0,
NPM 版本 8.1.0,
DiscordJS 版本 12.5.3,
运行 在 Heroku 服务器上。
尝试将 async
添加到 .then(posts =>
。
Booru.search('gelbooru', tag_query, {
...
.then(async posts => {
const filtered = posts.blacklist(['blacklist, of course.'])
if (filtered.length === 0) {
const notfoundEmbed = new Discord.MessageEmbed()
.setDescription("embed cut to preserve space")
message.channel.send(notfoundEmbed)
}
...
try {
headers = (await fetch(filtered.fileUrl, {
method: 'HEAD'
})).headers
}
我正在使用 NodeJS 制作一个不和谐的机器人,每次我启动机器人时它都会显示这个错误:
headers = (await fetch(filtered.fileUrl, { method: 'HEAD' })).headers
^^^^^
SyntaxError: Unexpected identifier
我所知道的是,fetch
需要节点获取。起初,我认为问题是我使用的是不支持 const fetch = require('node-fetch')
的 node-fetch@3.0.0,但即使我将其降级为 node-fetch@2.6.1,这个问题仍然存在。
完整代码:
const fetch = require('node-fetch')
const Booru = require('booru')
const Discord = require('discord.js')
const { escapeMarkdown } = Discord.Util
const path = require('path')
const Color = `RANDOM`;
module.exports = {
info: {
name: "booru",
description: "booru image scraper",
cooldown: 30,
},
async execute(client, message, args, Discord) {
const tag_query = args.join(' ');
if (!message.content.includes('help')) {
if (!message.content.includes('something')) {
const hornyEmbed = new Discord.MessageEmbed()
.setTitle('embed cut to preserve space')
if (!message.channel.nsfw) return message.channel.send(hornyEmbed)
Booru.search('gelbooru', tag_query, {
limit: 1,
random: true
})
.then(posts => {
const filtered = posts.blacklist(['blacklist, of course.'])
if (filtered.length === 0) {
const notfoundEmbed = new Discord.MessageEmbed()
.setDescription("embed cut to preserve space")
message.channel.send(notfoundEmbed)
}
let tags =
filtered.tags.join(', ').length < 50
? Discord.Util.escapeMarkdown(filtered.tags.join(', '))
: Discord.Util.escapeMarkdown(filtered.tags.join(', ').substr(0, 50))
+ `... [See All](https://giraffeduck.com/api/echo/?w=${Discord.Util
.escapeMarkdown(filtered.tags.join(',').replace(/(%20)/g, '_'))
.replace(/([()])/g, '\')
.substring(0, 1200)})`
let headers
let tooBig = false
let imgError = false
try {
headers = (await fetch(filtered.fileUrl, {
method: 'HEAD'
})).headers
} catch (e) {
imgError = true
}
if (headers) {
tooBig = parseInt(headers.get('content-length'), 10) / 1000000 > 10
}
for (let post of posts) {
embed_nsfw = new Discord.MessageEmbed()
.setTitle('you horny')
.setColor('#FFC0CB')
.setAuthor('-')
.setDescription(`-` +
`**Provided by:** Gelbooru.com | `, +`[**Booru Page**](${filtered.postView}) | ` +
`**Rating:** ${filtered.rating.toUpperCase()} | ` +
`**File:** ${path.extname(filtered.file_url).toLowerCase()}, ${headers ? fileSizeSI(headers.get('content-length')) : '? kB'}\n` +
`**Tags:** ${tags}` +
(!['.jpg', '.jpeg', '.png', '.gif'].includes(
path.extname(filtered.fileURL).toLowerCase(),
) ?
'`The file will probably not embed.`' :
'') +
(tooBig ? '\n`The image is over 10MB and will not embed.`' : '') +
(imgError ? '\n`I got an error while trying to get the image.`' : ''),
)
.setImage(filtered.sampleUrl)
message.channel.send(embed_nsfw);
}
})
}
if (message.content.includes('something')) {
const helpEmbed = new Discord.MessageEmbed()
.setTitle('cut to preserver space')
message.channel.send(helpEmbed)
}
}
if (message.content.includes('help')) {
const helpEmbed = new Discord.MessageEmbed()
.setTitle('cut to preserver space')
message.channel.send(helpEmbed)
}
}
}
我知道这段代码非常混乱,因为它基本上是我发现的不同代码拼凑而成的弗兰肯斯坦代码。因此,除了主要主题的解决方案外,我也将不胜感激关于另一个我没有注意到的问题的指示。
NodeJS 版本 16.13.0, NPM 版本 8.1.0, DiscordJS 版本 12.5.3, 运行 在 Heroku 服务器上。
尝试将 async
添加到 .then(posts =>
。
Booru.search('gelbooru', tag_query, {
...
.then(async posts => {
const filtered = posts.blacklist(['blacklist, of course.'])
if (filtered.length === 0) {
const notfoundEmbed = new Discord.MessageEmbed()
.setDescription("embed cut to preserve space")
message.channel.send(notfoundEmbed)
}
...
try {
headers = (await fetch(filtered.fileUrl, {
method: 'HEAD'
})).headers
}