PinaBot is a very simple Redd-based Reddit bot written in Ruby that monitors a subreddit for trigger words and, in turn, posts relevant replies. PinaBot also handles unsubscribe requests and stores these requests in a SQLite database.
- Ruby version 2.6 or greater (probably fine with a lesser version but no testing done to verify that)
- Bundler is needed to install gems that the bot needs
- SQLite 3 is required to save reply history and unsubscriptions
You'll likely want to create a separate Reddit account for your bot, so that it can have a unique name (i.e. PinaBot). After you've done that and have logged in as that bot's account, proceed below:
- Visit Preferences / Apps within Reddit
- Click "create another app..." button and register your app/bot
- Set
REDD_CLIENTin the.envfile or hardcode intopinabot.rb. This value is located directly below the bot's name within the newly created bot in Reddit. It'll look similar to94eF93wWwmZvyg. - Set
REDD_SECRETin the.envfile or hardcode intopinabot.rb. This value is labeled asSecretwithin the newly created bot in Reddit. - Set
REDD_USERNAMEandREDD_PASSWORDfor the Reddit account associated with the bot in.envor hardcode intopinabot.rb.
First, we'll need to install the Gems used by PinaBot:
$ bundle installNext, let's create the database and run our migrations:
$ bundle exec rake db:migratePer the Reddit app guidelines, every app/bot must have a unique User-Agent that must be precisely structured according to their standards. PinaBot builds this User-Agent string from some configuration data within pinabot.rb. It's very important that these variables are configured properly prior to running pinabot.rb.
The following variables within pinabot.rb must be configured prior to first start:
VERSION = '0.0.1' # increment this after making major changes
AUTHOR = 'your-reddit-username' # author/handler of bot (e.g. 'GFZDW')
BOTNAME = 'your-bot-name' # stylize as needed (e.g. 'PiñaBot')
OPTOUT = '!unsubscribe' # trigger phrase to unsubscribe
SUB = 'your-subreddit' # PinaBot can only monitor a single subreddit for the time beingPinaBot listens for trigger words/phrases and replies accordingly. Triggers and replies are set up using the DICTIONARY array within the script:
DICTIONARY = [
{
# Set 1
triggers: [
'pina',
'pineapple'
],
replies: [
'[Mmm... pineapple!](https://i.imgur.com/gw7lBej.jpg)',
'Did somebody mention *la piña?*'
]
},
{
# Set 2
triggers: [
'#takeitback',
'take it back'
],
replies: [
'[Verlander wants to \#TakeItBack!](https://i.imgur.com/ycNuaUv.jpg)',
'[Altuve wants to \#TakeItBack!](https://i.imgur.com/A9te6cQ.jpg)'
]
}
]This DICTIONARY array can handle as many trigger/reply sets as you like, making PinaBot more versatile than some simpler alternatives out there.
PinaBot can be run using the following command:
$ bundle exec ruby pinabot.rbThis will run in user mode and can be exited by typing CTRL-C.
After you've tested your modifications to PinaBot, you will likely want it to run in the background. You can use purpose-built programs like monit to do this or, more simply, have cron handle the job:
$ crontab -e* * * * * ps aux | grep -v grep | grep -q pinabot.rb || bundle exec ruby /path/to/pinabot.rb &This will check every minute to see if pinabot.rb is running and will start it if it isn't running. This also starts PinaBot after a reboot.
Use the Issues tab to post issues with PinaBot and feel free to submit pull requests for recommended changes or bug fixes.
Thanks!