You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js机器人snipe指令运行报错snipe.get is not a function求解决

问题原因

报错TypeError: snipe.get is not a function的核心原因是你从index.js导入的snipe不是你预期的Map/Collection实例,本质是导出/导入逻辑不匹配。

修复步骤

1. 检查index.js的snipe导出逻辑

你首先需要确认index.js中已经正确定义并导出了snipe实例,示例正确写法:

// index.js 顶部先定义snipe
const { Client, Collection } = require('discord.js');
const client = new Client();
// 定义snipe,优先用discord.js自带的Collection
const snipe = new Collection();
// 必须导出snipe才能被其他文件导入
module.exports = { snipe, client };
// 或者如果你是挂在client对象上,就不用单独导出
// client.snipe = new Collection();

2. 修正snipe.js的导入逻辑

方案A(更稳定,推荐):直接用execute传入的client

如果你把snipe挂在client对象上,完全不需要导入index.js,避免路径错误:

// 删除原来的const snipe = require('.././../index.js')这行
// 直接用client上挂载的snipe
const msg = client.snipe.get(message.channel.id);

方案B:修正导入语法

如果要保持导入index.js的写法,需要用解构语法拿到snipe实例:

// 注意路径要和你的项目结构匹配,../太多/太少都会导入失败
const { snipe } = require('../../index.js');
const msg = snipe.get(message.channel.id);

3. 修复逻辑顺序bug

你当前的代码先执行了moment(msg.title)再判断if (!msg),如果没有可snipe的消息会直接报错,调整顺序:

const msg = client.snipe.get(message.channel.id);
// 把判断放在最前面
if (!msg) return message.channel.send("Theres Nothing To Snipe here...");
const timeAgo = moment(msg.title).fromNow();

内容的提问来源于stack exchange,提问作者Lemun Juice

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 06:57:02