javascript - SyntaxError: await 仅在异步函数 discord.js 中有效
问题描述
我正在尝试创建一个命令来从 API 获取数据以显示在图像上,但我不断收到此错误:
/media/bobby/Hardrive/NEW PC/Public/commands/now.js:16
const background = await Canvas.loadImage('https://lgbtqlounge.xyz/assets/img/canvas.png')
^^^^^
SyntaxError: await is only valid in async function
at wrapSafe (internal/modules/cjs/loader.js:931:16)
at Module._compile (internal/modules/cjs/loader.js:979:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Module.require (internal/modules/cjs/loader.js:903:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object.<anonymous> (/media/bobby/Hardrive/NEW PC/Public/index.js:14:21)
at Module._compile (internal/modules/cjs/loader.js:1015:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1035:10)
at Module.load (internal/modules/cjs/loader.js:879:32)
at Function.Module._load (internal/modules/cjs/loader.js:724:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
at internal/main/run_main_module.js:17:47
这是我的代码:
const fetch = require("node-fetch")
const Canvas = require("canvas")
module.exports = {
name: 'now',
description: 'Tells you what is live on LGBTQLounge Radio!',
async execute(message, args) {
fetch("http://api.lgbtqlounge.xyz/radio").then(res => res.json()).then(res => {
const title = res.song.song
const artist = res.song.artist
const art = res.song.art
const currentDJ = res.currentdj.name
const canvas = Canvas.createCanvas(1250, 500)
const ctx = canvas.getContext('2d')
const background = await Canvas.loadImage('https://lgbtqlounge.xyz/assets/img/canvas.png')
ctx.drawImage(background, 0, 0, canvas.width, canvas.height)
ctx.strokeStyle = '#74037b'
ctx.strokeRect(0,0, canvas.width, canvas.height)
ctx.font = 'bold 48px "Noto Sans", sans-serif'
ctx.fillStyle = '#ffffff'
ctx.fillText(`${title} by ${artist}`, canvas.width / 2.5, canvas.height / 2.6)
ctx.font = 'bold 72px "Noto Sans", sans-serif'
ctx.fillStyle = '#ffffff'
ctx.fillText(currentDJ, canvas.width / 2.5, canvas.height / 1.6)
ctx.beginPath()
ctx.arc(235, 244, 150, 0, Math.PI * 2, true)
ctx.closePath()
ctx.clip()
ctx.drawImage(art, 85, 95, 300, 300)
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'now.png')
message.channel.send(attachment);
})
}
}
如果有人可以帮助我解决这个问题,将不胜感激!
提前感谢您的任何帮助,我希望每个人都度过了愉快的一天(它不会让我发布,因为其中大部分是代码,所以必须详细说明一下,所以我正在享受我一生中最有趣的事情)
解决方案
正如错误所说,await
仅在async
函数中有效,您需要将代码包装在async
函数中。您的execute
函数是,但不是异步async
的直接父函数,它是.const background = await Canvas.loadImage
.then
fetch("http://api.lgbtqlounge.xyz/radio").then(res => res.json())
.then(async res => {
const title = res.song.song
const artist = res.song.artist
const art = res.song.art
const currentDJ = res.currentdj.name
const canvas = Canvas.createCanvas(1250, 500)
const ctx = canvas.getContext('2d')
const background = await Canvas.loadImage('https://lgbtqlounge.xyz/assets/img/canvas.png')
ctx.drawImage(background, 0, 0, canvas.width, canvas.height)
ctx.strokeStyle = '#74037b'
ctx.strokeRect(0,0, canvas.width, canvas.height)
ctx.font = 'bold 48px "Noto Sans", sans-serif'
ctx.fillStyle = '#ffffff'
ctx.fillText(`${title} by ${artist}`, canvas.width / 2.5, canvas.height / 2.6)
ctx.font = 'bold 72px "Noto Sans", sans-serif'
ctx.fillStyle = '#ffffff'
ctx.fillText(currentDJ, canvas.width / 2.5, canvas.height / 1.6)
ctx.beginPath()
ctx.arc(235, 244, 150, 0, Math.PI * 2, true)
ctx.closePath()
ctx.clip()
ctx.drawImage(art, 85, 95, 300, 300)
const attachment = new Discord.MessageAttachment(canvas.toBuffer(), 'now.png')
message.channel.send(attachment);
})
制作回调函数async
将解决您的问题。
推荐阅读
- selenium - 如何在 Jenkins 中执行机器人测试用例
- gradle - 如何让自定义 Gradle 任务运行 Grails 应用程序
- c++ - 在原子多线程代码中删除容器
- c# - EF Core Add-Migration 失败并出现 System.MissingMethodException:未定义无参数构造函数
- bash - 替代不依赖于网络连接的主机名
- javascript - 如何访问值以使用 JavaScript 中嵌套对象的值?
- r - 在 R 中使用函数指定参数的名称
- google-cloud-platform - 使用 Google ESP 代理到多个服务
- r - 为什么我的简单代码不将变量存储在全局环境中
- hive - 创建表中的 Hive 日期函数