【发布时间】:2021-12-19 04:05:14
【问题描述】:
我目前正在尝试按照 discord.js 文档制作一个简单的不和谐机器人。(https://discordjs.guide/creating-your-bot/creating-commands.html#command-deployment-script) 我的问题是关于 deploy-command.js 文件:
const {SlashCommandBuilder} = require('@discordjs/builders')
const {Routes} = require('discord-api-types/v9')
const dotenv = require('dotenv')
const {REST} = require("@discordjs/rest");
dotenv.config()
const token = process.env.DISCORD_JS_TOKEN
const clientId = process.env.clientId
const guildId = process.env.guildId
const commands = [
new SlashCommandBuilder().setName('ping').setDescription('Replies with pong!'),
new SlashCommandBuilder().setName('server').setDescription('Replies with server info!'),
new SlashCommandBuilder().setName('user').setDescription('Replies with user info!')
].map(command => command.toJSON())
const rest = new REST({version:'9'}).setToken(token)
rest.put(Routes.applicationGuildCommands(clientId,guildId),{body:commands})
.then(() => console.log("Successfully registered application commands from file"))
.catch(() => console.log('error happened'))
以下行出现错误:
const rest = new REST({version:'9'}).setToken(token) ---> 接口无法实例化
我尝试删除 package-lock.json 和 node_modules,然后通过“npm install”重新安装它们,但没有任何效果。 discordjs/rest 模块中有一个 REST 接口和一个 REST 类。悬停在 {version:'9'},我收到以下消息:“参数数量无效,预期为 0”和“在类 rest 中创建构造函数”的建议(对于 REST 类已经有一个)
这个问题非常令人沮丧,因为我似乎找不到其他有类似问题的人,而且我在 youtube 上观看的所有视频都执行完全相同的代码,没有任何问题(唯一的区别是他们使用视觉视频中的工作室代码)。
任何帮助将不胜感激。
使用的软件:
nodejs(试用版本 16.6.0 和 17.0.0)
网络风暴 2021.2.2
【问题讨论】:
-
你的令牌正确吗?
-
嗨!欢迎来到 StackOverflow。我建议使用 vscode 进行 discord.js 开发,因为它有一些非常好的语言特性,但 WebStorm 也很好。您的语法可能不正确。确保大括号之间有空格:
const rest = new REST({ version: '9' }).setToken(token);
标签: javascript node.js rest discord discord.js