Discord.js机器人报segmentation fault (core dumped)错误 修改后仍失效求解决
问题根源与修复方案
核心错误点
- 未定义变量引用:
messageCreate事件回调的参数命名为msg,但后续嵌入命令逻辑中全部使用了未声明的message对象,访问message.content会触发严重引用错误,是导致段错误的核心原因 - prefix变量冲突:全局已定义固定前缀
?,但事件回调内重复从环境变量读取config.prefix覆盖原有值,若Replit环境变量未配置prefix参数,前缀会变为undefined导致所有命令匹配失效 - 版本兼容问题:若你使用的是discord.js v14及以上版本,旧版字符串形式的intent声明、
MessageEmbed类均已废弃,不兼容的API调用会触发底层报错 - 缺少异常捕获:没有对消息发送等异步操作做错误捕获,权限不足、频道异常等问题会触发未捕获异常导致进程崩溃
修复后完整代码
const prefix = '?'; const Discord = require('discord.js'); // 如果使用discord.js v14,请把上面两行替换为下面的导入 // const { Client, GatewayIntentBits, EmbedBuilder } = require('discord.js'); const { MessageEmbed } = require('discord.js'); const bot = new Discord.Client({ intents: [ "GUILDS", "GUILD_MESSAGES" ] // 如果使用discord.js v14,intent替换为下面的写法 // intents: [ // GatewayIntentBits.Guilds, // GatewayIntentBits.GuildMessages, // GatewayIntentBits.MessageContent // ] }); const config = process.env; bot.on('ready', () => { console.log(bot.user.username + ' is online.') }); //Text Commands bot.on('messageCreate', async (msg) => { if(msg.author.bot || msg.channel.type === "dm") return; const messageArray = msg.content.split(' '); const cmd = messageArray[0]; const args = msg.content.substring(msg.content.indexOf(' ') + 1); try { if(cmd.toLowerCase() === prefix + 'discord' ) { await msg.channel.send({ content: 'https://discord.gg/RehTqdDpEm' }) } if(cmd.toLowerCase() === prefix + 'realm' ) { await msg.channel.send({ content: 'https://realms.gg/5T7DrRwbS7o' }) } if(cmd.toLowerCase() === prefix + 'yada' ) { await msg.channel.send({ content: 'yada' }) } //Embed Commands if(msg.content.toLowerCase() === prefix + "help") { let embed = new MessageEmbed() // v14版本替换为 new EmbedBuilder() .setTitle("Command List") .setDescription("**?Player Description**- Shows a description of all the players. \n **realm**- Sends a realm invite \n **?factions**- Shows the factions of this season and their members \n **?info {faction}**- Shows the lore and banner of a faction \n **?help**- Shows this help menu \n **?quote**- Sends a random quote from the server \n **?world**- sends the world lore.") .setColor("RANDOM") await msg.channel.send({ embeds: [embed]}) } if(msg.content.toLowerCase() === prefix + "world") { let embed2 = new MessageEmbed() .setTitle("World Lore") .setDescription("lore") .setColor("RANDOM") await msg.channel.send({ embeds: [embed2]}) } if(msg.content.toLowerCase() === prefix + "info giraduan") { let embed2 = new MessageEmbed() .setTitle("The Giraduan Dominion") .setDescription("lore") .setColor("RANDOM") await msg.channel.send({ embeds: [embed2]}) } if(msg.content.toLowerCase() === prefix + "info rpa") { let embed2 = new MessageEmbed() .setTitle("Rerualin Preservation Association") .setDescription("faction lore") .setColor("RANDOM") await msg.channel.send({ embeds: [embed2]}) } if(msg.content.toLowerCase() === prefix + "player description") { let embed2 = new MessageEmbed() .setTitle("Player Descriptions") .setDescription("oops no info for you") .setColor("RANDOM") await msg.channel.send({ embeds: [embed2]}) } if(msg.content.toLowerCase() === prefix + "character example") { let embed2 = new MessageEmbed() .setTitle("Example Character") .setDescription("description") .setColor("RANDOM") .setThumbnail('https://cdn.discordapp.com/attachments/877727121341026304/904443409614401546/YIN_YANG-removebg-preview.png') await msg.channel.send({ embeds: [embed2]}) } } catch (err) { console.error('操作出错:', err) } }); bot.login(config.token);
额外注意事项
- 如果你使用discord.js v14,需要额外在Discord开发者后台开启Message Content Intent权限,否则无法读取消息内容
- 确认Replit的 Secrets(环境变量)中已经正确配置
token参数,值为你的机器人令牌 - 若修复后仍然报错,可以在Replit控制台执行
npm uninstall discord.js && npm install discord.js@13.17.1固定使用v13版本,避免版本兼容问题
内容的提问来源于stack exchange,提问作者Saturnian
相关产品推荐
相关产品推荐

