javascript - 选择机器人滚动的次数(Discord.js)
问题描述
现在我正在研究一个随机数生成器,就像一个虚拟的掷骰子。我让用户输入一个数字,机器人随机选择一个介于 1 和用户输入的任何数字之间的数字。我一直在尝试添加一种方式,让用户首先说出他们希望它滚动多少次。我对如何使用我已经拥有的代码执行此操作感到非常困惑。
快速运行:我希望机器人在用户发送最大滚动数后询问我希望机器人询问他们想要滚动多少次。
代码:
const Discord = require('discord.js');
const client = new Discord.Client();
function getRandomInt(max){
return Math.floor(Math.random() * Math.floor(max));
}
const prefix = 'roll';
client.once('ready', () => {
console.log('your bot name is online!');
client.user.setActivity("Type 'Role Help'", {type: 3});
});
client.on('message', message =>{
if(!message.content.toLowerCase().startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === ''){
message.channel.send("What's the **Max** role number you want?");
message.channel.awaitMessages(m => m.author.id == message.author.id,
{max: 1, time: 10000}).then(collected => {
console.log('collected :' + collected.first().content);
if (isNaN(collected.first().content)){
return message.channel.send("Please provide a valid number!")};
var n = (getRandomInt(collected.first().content));
message.channel.send("You Rolled a " + (n + 1));
}).catch(() => {
message.channel.send("*Roll has been cancelled.*");
})} else if(command === 'Help' || 'help' || 'h' || 'H'){
message.channel.send("**How to use Roll Bot**");
message.channel.send("*1. Type Roll to begin*");
message.channel.send("*2. Input a number or type stop to end the roll.*");
message.channel.send("**Have Fun!**");
}
});
client.login(process.env.token);
解决方案
推荐阅读
- sql - 从 SQL Server 中的分组视图中获取一行
- jenkins - Jenkins 设置为使用 repo 工具管理的不同项目
- java - 如何获取 Java 类路径分隔符?
- python - 我如何添加与其他模型相关的选择字段并传递 id 当前对象?
- sql - 通过删除前导字母字符来连接多个表
- npm - 从 gulpfile 中 Nuget 还原 Azure Devops 工件
- node.js - cPanel(Apache)上的 Phusion 乘客“无法获取”(Express);相同的代码适用于 localhost 和 Heroku
- pandas - 在 pandas 中转置和合并数据
- azure - 如何为 azure 静态网站配置特定的 url?
- c - 使用 setenv (C) 时出现分段错误