首页 > 解决方案 > SyntaxError:意外的令牌“导出”

问题描述

所以是的,我会直截了当,一个错误被抛出,而我不知道该怎么做,这很奇怪,因为代码很好,只是我在这里说的有问题。这是问题所在:

const { codeBlock } = require("@discordjs/builders");
const { ExplicitContentFilterLevels } = require("discord.js/typings/enums.d.ts");

async function clean(client, text) {
  if (text && text.container.name == "Promise")
    text = await text;
  if (typeof text !== "string")
    text = require("util").inspect(text, {depth: 1});

  text = text
    .replace(/`/g, "`" + String.fromCharCode(8023))
    .replace(/@/g, "@" + String.fromCharCode(8023));

  text = text.replaceAll(client.token, "[REDACTED]");

  return text;
}

exports.run = async (client, message, args, level) => {
  const code = args.join(" ");
  const evaled = eval(code);
  const cleaned = await clean(client, evaled);
  message.channel.send(codeBlock("js", cleaned));
};

exports.conf = {
  enabled: true,
  guildOnly: false,
  aliases: [],
  permLevel: "Bot Owner"
};

exports.help = {
  name: "eval",
  category: "System",
  description: "Evaluates arbitrary javascript.",
  usage: "eval [...code]"
};

这是向我抛出错误的地方:

ERROR Unhandled rejection: SyntaxError: Unexpected token 'export' 
C:\Users\emilb\OneDrive\Desktop\Tap\node_modules\discord.js\typings\enums.d.ts:4
export const enum ActivityTypes {
^^^^^^

SyntaxError: Unexpected token 'export'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (C:\Users\emilb\OneDrive\Desktop\Tap\commands\eval.js:2:41)

标签: javascriptnode.jsdiscord.js

解决方案


您不应该导入*.d.ts文件。这些文件是 TypeScript 定义文件,可以帮助您 IDE 提供更好的类型提示。

将导入更改为:

const { ExplicitContentFilterLevels } = require("discord.js/src/util/Constants");

或者甚至更好地将其更改为下面的行,因为这样您就不会被 Discord 包的内部结构所束缚。

const { Constants: { ExplicitContentFilterLevels } } = require('discord.js');

推荐阅读