【问题标题】:Creating a module for multiple 'chat' features for a discord bot为不和谐机器人创建多个“聊天”功能的模块
【发布时间】:2022-01-05 13:05:19
【问题描述】:

我正在重新访问我想要测试升级的第一个不和谐机器人。这个机器人本质上为用户提供了很多关于角色的信息(它是一个狂热的机器人),所以他们有很多行来响应特定的引用。

我之前的经验是 C# 和 Unity。在这种情况下,如果我想在单独的脚本中不断听取任何提示,我会执行以下操作:

公开脚本聊天;

public void Update()
{
chat();
}

因此,只要有人执行脚本中的命令,它就会知道始终倾听并响应这些命令。问题是,这个机器人是我用 JS(Java Script)编写的,我对它还不够熟悉,不知道是否有类似的功能用于不和谐机器人 API。

我遵循了有关模块的教程,但似乎无法让它们正常工作,所以我认为我做错了什么。下面是 index.js 和 chat.js 脚本的副本,但我想我在这里忽略了一些基本的东西。

index.js:

const Discord = require('discord.js');
const chat = require('./chat.js');
const Greeting = require('Greetings');
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 =>
{
    let args = message.content.substring(PREFIX.length).split(" ");

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

    var role = message.guild.roles.find(role => role.name === "user");

    

});

bot.login(token);

聊天.js:

function chat()
{
    //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>.")
        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.");
        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!");
    }

}
function Greetings()
{
    //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.js;
module.exports = Greetings;

如您所见,这个同人圈机器人有很多信息和响应,在此之前,每个命令都保存在索引脚本中。这使得更新/添加功能和升级到机器人变得异常乏味。我确定这是我在这里忽略的非常简单的事情,我只是不够熟悉,无法发现代码出错的地方。

【问题讨论】:

    标签: javascript module discord.js bots


    【解决方案1】:

    也许你正在寻找这个:https://discordjs.guide/creating-your-bot/command-handling.html

    上面的指南演示了斜杠命令的使用,这些命令将在未来取代常规的基于文本的命令。但是,如果您仍然希望拥有一个基于消息的机器人,这里有一些代码:

    const Discord = require('discord.js');
    const fs = require("fs") // "fs" is a built-in module, short for filesystem
    const Greeting = require('Greetings');
    const bot = new Discord.Client();
    const commands = new Discord.Collection(); // A collection is very similar to a C# Hashtable or Dictionary
    
    const token = 'hidden'; 
    
    const PREFIX = '!';
    
    bot.on('ready', () =>
    {
        console.log('Meepo has been freed!');
        bot.user.setActivity("Kameo: Elements of Power");
    })
    // REGISTER COMMAND FILES
    const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js')); // read the directory ./commands for all .js files
    
    for (const file of commandFiles) {
        const command = require(`./commands/${file}`);
        // set a new item in the Collection
        // with the key as the command name and the value as the exported module
        commands.set(command.name, command);
    }
    
    bot.on('messageCreate', async message => // the "message" event is deprecated. Replace with messageCreate
    {
      if(message.author.bot) return // stop if the author of the msg is a bot
      if(!commands.has(message.content)) return // stop if there is no such command
      try {
        commands.get(message.content).execute(message)
      } catch (err) {
        console.error("whoops! error!")
      }
    });
    
    bot.login(token);
    

    现在,创建一个commands 目录。每个命令必须有一个不同的文件,看起来像这样

    module.exports { // export some code
      name: "Message to respond to",
      execute(message) { // method called when bot hears command
        message.channel.send("response")
      }
    }
    

    建议在使用大量 ifswitch 语句之前使用它。

    【讨论】:

      猜你喜欢
      • 2021-03-24
      • 1970-01-01
      • 2019-01-29
      • 2018-01-21
      • 2017-03-05
      • 2022-12-14
      • 2021-03-10
      • 1970-01-01
      • 2020-11-29
      相关资源
      最近更新 更多