Many fixes, version bump

This commit is contained in:
dakedres 2023-05-20 17:35:11 -06:00
parent 31e6ccdaab
commit 0e825a698e
3 changed files with 25 additions and 36 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "dicedicedice", "name": "dicedicedice",
"version": "1.1.0", "version": "1.2.0",
"description": "Dice bot", "description": "Dice bot",
"main": "src/index.js", "main": "src/index.js",
"scripts": { "scripts": {

View File

@ -1,7 +1,7 @@
const constants = { const constants = {
rollRegex: /^(\d+)?([dhl])(\d+)(\s*([+\-*x\/])\s*(\d+))?/, rollRegex: /^(\d+)?([dhl])(\d+)(\s*([+\-*x\/])\s*(\d+))?/,
optionRollRegex: /^(\d+)?(([dhl])(\d+))?(\s*[+\-*x\/]\s*\d+)?/, optionRollRegex: /^(\d+)?(([dhl])(\d+))?(\s*([+\-*x\/])\s*(\d+))?/,
descriptionRegex: /\s*(\d*-\d*)|(\d+)/g, descriptionRegex: /\s*((\d*-\d*)|(\d+))?([^;\n]+)/g,
macroNameRegex: /^[a-z0-9]+$/, macroNameRegex: /^[a-z0-9]+$/,
commands: { commands: {

View File

@ -25,8 +25,7 @@ const parseOptionRoll = expression => {
modifier modifier
] = match ] = match
.slice(1) .slice(1)
.map(v => v == undefined ? null : v) // Allow us to merge it easier
return { return {
count: parseRollInt(count), count: parseRollInt(count),
mode, mode,
@ -63,40 +62,30 @@ const parseRoll = expression => {
} }
const pullDescription = (expression, match) => { const pullDescription = (expression, match) => {
let description = expression.slice(match[0].length) if(match[0].length == expression.length)
return
return description && parseDescription(description) return parseDescription(expression.slice(match[0].length))
} }
const parseDescription = description => { const parseDescription = description => {
const regex = constants.descriptionRegex
let conditions = [] let conditions = []
let range
let descriptionStart
let match let match
let finishCondition = index => while((match = constants.descriptionRegex.exec(description)) !== null) {
conditions.push({ let range
range,
content: description.slice(descriptionStart, index).trim()
})
while((match = regex.exec(description)) !== null) {
if(range)
finishCondition(match.index)
let [ let [
rangeExp, rangeExp,
valueExp valueExp,
] = match.slice(1) content
] = match.slice(2)
if(rangeExp) { if(rangeExp) {
let split = rangeExp.split('-') let split = rangeExp.split('-')
range = { range = {
lower: split[0] || -Infinity, lower: parseRollInt(split[0], -Infinity),
upper: split[1] || Infinity upper: parseRollInt(split[1], Infinity)
} }
} else if(valueExp) { } else if(valueExp) {
range = { range = {
@ -105,10 +94,12 @@ const parseDescription = description => {
} }
} }
descriptionStart = regex.lastIndex conditions.push({
range,
content: content.trim()
})
} }
finishCondition()
return conditions return conditions
} }
@ -277,15 +268,12 @@ const pruneDB = async () => {
for await(let key of db.keys()) { for await(let key of db.keys()) {
let [ guildId ] = key.split('!').slice(1) let [ guildId ] = key.split('!').slice(1)
console.log(guildId)
if(validIds.includes(guildId)) if(validIds.includes(guildId))
continue continue
if(client.guilds.cache.has(guildId)) { if(client.guilds.cache.has(guildId)) {
validIds.push(guildId) validIds.push(guildId)
} else { } else {
console.log('Pruning key: ' + key)
await db.del(key) await db.del(key)
} }
} }
@ -410,24 +398,25 @@ client.on('interactionCreate', async interaction => {
return return
} }
await interaction.deferReply()
let roll = await openMacros(interaction.guild.id).get(interaction.commandName) let roll = await openMacros(interaction.guild.id).get(interaction.commandName)
if(roll) { if(roll) {
let dice = parseRoll(roll) let dice = parseRoll(roll)
let optionsRoll = interaction.options.get('options') let options = interaction.options.get('options')
if(optionsRoll) { if(options) {
let optionDice = parseOptionRoll(optionsRoll.value) let optionDice = parseOptionRoll(options.value)
console.log(optionDice)
for(let [ key, value ] of Object.entries(optionDice)) { for(let [ key, value ] of Object.entries(optionDice)) {
if(value) if(value)
dice[key] = value dice[key] = value
} }
console.log(dice)
} }
rollDice(dice, content => interaction.reply(content) ) rollDice(dice, content => interaction.followUp(content) )
} }
}) })