Alan Colon 7 rokov pred
commit
d9057ca838
6 zmenil súbory, kde vykonal 241 pridanie a 0 odobranie
  1. 1 0
      .gitignore
  2. 77 0
      bot.js
  3. 97 0
      dice.js
  4. 5 0
      notes
  5. 9 0
      package.json
  6. 52 0
      yarn.lock

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+node_modules/

+ 77 - 0
bot.js

@@ -0,0 +1,77 @@
+const { Dice } = require('./dice')
+var Discord = require('discord.js');
+
+var bot = new Discord.Client()
+/*
+bot.on('ready', function() {
+//    console.log('ready', arguments)
+    console.log('Logged in as %s - %s\n', bot.username, bot.id);
+});
+*/
+
+bot.on('message', function(message) {
+    if (message.author.id === bot.user.id) return;
+    const response = Dice.chat(message.content)
+    console.log(`message from ${message.author.username}: ${message.content}`)
+    if (response) {
+        console.log(response)
+        message.reply(response)
+    }
+});
+/*
+bot.on("any", function(event) {
+  console.log(event)
+});
+*/
+
+/*
+bot.on('channelCreate', function() { console.log('channelCreate', Array.from(arguments)) })
+bot.on('channelDelete', function() { console.log('channelDelete', Array.from(arguments)) })
+bot.on('channelPinsUpdate', function() { console.log('channelPinsUpdate', Array.from(arguments)) })
+bot.on('channelUpdate', function() { console.log('channelUpdate', Array.from(arguments)) })
+bot.on('clientUserGuildSettingsUpdate', function() { console.log('clientUserGuildSettingsUpdate', Array.from(arguments)) })
+bot.on('clientUserSettingsUpdate', function() { console.log('clientUserSettingsUpdate', Array.from(arguments)) })
+bot.on('debug', function() { console.log('debug', Array.from(arguments)) })
+bot.on('disconnect', function() { console.log('disconnect', Array.from(arguments)) })
+bot.on('emojiCreate', function() { console.log('emojiCreate', Array.from(arguments)) })
+bot.on('emojiDelete', function() { console.log('emojiDelete', Array.from(arguments)) })
+bot.on('emojiUpdate', function() { console.log('emojiUpdate', Array.from(arguments)) })
+bot.on('error', function() { console.log('error', Array.from(arguments)) })
+bot.on('guildBanAdd', function() { console.log('guildBanAdd', Array.from(arguments)) })
+bot.on('guildBanRemove', function() { console.log('guildBanRemove', Array.from(arguments)) })
+bot.on('guildCreate', function() { console.log('guildCreate', Array.from(arguments)) })
+bot.on('guildDelete', function() { console.log('guildDelete', Array.from(arguments)) })
+bot.on('guildMemberAdd', function() { console.log('guildMemberAdd', Array.from(arguments)) })
+bot.on('guildMemberAvailable', function() { console.log('guildMemberAvailable', Array.from(arguments)) })
+bot.on('guildMemberRemove', function() { console.log('guildMemberRemove', Array.from(arguments)) })
+bot.on('guildMembersChunk', function() { console.log('guildMembersChunk', Array.from(arguments)) })
+bot.on('guildMemberSpeaking', function() { console.log('guildMemberSpeaking', Array.from(arguments)) })
+bot.on('guildMemberUpdate', function() { console.log('guildMemberUpdate', Array.from(arguments)) })
+bot.on('guildUnavailable', function() { console.log('guildUnavailable', Array.from(arguments)) })
+bot.on('guildUpdate', function() { console.log('guildUpdate', Array.from(arguments)) })
+bot.on('message', function() { console.log('message', Array.from(arguments)) })
+bot.on('messageDelete', function() { console.log('messageDelete', Array.from(arguments)) })
+bot.on('messageDeleteBulk', function() { console.log('messageDeleteBulk', Array.from(arguments)) })
+bot.on('messageReactionAdd', function() { console.log('messageReactionAdd', Array.from(arguments)) })
+bot.on('messageReactionRemove', function() { console.log('messageReactionRemove', Array.from(arguments)) })
+bot.on('messageReactionRemoveAll', function() { console.log('messageReactionRemoveAll', Array.from(arguments)) })
+bot.on('messageUpdate', function() { console.log('messageUpdate', Array.from(arguments)) })
+bot.on('presenceUpdate', function() { console.log('presenceUpdate', Array.from(arguments)) })
+bot.on('rateLimit', function() { console.log('rateLimit', Array.from(arguments)) })
+bot.on('ready', function() { console.log('ready', Array.from(arguments)) })
+bot.on('reconnecting', function() { console.log('reconnecting', Array.from(arguments)) })
+bot.on('resume', function() { console.log('resume', Array.from(arguments)) })
+bot.on('roleCreate', function() { console.log('roleCreate', Array.from(arguments)) })
+bot.on('roleDelete', function() { console.log('roleDelete', Array.from(arguments)) })
+bot.on('roleUpdate', function() { console.log('roleUpdate', Array.from(arguments)) })
+bot.on('typingStart', function() { console.log('typingStart', Array.from(arguments)) })
+bot.on('typingStop', function() { console.log('typingStop', Array.from(arguments)) })
+bot.on('userNoteUpdate', function() { console.log('userNoteUpdate', Array.from(arguments)) })
+bot.on('userUpdate', function() { console.log('userUpdate', Array.from(arguments)) })
+bot.on('voiceStateUpdate', function() { console.log('voiceStateUpdate', Array.from(arguments)) })
+bot.on('warn', function() { console.log('warn', Array.from(arguments)) })
+*/
+
+bot.login('NTA5NzMzOTczNjQ0Mjc5ODE4.DsSLOw.AeKUx4C-ceWxHYjf1tbephaRDxw')
+
+module.exports = bot

