Last active
February 5, 2019 05:26
-
-
Save takoeight0821/190ba45abcd26e25d806077d5b629668 to your computer and use it in GitHub Desktop.
ダブルクロス対応のダイスbot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Discord = require('discord.js'); | |
function getRandomInt(min, max) { | |
min = Math.ceil(min); | |
max = Math.floor(max); | |
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive | |
} | |
function diceRoll(count, roll) { | |
let dices = []; | |
for (let i = 0; i < count; i++) { | |
dices.push(getRandomInt(1, roll + 1)); | |
} | |
return dices; | |
} | |
class DiceRoller { | |
constructor(msg) { | |
this.msg = msg; | |
this.text = ""; | |
} | |
addText(text, sep = "\n") { | |
this.text = this.text.concat(sep, text); | |
} | |
trimText(length = 1000) { | |
this.text = this.text.substr(0, length); | |
if (this.text.length >= length) { | |
this.text += "..."; | |
} | |
} | |
sendText() { | |
this.msg.channel.send(this.text); | |
} | |
} | |
function roll_nDn(dr, count, roll) { | |
let dices = diceRoll(count, roll); | |
dr.addText(`[${dices}`); | |
dr.trimText(); | |
dr.addText(`${dices.reduce((a, b) => a + b)}`, "] = "); | |
return dices.reduce((a, b) => a + b); | |
} | |
function roll_dx(dr, count, critical) { | |
let tmp = diceRoll(count, 10); | |
let dices = `[${tmp}]` | |
let score = (Math.max(...tmp) >= critical) ? 10 : Math.max(...tmp); | |
while (tmp.some(a => a >= critical)) { | |
console.log(`DEGUG: ${tmp}`); | |
tmp = diceRoll(tmp.filter((a) => a >= critical).length, 10); | |
dices = dices.concat(", ", `[${tmp}]`); | |
score += (Math.max(...tmp) >= critical) ? 10 : Math.max(...tmp); | |
} | |
dr.addText(`[${dices}`); | |
dr.trimText(); | |
dr.addText(`${score}`, "] = "); | |
return score; | |
} | |
const client = new Discord.Client(); | |
client.on('ready', () => { | |
console.log(`Logged in as ${client.user.tag}!`); | |
}); | |
client.on('message', msg => { | |
const nDn_re = /^(\d+)D(\d+)(\+\d+|\-\d+)?$/i; | |
const ndx_re = /^(\d+)DX(@\d+)?(\+\d+|\-\d+)?$/i; | |
const D66_re = /^D66$/; | |
let dr = new DiceRoller(msg); | |
if (nDn_re.test(msg.content)) { | |
let found = nDn_re.exec(msg.content); | |
dr.addText("roll " + found[0]); | |
let result = roll_nDn(dr, Number(found[1]), Number(found[2])); | |
if (!(found[3] === undefined)) { | |
dr.addText(`${result} + ${Number(found[3])} = ${result + Number(found[3])}`); | |
} | |
dr.sendText(); | |
} else if (ndx_re.test(msg.content)) { | |
let found = ndx_re.exec(msg.content); | |
dr.addText("roll " + found[0]); | |
let result = roll_dx(dr, Number(found[1]), (found[2] === undefined) ? 10 : Number(found[2].slice(1))); | |
if (!(found[3] === undefined)) { | |
dr.addText(`${result} + ${Number(found[3])} = ${result + Number(found[3])}`); | |
} | |
dr.sendText(); | |
} else if (D66_re.test(msg.content)) { | |
dr.addText("roll D66"); | |
roll_nDn(dr, 2, 6); | |
dr.sendText(); | |
} | |
}); | |
client.login('token'); | |
const readline = require('readline'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
rl.on('close', () => { | |
client.destroy(); | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment