所有pastebin-api npm包均报错,Discord机器人代码故障求助
Replit托管Discord机器人时Pastebin API相关npm包持续报错
我在Replit上托管Discord机器人进行测试,所有pastebin-api相关的npm包均出现报错,尝试更换多个同类型包后问题仍未解决。以下是相关代码及报错信息:
相关代码
const { connect, connection: db } = require('mongoose') const PasteClient = require('pastebin-api').default const { Routes } = require('discord-api-types/v9') const { REST } = require('@discordjs/rest') const { resolve } = require('path') const { sync } = require('glob') require('./Interaction') require('./Event') module.exports = class Bot extends Client { constructor () { super({ intents: Object.values(Intents.FLAGS), partials: ['MESSAGE', 'REACTION'], allowedMentions: { parse: ['roles', 'users'], repliedUser: false } }) this.events = new Collection() this.logger = require('../utils/Logger') this.interactions = new Collection() this.database = {} this.guildsData = require('../models/Guilds') this.database.guilds = new Collection() db.on('connected', async () => this.logger.log( `Successfully connected to the database! (Latency: ${Math.round(await this.databasePing())}ms)`, { tag: 'Database' } ) ) db.on('disconnected', () => this.logger.error('Disconnected from the database!', { tag: 'Database' })) db.on('error', (error) => this.logger.error( `Unable to connect to the database!\n${error.stack ? error + '\n\n' + error.stack : error}`, { tag: 'Database' } ) ) db.on('reconnected', async () => this.logger.log( `Reconnected to the database! (Latency: ${Math.round(await this.databasePing())}ms)`, { tag: 'Database' } ) ) } async getGuild ({ _id: guildId }, check) { if (this.database.guilds.get(guildId)) { return check ? this.database.guilds.get(guildId).toJSON() : this.database.guilds.get(guildId) } else { let guildData = check ? await this.guildsData.findOne({ _id: guildId }).lean() : await this.guildsData.findOne({ _id: guildId }) if (guildData) { if (!check) this.database.guilds.set(guildId, guildData) return guildData } else { guildData = new this.guildsData({ _id: guildId }) await guildData.save() this.database.guilds.set(guildId, guildData) return check ? guildData.toJSON() : guildData } } } /* Start database */ async loadDatabase () { return connect(process.env.MONGO, { useNewUrlParser: true, useUnifiedTopology: true }) } /* Get database ping */ async databasePing () { const cNano = process.hrtime() await db.db.command({ ping: 1 }) const time = process.hrtime(cNano) return (time[0] * 1e9 + time[1]) * 1e-6 } async loadPaste () { const paste = new PasteClient(process.env.PASTEBIN_API_KEY) this.paste = paste } /* Load slash commands for each guilds */ async loadInteractions (guildId) { const intFile = await sync(resolve('./src/commands/**/*.js')) intFile.forEach((filepath) => { const File = require(filepath) if (!(File.prototype instanceof Interaction)) return const interaction = new File() interaction.client = this this.interactions.set(interaction.name, interaction) const rest = new REST({ version: '9' }).setToken(process.env.TOKEN) rest.post(Routes.applicationGuildCommands(process.env.CLIENT_ID, guildId), { body: interaction } ) }) } /* Load events */ async loadEvents () { const evtFile = await sync(resolve('./src/events/**/*.js')) evtFile.forEach((filepath) => { const File = require(filepath) if (!(File.prototype instanceof Event)) return const event = new File() event.client = this this.events.set(event.name, event) const emitter = event.emitter ? (typeof event.emitter === 'string' ? this[event.emitter] : emitter) : this emitter[event.type ? 'once' : 'on'](event.name, (...args) => event.exec(...args)) }) } /* Start the bot */ async start (token) { await this.loadEvents() await this.loadDatabase() await this.loadPaste() return super.login(token) } }
报错截图

解决方案
- 排查Replit网络限制:在Replit Shell中执行
curl https://pastebin.com/api,测试是否能正常访问Pastebin API。如果无法访问,说明Replit环境屏蔽了该域名,可尝试使用代理或更换托管环境。 - 验证API密钥:确认
process.env.PASTEBIN_API_KEY中的密钥有效,无过期或权限限制。可以临时硬编码密钥测试,排除环境变量读取问题。 - 改用原生HTTP请求:放弃第三方包,直接用
node-fetch发送请求调用Pastebin官方API,示例代码:const fetch = require('node-fetch'); async function createPaste(apiKey, content, title) { const formData = new URLSearchParams(); formData.append('api_dev_key', apiKey); formData.append('api_option', 'paste'); formData.append('api_paste_code', content); formData.append('api_paste_name', title); const response = await fetch('https://pastebin.com/api/api_post.php', { method: 'POST', body: formData }); return response.text(); } - 调整Node.js版本:在Replit的
.replit文件中指定兼容的Node版本,比如nodejs-18,避免版本不兼容导致的包加载问题:run = "node index.js" language = "nodejs" nodejs_version = "18"
内容的提问来源于stack exchange,提问作者Nico-Nico
相关产品推荐
相关产品推荐

