6 Commits 412cc26860 ... da7e926f6d

Author SHA1 Message Date
  Andrew Swistak da7e926f6d Attempt to fix connection pooling issues 5 years ago
  Andrew Swistak eceeae1e71 Add server deployment config 5 years ago
  Andrew Swistak 7ef63c298e Prepare docker-compose for VPS deployment 5 years ago
  Kylie Jo Swistak 3073a4ac64 update numbers to represent hexadecimal pattern 5 years ago
  Kylie Jo Swistak 221999c26a update attachment value to 50 chars 5 years ago
  Kylie Jo Swistak 4da6d9f58b alter xp and begin collecting message length statistics 5 years ago
7 changed files with 57 additions and 14 deletions
  1. 1 2
      .env.template
  2. 15 3
      app/models/users.rb
  3. 12 6
      bot.rb
  4. 9 0
      deploy/deploy.sh
  5. 15 0
      deploy/rotom-bot.service
  6. 4 2
      docker-compose.yml
  7. 1 1
      lib/emoji.rb

+ 1 - 2
.env.template

@@ -1,9 +1,8 @@
 POSTGRES_USER=rotom
 POSTGRES_PASSWORD=PleaseChangeMe:D
 POSTGRES_DB=pmd
+POSTGRES_PORT=5432
 
 DISCORD_CLIENT_ID=
 DISCORD_SECRET=
 DISCORD_BOT_TOKEN=
-
-DB_HOST_PORT=6543

+ 15 - 3
app/models/users.rb

@@ -39,9 +39,8 @@ class User < ActiveRecord::Base
   end
 
   def update_xp(msg_length, member=nil)
-    xp =
+    old_xp =
       case msg_length
-      when 0..39 then 0
       when 40..149 then 1
       when 150..299 then 2
       when 300..599 then 3
@@ -49,9 +48,22 @@ class User < ActiveRecord::Base
       else 5
       end
 
+    multiplier =
+      case msg_length
+      when 50..299 then 1
+      when 300..649 then 1.5
+      when 650..999 then 1
+      else 0.5
+      end
+
+    xp = (msg_length/50 * multiplier).to_i
+
     self.update(
       boosted_xp: boosted_xp + xp,
-      unboosted_xp: unboosted_xp + xp
+      unboosted_xp: unboosted_xp + xp,
+      additional_xp: additional_xp + (xp - old_xp),
+      post_length: post_length + msg_length,
+      post_count: post_count + 1
     )
 
     self.reload

+ 12 - 6
bot.rb

@@ -34,7 +34,8 @@ ActiveRecord::Base.establish_connection(
   host: db_config.fetch('host') { 'localhost' },
   database: db_config['database'],
   user: db_config['user'],
-  password: db_config['password']
+  password: db_config['password'],
+  pool: 5
 )
 
 Dir['app/**/*.rb'].each { |f| require File.join(File.expand_path(__dir__), f) }
@@ -95,19 +96,24 @@ bot.message do |event|
   # Apply experience to non-bot users
   elsif !author.bot_account? && !author.webhook?
     # Replace any urls with 150 'x' chars
-    message = URL.match(content) ? content.gsub(URL, "x" * 150) : content
+    message = URL.match(content) ? content.gsub(URL, "x" * 100) : content
 
     # Add 40 to the message length if there's a file
     msg_length = event.message.attachments.map(&:filename).count > 0 ?
-      40 + message.length : message.length
+      50 + message.length : message.length
 
-    if msg_length >= 40
-      # Wait until now to find user, so DB isn't touched for every message
-      user = User.find(author.id)
+    # Record post lengths and count
+    user = User.find(author.id)
 
+    if msg_length >= 40
       # Update User and reply with image, if there is one
       reply =  user.update_xp(msg_length, author)
       bot.send_file(event.channel.id, File.open(reply, 'r')) if reply
+    else
+      user.update(
+        short_post_length: user.short_post_length + msg_length,
+        short_post_count: user.short_post_count + 1
+      )
     end
   end
 

+ 9 - 0
deploy/deploy.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+
+cd $HOME/rotom_bot
+git pull
+
+docker build . -t rotom_bot:latest
+docker stop rotom_bot
+docker run --env-file .env --name rotom_bot --rm -d rotom_bot:latest
+docker image prune -f

+ 15 - 0
deploy/rotom-bot.service

@@ -0,0 +1,15 @@
+[Unit]
+Description=Docker Compose Encapsulated Rotom Discord Bot
+Requires=docker.service
+After=docker.service
+
+[Service]
+Type=oneshot
+RemainAfterExit=yes
+WorkingDirectory=/srv/docker
+ExecStart=/usr/local/bin/docker-compose up -d
+ExecStop=/usr/local/bin/docker-compose down
+TimeoutStartSec=0
+
+[Install]
+WantedBy=multi-user.target

+ 4 - 2
docker-compose.yml

@@ -2,6 +2,7 @@ version: "3.7"
 
 services:
   bot:
+    restart: unless-stopped
     build: .
     depends_on:
       - db
@@ -9,20 +10,21 @@ services:
       - POSTGRES_DB
       - POSTGRES_HOST=db
       - POSTGRES_PASSWORD
-      - POSTGRES_PORT=6543
+      - POSTGRES_PORT
       - POSTGRES_USER
       - DISCORD_CLIENT_ID
       - DISCORD_SECRET
       - DISCORD_BOT_TOKEN
 
   db:
+    restart: unless-stopped
     image: postgres:11.2-alpine
     environment:
       - POSTGRES_DB
       - POSTGRES_PASSWORD
       - POSTGRES_USER
     ports:
-      - ${DB_HOST_PORT}:6543
+      - ${POSTGRES_PORT}:5432
     volumes:
       - postgres-persisted-volume:/var/lib/postgresql/data
 

+ 1 - 1
lib/emoji.rb

@@ -90,7 +90,7 @@ module Emoji
 
 
   LETTERS = [A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z]
-  NUMBERS = [ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN]
+  NUMBERS = [ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, A, B, C, D, E, F]
 
   CHAR_APP = [SPEECH_BUBBLE, PICTURE, BOOKS, NOTE, QUESTION, PEOPLE, WIZARD]
   IMG_APP = [DOG, KEY, FLAG, PAGE, BOOKS, VULGAR]