+ 97 - 0
dice.js

@@ -0,0 +1,97 @@
+const diceRegex = () => /\b(\d*)[dD](\d+)\b(?:\s?([+-])\s?(\d+))?/g
+
+class Roll {
+  constructor(roll) {
+    if (roll) Object.assign(this, roll)
+  }
+  value() {
+    return this.values.reduce((a, b) => a + b, this.bonus || 0)
+  }
+  toString() {
+    let str = this.values.map(val => `[${val}]`).join(' + ')
+    if (this.bonus < 0) {
+      str += ` - ${Math.abs(this.bonus)}`
+    } else if (this.bonus > 0) {
+      str += ` + ${this.bonus}`
+    }
+    if (this.values.length > 1 || this.bonus) {
+      str += ` = ${this.value()}`
+    }
+    return str
+  }
+}
+
+class BadRoll extends Roll {
+  constructor(text) {
+    super({text})
+  }
+  toString() {
+    return this.text
+  }
+}
+
+class Dice {
+  constructor(dice) {
+    if (dice) Object.assign(this, dice)
+  }
+  roll() {
+    if (this.count > 100) return new BadRoll(`I lost count rolling ${this}`)
+    if (this.sides > 256) return new BadRoll(`The faces are too small to read on ${this}`)
+    if (this.count === 0) return new BadRoll(`I finished before I started rolling ${this}`)
+    if (this.sides === 0) return new BadRoll(`I lost my grip on ${this}`)
+    if (this.sides === 1) return new BadRoll(`The mobius ${this} never stopped rolling`)
+    if (this.bonusSign === '-' && this.bonus > this.sides) return new BadRoll(`It doesn't seem fair to roll ${this}`)
+
+    let values = []
+    for (let i = 0; i < this.count; i++) {
+      values.push(Math.floor(Math.random() * this.sides) + 1)
+    }
+    let bonus = 0
+    switch (this.bonusSign) {
+      case '-': bonus = -this.bonus; break
+      case '+': bonus = this.bonus; break
+    }
+    return new Roll({
+      values,
+      bonus
+    })
+  }
+  toString() {
+    let str = `${this.count}d${this.sides}`
+    if (this.bonusSign) {
+      str += this.bonusSign + this.bonus
+    }
+    return str
+  }
+}
+
+Dice.parse = str => {
+  const regex = diceRegex()
+  const ret = []
+  let dice
+  while (dice = regex.exec(str)) {
+    ret.push(new Dice({
+      match: dice,
+      count: dice[1] === '' ? 1 : parseFloat(dice[1]),
+      sides: parseFloat(dice[2]),
+      bonusSign: dice[3] || null,
+      bonus: dice[4] === undefined ? null : parseFloat(dice[4])
+    }))
+  }
+  return ret
+}
+
+Dice.chat = chat => {
+  const dice = Dice.parse(chat)
+  if (dice.length) {
+    const diceStrings = dice.map(x => x.toString())
+    if (diceStrings.length > 1) {
+      diceStrings[diceStrings.length - 1] = `and ${diceStrings[diceStrings.length - 1]}`
+    }
+    const rollingString = `Rolling ${diceStrings.join(', ')}...`
+    const rollsStrings = dice.map(die => die.roll().toString())
+    return `${rollingString} ${rollsStrings.join(', ')}.`
+  }
+}
+
+module.exports = {Dice, Roll}

