【问题标题】:How to return a variable using a discord bot [duplicate]如何使用不和谐机器人返回变量[重复]
【发布时间】:2019-12-18 12:16:12
【问题描述】:

所以我按照教程创建了一个简单的不和谐机器人,我正在搞乱它,机器人将连接到服务器,当用户输入!ping 时,机器人将返回一条消息。我想将其更改为打印全局定义的变量集。我不知道该怎么做

我是 Javascript 新手并制作了不和谐的机器人,所以我不确定如何以我想要的方式打印变量,下面是代码。

var money;
money = 0;

bot.on('message', function (user, userID, channelID, message, evt) {
    // Our bot needs to know if it will execute a command
    // It will listen for messages that will start with `!`
    if (message.substring(0, 1) == '!') {
        var args = message.substring(1).split(' ');
        var cmd = args[0];

        args = args.splice(1);
        switch(cmd) {

            // !ping
            case 'ping':
                bot.sendMessage({
                    to: channelID,
                    message: "Hello World",
                });
            break;
            // Just add any case commands if you want to..
         }
     }
});

所以在顶部,我定义了整数及其名称以及值 0 现在,当我输入 !ping 时,机器人不会说“Hello World”,我希望它返回钱等于的值,但不知道该怎么做。

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    请注意 sendMessage 已弃用,请改用 textchannel#send

    要在字符串中包含变量,您可以使用以下任何示例:

    channel.send('A variable: ' + var);
    
    channel.send(`A variable: ${var}`);
    

    其中var 是您希望包含在字符串中的变量。

    所以以你的金钱为例,你会这样做:

    message.channel.send(money);
    

    【讨论】:

    猜你喜欢
    • 2021-02-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 1970-01-01
    • 2020-10-11
    • 1970-01-01
    相关资源
    最近更新 更多