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

JS开发Discord同人机器人时多聊天功能模块调用问题咨询

存在的基础错误

  1. 模块导入导出逻辑错误
    • chat.js末尾两次赋值module.exports会导致前一次被覆盖,且不能导出文件名chat.js,应导出你定义的函数本身
    • Greetings是chat.js内定义的函数,不是独立第三方包,index.js中require('Greetings')会直接报错找不到模块
  2. 参数未传递问题
    chat和Greetings函数内使用的message对象是discord.js触发message事件时传入的参数,你没有将它传入两个函数,运行时会直接报message is not defined错误
  3. 函数未调用问题
    index.js的message事件回调里只处理了!ping指令,没有调用你写的chat、Greetings函数,相当于你Unity里写了Update但没写调用chat的逻辑,自然不会触发对应回复
  4. discord.js版本兼容问题(如果用v12及以上版本)
    v12之后discord.js把大部分集合操作移到了cache下:
    • message.guild.roles.find要改为message.guild.roles.cache.find
    • message.member.addRole要改为message.member.roles.add

修正后的代码

chat.js

// 函数接收message作为参数
function chat(message)
{
    //Character dialogue. 
    //This is to get more information about characters, or specific reactions to character names.
    if (message.content == "Kreepo") {
        message.reply("That legless guy? He's such a loser, he's got no legs and a BAD attitude!");
    }

    if (message.content == "elemental sprite") {
        message.reply("WHERE?!");
    }

    //Info about Kameo
    if (message.content == "Hey Meepo, can you tell me about Kameo?") {
        message.reply("Sure can! What do you wanna know?\n Kameo Character Info.\n Kameo's Skills.\n Kameo's Origins.")
    }

    if (message.content == "Kameo's Skills.") {
        message.reply("Kameo is basically the jock of the family with twice the charm, she can dropkick a baby and people would still love her!\n \n She can fly pretty fast but can't really get off the ground. That said, large gaps are a pretty big problem for her too.\n \n When fighting on her own, Kameo can do this flip kick, like WHAPOW!\n \n and....that's pretty much it actually.")
    }

    //if the user makes a typo:
    if (message.content == "Kameo's kills.") {
        message.reply("Her WHAT?")
    }

    if (message.content == "Kameo's Origins.") {
        message.reply("Okay, lemme see here. Okay, here it says Kameo came from the Twycross Village, her Gramps, Terramus, is the Cheiftan and he...oh, \n \n -yeah okay he died after Thorn smacked him.\n Solon got real mad and used The Ultimate Curse, then HE died and the Warriors and Wotnot were kaput.\n \n 5 Sprites got captured, 5 were pro at hide n seek and took baby Kameo back to the Royal Fameo, where Theena adopted her and started playing favourites.\n  \nWait, so that means Kameo's parents are still alive, right? \n \nHmmm...")
    }

    if (message.content == "Kameo Character Info.") {
        message.reply("Kameo is the youngest Elvin Princess of the Royal Sisters. She is very headstrong, stubborn and ain't afraid to get into a brawl.\n \n After a certain incident, Kameo changes Kalus' fate and inherits the Element of Power and the Wotnot.\n \n Obviously Kalus is mad about this, so she up and leaves with a plan in mind, ya see.\n \n A plan that would start a really nasty war.\n \n For more, ya gonna have to check out <#424906520883625984>.\n \  Or ya could just go talk to her, I think she's in <#695571436789825536>.")
        // v12+版本用下面这句
        // message.member.roles.add('695568329015951413');
        message.member.addRole('695568329015951413');
    }

    //Info about Kalus

    if (message.content == "Hey Meepo, what do you think about Kalus?") {
        message.reply("Kalus? She's way too serious all the time, and scary when she's angry too! But one time I saw her walking down the hallway by herself...\n \n and she looked so sad. I wonder why? I'm not brave enough to ask, but maybe you can?\n \n ALSO IF SHE ASKS I DIDN'T SEND YOU.");
        // v12+版本用下面这句
        // message.member.roles.add('695568036123508821');
        message.member.addRole('695568036123508821');
    }

    if (message.content == "What else can you tell me about Kalus?") {
        message.reply("I dunno, I'm not Kreepo.");
    }

    if (message.content == "Where's Ortho?") {
        message.reply("Takin' a nap. Old people sleep a lot.");
    }

    //Conversation
    //This is for general conversation/reactions to dialogue, and mostly for fun.
    if (message.content == "Hahaha" || message.content == "Ha ha ha") {
        message.reply("Hee hee hee....what're we laughing at?");

    }

    if (message.content == "thanks meepo") {
        message.reply("You're welcome!");
    }

    if (message.content == "Do I amuse you?") {
        message.reply("Yeah, you're supposed to be a joke, right?");

    }

    if (message.content == "How was it being trapped in stone?") {
        message.reply("Feeling really attacked right now.");
    }

    if (message.content == "Did you know that Meepo was a Diva?") {
        message.reply("I am not! They just said that cause I wanted a PlayStation 3 in my dressing room! ....and maybe I may have wanted the game renamed to Meepo: Elements of Power...*cough*");
    }

    if (message.content == "I feel sad.") {
        message.reply("Hey it's okay, I know what'll cheer you up! What's green and stinky? A TROLL!");
    }

    if (message.content == "lol") {
        message.reply("You look good with a smile!");
    }

    if (message.content == "XD") {
        message.reply("You look good with a smile!");
    }

    if (message.content == "How are you?") {
        message.reply("I'm fantastic, thanks for asking!");
    }

    if (message.content == "Not you Meepo.") {
        message.reply("....yes you Meepo.");
    }

    if (message.content == "Meepo no.") {
        message.reply("MEEPO YES!");
    }

    if (message.content == "fortnite") {
        message.reply("....I will ban you. Right here. Right now.");
    }

    if (message.content == "Meepo can't ban.") {
        message.reply("....In my dreams I can.");
    }

    if (message.content == "Kameo 2.") {
        message.reply("Here is my resume, I can be thrown to catch elemental sprites and really help out!!");
    }

    if (message.content == "I'm going to bed.") {
        message.reply("Want me to tuck you in?");
    }

    if (message.content == "Thanks Meepo") {
        message.reply("You're welcome!");
    }

    if (message.content == "I gotta go to bed.") {
        message.reply("Want me to tuck you in?");
    }

    if (message.content == "No Meepo.") {
        message.reply("Aww.");
    }

    if (message.content == "Hey Meepo, can you tell me about the lore of the game?.") {
        message.reply("Sorry, I can't do that, but there's something in the works for that!");
    }

    //Responds to insults:

    if (message.content == "you suck meepo") {
        message.reply("D:<");
    }

    if (message.content == "meepo you suck") {
        message.reply("D:<");
    }

    if (message.content == "shut up meepo") {
        message.reply("NEVER");
    }

    if (message.content == "you're a loser") {
        message.reply("Takes one to know one!");
    }

}
// 函数接收message作为参数
function Greetings(message)
{
    //Greetings and Goodbyes
    //This is for saying hello or goodbye to the user.
    if (message.content == "Hi!")
    {
        message.reply("Hey, my name's Meepo!");
    }

    if (message.content == "Hello!")
    {
        message.reply("Hey, my name's Meepo, great to meet ya!");
    }

    if (message.content == "Goodnight.")
    {
        message.reply("Goodnight, sleep tight! Don't let the trolls bite!");
    }

    if (message.content == "G'night.")
    {
        message.reply("Goodnight, sleep tight! Don't let the trolls bite!");
    }
}

// 导出两个函数
module.exports = {
  chat,
  Greetings
}

index.js

const Discord = require('discord.js');
// 从chat.js导入两个函数
const { chat, Greetings } = require('./chat.js');
const bot = new Discord.Client();

const token = 'hidden'; 

const PREFIX = '!';

bot.on('ready', () =>
{
    console.log('Meepo has been freed!');
    bot.user.setActivity("Kameo: Elements of Power");
})

bot.on('message', async message =>
{
    // 先跳过机器人自己发的消息,避免死循环
    if(message.author.bot) return;

    let args = message.content.substring(PREFIX.length).split(" ");

    switch (args[0])
    {
        case 'ping':
            message.reply('pong');
            break;
    }

    // v12+版本用下面这句
    // var role = message.guild.roles.cache.find(role => role.name === "user");
    var role = message.guild.roles.find(role => role.name === "user");

    // 调用你写的逻辑,把message作为参数传入
    Greetings(message);
    chat(message);

});

bot.login(token);

额外优化建议

后续可以把不同类别的指令拆成单独的js文件,比如对话类放chat.js,问候类放greet.js,角色查询类放character.js,统一导出后在index.js里调用,维护会更方便。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 20:24:03