| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- const Cache = require('cache')
- const { Dice } = require('./dice')
- const SeedRandom = require('seedrandom')
- var { Client, Attachment } = require('discord.js');
- var bot = new Client()
- /*
- bot.on('ready', function() {
- // console.log('ready', arguments)
- console.log('Logged in as %s - %s\n', bot.username, bot.id);
- });
- */
- setInterval(() => {
- // Chew through randomness to make it more unpredictable.
- Math.random()
- })
- const cache = new Cache(60 * 60 * 1000)
- const formatChatLine = response => response.split('[').join('`').split(']').join('`')
- .replace(/= (\d+)\./g, (a, b) => `= **${b}**.`)
- function handleMessage(message) {
- if (message.author.id === bot.user.id) return;
- const session = cache.get(message.id) || { seed: message.id }
- const rng = new SeedRandom(session.seed)
- const response = Dice.chat(message.content, {
- rng: () => rng.double()
- })
- console.log(`message from ${message.author.username}: ${message.content}`)
- if (response) {
- console.log(response)
- const chatLine = formatChatLine(response)
- console.log(chatLine)
- if (session.reply) {
- session.reply.edit(chatLine).then(reply => {
- session.reply = reply
- })
- } else {
- message.reply(chatLine).then(reply => {
- session.reply = reply
- })
- }
- cache.put(message.id, session)
- } else {
- if (session.reply) {
- session.reply.delete()
- session.reply = null
- }
- }
- }
- bot.on('message', handleMessage);
- bot.on('messageUpdate', function(oldMessage, newMessage) {
- const session = cache.get(oldMessage.id)
- if (session) {
- cache.put(newMessage.id, session)
- }
- handleMessage(newMessage)
- })
- bot.on('messageDelete', function(message) {
- const session = cache.get(message.id)
- if (session && session.reply) {
- session.reply.delete()
- session.reply = null
- }
- })
- bot.on('error', function(err) {
- console.error(err)
- process.exit(1)
- })
- /*l
- 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
|