我想制作一个对一个触发案例给出多种响应的机器人,但是由于我仍然只是编码的新手,所以我不知道该怎么做。我问是因为我正在从游戏中重新创建关键内容,并且其中一个代码对此有多个响应。我已经粘贴了所有代码并进行了更改以缩短代码。
我在线检查了一下,但是没有一个完全匹配我的代码或没有显示任何放置示例。
var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
token: auth.token,
autorun: true
});
bot.on('ready', function (evt) {
logger.info('Connected');
logger.info('Logged in as: ');
logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function (user, userID, channelID, message, evt) {
if (message.substring(0, 1) == '!') {
var args = message.substring(1).split(' ');
var cmd = args[0];
args = args.splice(1);
switch(cmd) {
case '7378':
bot.sendMessage({
to: channelID,
message: 'Response TEST'
});
break;
case '5653':
bot.sendMessage({
to: channelID,
message: 'Response JOKE 1'
// JOKE has multiple responses to the case. It is the one I'm looking to add the extra responses to.
});
break;
case '5683':
bot.sendMessage({
to: channelID,
message: 'Response LOVE'
});
break;
case '1841':
bot.sendMessage({
to: channelID,
message: 'Response QUIZ'
});
break;
case '8368':
bot.sendMessage({
to: channelID,
message: 'Response VENT'
});
break;
case '2673':
bot.sendMessage({
to: channelID,
message: 'Response CORE'
});
break;
// Just add any case commands if you want to.
}
}
});
我应该期望对JOKE代码有多个响应(5个可能的响应)。
答案 0 :(得分:0)
将消息放入数组
VpcSecurityGroup:
Type: AWS::EC2::SecurityGroup
DependsOn:
- ServerlessSubnetA
- ServerlessSubnetB
Properties:
VpcId:
Ref: ServerlessVPC
GroupDescription: "Allow all traffic"
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
SecurityGroupIngress:
- IpProtocol: -1
CidrIp: 0.0.0.0/0
然后,您可以根据嵌套在对象中的数组中的索引来随机检索一个,或者为每个消息分配一个ID并使用它们,或者您想做的其他事情
答案 1 :(得分:0)
您可以利用Math.random()函数帮助对笑话返回随机答案
case '5653':
switch (Math.floor(Math.random() * 5)){
case 0:
messageVal = "First Joke answer";
break;
case 1:
messageVal = "Second Joke answer";
break;
case 2:
messageVal = "Third Joke answer";
break;
case 3:
messageVal = "Forth Joke answer";
break;
case 4:
messageVal = "Fifth Joke answer";
break;
}
bot.sendMessage({
to: channelID,
message: messageVal
});
break;
这是基于这样的假设,即没有条件发送回什么响应。