bot.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. const Cache = require('cache')
  2. const { Dice } = require('./dice')
  3. const SeedRandom = require('seedrandom')
  4. var { Client, Attachment } = require('discord.js');
  5. var bot = new Client()
  6. /*
  7. bot.on('ready', function() {
  8. // console.log('ready', arguments)
  9. console.log('Logged in as %s - %s\n', bot.username, bot.id);
  10. });
  11. */
  12. setInterval(() => {
  13. // Chew through randomness to make it more unpredictable.
  14. Math.random()
  15. })
  16. const cache = new Cache(60 * 60 * 1000)
  17. const formatChatLine = response => response.split('[').join('`').split(']').join('`')
  18. .replace(/= (\d+)\./g, (a, b) => `= **${b}**.`)
  19. function handleMessage(message) {
  20. if (message.author.id === bot.user.id) return;
  21. const session = cache.get(message.id) || { seed: message.id }
  22. const rng = new SeedRandom(session.seed)
  23. const response = Dice.chat(message.content, {
  24. rng: () => rng.double()
  25. })
  26. console.log(`message from ${message.author.username}: ${message.content}`)
  27. if (response) {
  28. console.log(response)
  29. const chatLine = formatChatLine(response)
  30. console.log(chatLine)
  31. if (session.reply) {
  32. session.reply.edit(chatLine).then(reply => {
  33. session.reply = reply
  34. })
  35. } else {
  36. message.reply(chatLine).then(reply => {
  37. session.reply = reply
  38. })
  39. }
  40. cache.put(message.id, session)
  41. } else {
  42. if (session.reply) {
  43. session.reply.delete()
  44. session.reply = null
  45. }
  46. }
  47. }
  48. bot.on('message', handleMessage);
  49. bot.on('messageUpdate', function(oldMessage, newMessage) {
  50. const session = cache.get(oldMessage.id)
  51. if (session) {
  52. cache.put(newMessage.id, session)
  53. }
  54. handleMessage(newMessage)
  55. })
  56. bot.on('messageDelete', function(message) {
  57. const session = cache.get(message.id)
  58. if (session && session.reply) {
  59. session.reply.delete()
  60. session.reply = null
  61. }
  62. })
  63. bot.on('error', function(err) {
  64. console.error(err)
  65. process.exit(1)
  66. })
  67. /*l
  68. bot.on('channelCreate', function() { console.log('channelCreate', Array.from(arguments)) })
  69. bot.on('channelDelete', function() { console.log('channelDelete', Array.from(arguments)) })
  70. bot.on('channelPinsUpdate', function() { console.log('channelPinsUpdate', Array.from(arguments)) })
  71. bot.on('channelUpdate', function() { console.log('channelUpdate', Array.from(arguments)) })
  72. bot.on('clientUserGuildSettingsUpdate', function() { console.log('clientUserGuildSettingsUpdate', Array.from(arguments)) })
  73. bot.on('clientUserSettingsUpdate', function() { console.log('clientUserSettingsUpdate', Array.from(arguments)) })
  74. bot.on('debug', function() { console.log('debug', Array.from(arguments)) })
  75. bot.on('disconnect', function() { console.log('disconnect', Array.from(arguments)) })
  76. bot.on('emojiCreate', function() { console.log('emojiCreate', Array.from(arguments)) })
  77. bot.on('emojiDelete', function() { console.log('emojiDelete', Array.from(arguments)) })
  78. bot.on('emojiUpdate', function() { console.log('emojiUpdate', Array.from(arguments)) })
  79. bot.on('error', function() { console.log('error', Array.from(arguments)) })
  80. bot.on('guildBanAdd', function() { console.log('guildBanAdd', Array.from(arguments)) })
  81. bot.on('guildBanRemove', function() { console.log('guildBanRemove', Array.from(arguments)) })
  82. bot.on('guildCreate', function() { console.log('guildCreate', Array.from(arguments)) })
  83. bot.on('guildDelete', function() { console.log('guildDelete', Array.from(arguments)) })
  84. bot.on('guildMemberAdd', function() { console.log('guildMemberAdd', Array.from(arguments)) })
  85. bot.on('guildMemberAvailable', function() { console.log('guildMemberAvailable', Array.from(arguments)) })
  86. bot.on('guildMemberRemove', function() { console.log('guildMemberRemove', Array.from(arguments)) })
  87. bot.on('guildMembersChunk', function() { console.log('guildMembersChunk', Array.from(arguments)) })
  88. bot.on('guildMemberSpeaking', function() { console.log('guildMemberSpeaking', Array.from(arguments)) })
  89. bot.on('guildMemberUpdate', function() { console.log('guildMemberUpdate', Array.from(arguments)) })
  90. bot.on('guildUnavailable', function() { console.log('guildUnavailable', Array.from(arguments)) })
  91. bot.on('guildUpdate', function() { console.log('guildUpdate', Array.from(arguments)) })
  92. bot.on('message', function() { console.log('message', Array.from(arguments)) })
  93. bot.on('messageDelete', function() { console.log('messageDelete', Array.from(arguments)) })
  94. bot.on('messageDeleteBulk', function() { console.log('messageDeleteBulk', Array.from(arguments)) })
  95. bot.on('messageReactionAdd', function() { console.log('messageReactionAdd', Array.from(arguments)) })
  96. bot.on('messageReactionRemove', function() { console.log('messageReactionRemove', Array.from(arguments)) })
  97. bot.on('messageReactionRemoveAll', function() { console.log('messageReactionRemoveAll', Array.from(arguments)) })
  98. bot.on('messageUpdate', function() { console.log('messageUpdate', Array.from(arguments)) })
  99. bot.on('presenceUpdate', function() { console.log('presenceUpdate', Array.from(arguments)) })
  100. bot.on('rateLimit', function() { console.log('rateLimit', Array.from(arguments)) })
  101. bot.on('ready', function() { console.log('ready', Array.from(arguments)) })
  102. bot.on('reconnecting', function() { console.log('reconnecting', Array.from(arguments)) })
  103. bot.on('resume', function() { console.log('resume', Array.from(arguments)) })
  104. bot.on('roleCreate', function() { console.log('roleCreate', Array.from(arguments)) })
  105. bot.on('roleDelete', function() { console.log('roleDelete', Array.from(arguments)) })
  106. bot.on('roleUpdate', function() { console.log('roleUpdate', Array.from(arguments)) })
  107. bot.on('typingStart', function() { console.log('typingStart', Array.from(arguments)) })
  108. bot.on('typingStop', function() { console.log('typingStop', Array.from(arguments)) })
  109. bot.on('userNoteUpdate', function() { console.log('userNoteUpdate', Array.from(arguments)) })
  110. bot.on('userUpdate', function() { console.log('userUpdate', Array.from(arguments)) })
  111. bot.on('voiceStateUpdate', function() { console.log('voiceStateUpdate', Array.from(arguments)) })
  112. bot.on('warn', function() { console.log('warn', Array.from(arguments)) })
  113. */
  114. bot.login('NTA5NzMzOTczNjQ0Mjc5ODE4.DsSLOw.AeKUx4C-ceWxHYjf1tbephaRDxw')
  115. module.exports = bot