+ 5 - 0
notes

@@ -0,0 +1,5 @@
+Permissions integer: 523328
+
+https://discordapp.com/oauth2/authorize?client_id=509733973644279818&scope=bot&permissions=523328
+
+

+ 9 - 0
package.json

@@ -0,0 +1,9 @@
+{
+  "name": "dicebot",
+  "version": "1.0.0",
+  "main": "index.js",
+  "license": "MIT",
+  "dependencies": {
+    "discord.js": "^11.4.2"
+  }
+}

+ 52 - 0
yarn.lock

@@ -0,0 +1,52 @@
+# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
+# yarn lockfile v1
+
+
+async-limiter@~1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
+  integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==
+
+discord.js@^11.4.2:
+  version "11.4.2"
+  resolved "https://registry.yarnpkg.com/discord.js/-/discord.js-11.4.2.tgz#54586981926521572051f2a30b984aad2b49786e"
+  integrity sha512-MDwpu0lMFTjqomijDl1Ed9miMQe6kB4ifKdP28QZllmLv/HVOJXhatRgjS8urp/wBlOfx+qAYSXcdI5cKGYsfg==
+  dependencies:
+    long "^4.0.0"
+    prism-media "^0.0.3"
+    snekfetch "^3.6.4"
+    tweetnacl "^1.0.0"
+    ws "^4.0.0"
+
+long@^4.0.0:
+  version "4.0.0"
+  resolved "https://registry.yarnpkg.com/long/-/long-4.0.0.tgz#9a7b71cfb7d361a194ea555241c92f7468d5bf28"
+  integrity sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA==
+
+prism-media@^0.0.3:
+  version "0.0.3"
+  resolved "https://registry.yarnpkg.com/prism-media/-/prism-media-0.0.3.tgz#8842d4fae804f099d3b48a9a38e3c2bab6f4855b"
+  integrity sha512-c9KkNifSMU/iXT8FFTaBwBMr+rdVcN+H/uNv1o+CuFeTThNZNTOrQ+RgXA1yL/DeLk098duAeRPP3QNPNbhxYQ==
+
+safe-buffer@~5.1.0:
+  version "5.1.2"
+  resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.1.2.tgz#991ec69d296e0313747d59bdfd2b745c35f8828d"
+  integrity sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==
+
+snekfetch@^3.6.4:
+  version "3.6.4"
+  resolved "https://registry.yarnpkg.com/snekfetch/-/snekfetch-3.6.4.tgz#d13e80a616d892f3d38daae4289f4d258a645120"
+  integrity sha512-NjxjITIj04Ffqid5lqr7XdgwM7X61c/Dns073Ly170bPQHLm6jkmelye/eglS++1nfTWktpP6Y2bFXjdPlQqdw==
+
+tweetnacl@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/tweetnacl/-/tweetnacl-1.0.0.tgz#713d8b818da42068740bf68386d0479e66fc8a7b"
+  integrity sha1-cT2LgY2kIGh0C/aDhtBHnmb8ins=
+
+ws@^4.0.0:
+  version "4.1.0"
+  resolved "https://registry.yarnpkg.com/ws/-/ws-4.1.0.tgz#a979b5d7d4da68bf54efe0408967c324869a7289"
+  integrity sha512-ZGh/8kF9rrRNffkLFV4AzhvooEclrOH0xaugmqGsIfFgOE/pIz4fMc4Ef+5HSQqTEug2S9JZIWDR47duDSLfaA==
+  dependencies:
+    async-limiter "~1.0.0"
+    safe-buffer "~5.1.0"