【发布时间】:2022-07-23 04:13:31
【问题描述】:
所以我为龙与地下城创建了一个基本的掷骰子 dicord 机器人。
到目前为止,我的代码可以用来掷任何类型的骰子,(例如“roll xdy”“roll 1d20”、“roll 100d100”)
当有人发送匹配的消息时,它会输出结果骰子。
我的问题是我想将这些数字加在一起并显示结果总数,但我不确定如何到达那里。
// Run dotenv
require('dotenv').config();
const { any } = require('async');
const Discord = require('discord.js');
const client = new Discord.Client({ intents: ["GUILDS", "GUILD_MESSAGES"] });
client.on('messageCreate', msg => {
z = msg.content;
matches = z.match(/\d+/g);
x = matches[0];
y = matches[1];
if (msg.content === 'ping') {
msg.reply('pong');
}
if (msg.content == 'roll ' + x + 'd' + y) {
function rollDie(sides) {
if (!sides) sides = 6;
return 1 + Math.floor(Math.random() * sides);
}
function rollDice(number, sides) {
var total = [];
var number = x;
var sides = y;
while (number-- > 0) total.push(rollDie(sides));
return total;
}
msg.reply("result: " + rollDice());
console.log(rollDice())
}
});
client.login(process.env.DISCORD_TOKEN);
【问题讨论】:
标签: javascript discord bots dice