【问题标题】:discord.js / typescript / heroku / github: SyntaxError: Cannot use import statement outside a modulediscord.js / typescript / heroku / github:SyntaxError:不能在模块外使用导入语句
【发布时间】:2021-11-27 03:18:23
【问题描述】:
// imports all the things needed to run the discord bot successfully
import DiscordJS, { TextChannel, Intents, Message, Channel, NewsChannel, ThreadChannel, DiscordAPIError } from 'discord.js'
type guildTextBasedChannel = TextChannel | NewsChannel | ThreadChannel

import dotenv from 'dotenv'
dotenv.config()

//sets prefix to be used when running bot commands
const prefix = '~';

//This lets the discord bot know what your intentions are using this bot. Hence the guilds, guilds messages and message reactions
const client = new DiscordJS.Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
        Intents.FLAGS.DIRECT_MESSAGES
    ]
})

//This prints out "The bot is ready" whenever you turn the bot, hence the 'ready' parameter
client.on('ready', () => {
    console.log('The bot is ready')
})

//Allows the program to access the correct bot using the specified token
client.login(process.env.token)

我正在尝试制作一个不和谐的机器人,但让它使用 github 和 heroku 24/7 全天候运行。我正在观看有关如何设置它的视频,但是在 heroku 上运行它时出现错误。

我猜这个错误是因为我正在为机器人使用打字稿。我必须转换为 javascript 还是有一个简单的解决方法?

编辑:

"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });


// imports all the things needed to run the discord bot successfully
const discord_js_1 = __importStar(require("discord.js"));
const dotenv_1 = __importDefault(require("dotenv"));
dotenv_1.default.config();

//sets prefix to be used when running bot commands
const prefix = '~';

//This lets the discord bot know what your intentions are using this bot. Hence the guilds, guilds messages and message reactions
const client = new discord_js_1.default.Client({
    intents: [
        discord_js_1.Intents.FLAGS.GUILDS,
        discord_js_1.Intents.FLAGS.GUILD_MESSAGES,
        discord_js_1.Intents.FLAGS.GUILD_MESSAGE_REACTIONS,
        discord_js_1.Intents.FLAGS.DIRECT_MESSAGES
    ]
});




//This prints out "The bot is ready" whenever you turn the bot, hence the 'ready' parameter
client.on('ready', () => {
    console.log('The bot is ready');
});
client.login(process.env.token);

上面是我做“tsc”之后的 index.js 代码。

上图是我在heroku中得到的错误信息。

【问题讨论】:

  • 确保您运行的是最新的节点版本,并且您的 package.json ('type': 'module',) 中的 'type' 设置为 'module'

标签: typescript github heroku import discord.js


【解决方案1】:

根据我使用 Heroku 的经验,如果您尝试直接执行 TS 文件,则效果不佳。我建议你使用类似tsc 的东西来转译为 JS 并在 Heroku 上运行 JavaScript。

旁注:如果您在 package.json 中设置 "type": "module",您将无法访问一些全局变量,例如 __dirname

【讨论】:

  • 嗨,我使用 tsc 按照您的建议转译代码,但是还有另一个错误说我的令牌现在无效,我不明白,但我已经用索引更新了问题.js 代码和错误消息。如果您能再次帮助我,我将不胜感激!
  • 您是否使用了 --esModuleInterop 标志?此外,您可能希望将 dotenv_1.default.config(); 更改为 dotenv_1.config();
猜你喜欢
  • 2021-09-01
  • 2020-07-14
  • 2023-02-08
  • 2021-08-05
  • 2021-03-10
  • 1970-01-01
  • 2021-02-06
  • 2021-07-05
  • 1970-01-01
相关资源
最近更新 更多