自定义词典的Discord机器人

时间:2019-11-21 03:31:08

标签: discord.js

我正在准备一个包含将近5000个术语的“词典”认证课程。我想创建一个Discord机器人,该机器人可以通过以下方式搜索定义:

!define dog

,输出应该是

dog : "A slang term used to refer to a low-growth, low-market-share product. See: growth-share matrix."

所有术语和定义当前都以下列方式在制表符分隔的文本文件中:

term definition
term definition
term definition

我该如何构建这样的机器人?

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

    var dictionary = ["dog", "cat", "ape"];
    var definitions = ["canine", "feline", "mammal"];
    var arrIndex = 0;
    var messageWords = message.content.toLowerCase().split(' ');

    if (messageWords[0] === `!define`) {
        if (isNaN(messageWords[1][0] / 1)) {
            arrIndex = dictionary.indexOf(dictionary.find(element => element === messageWords[1]));
            message.channel.send(`${dictionary[arrIndex]}: ${definitions[arrIndex]}`);
        };    
    };

我将字典和定义分开,因为它使使用indexOf更加容易。

编辑:您必须将术语和定义分开,并按照上面的格式设置它们。