Skip to content

Commit a4d2e19

Browse files
authored
Merge pull request #1 from ombulabs/IIRR-11-setup
IIRR-11 Add setup for app to run
2 parents 9f2ab4f + bf250e0 commit a4d2e19

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

Gemfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ source "https://rubygems.org"
44
gem "rails", "~> 8.0.2"
55
# The modern asset pipeline for Rails [https://github.com/rails/propshaft]
66

7-
gem 'discordrb'
8-
gem 'dotenv-rails'
7+
gem "discordrb"
8+
gem "dotenv-rails"
99

1010
gem "propshaft"
1111
# Use postgresql as the database for Active Record

app/lib/discord/bot.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
module Discord
2+
# This class handles running and setting up the bot.
3+
class Bot
4+
class << self
5+
attr_accessor :configuration
6+
7+
def configure
8+
self.configuration ||= Discord::Configuration.new
9+
if block_given?
10+
yield(configuration)
11+
end
12+
new(configuration)
13+
end
14+
end
15+
16+
attr_accessor :bot
17+
18+
delegate :send_message, to: :bot
19+
20+
def initialize(configuration)
21+
@bot = Discordrb::Bot.new(token: configuration.token)
22+
@bot.ready do
23+
Rails.logger.info "✅ Bot is online and connected to Discord!"
24+
puts "✅ Bot is online and connected to Discord!"
25+
end
26+
end
27+
end
28+
end

app/lib/discord/configuration.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Discord
2+
# This class encapsulates the configuration used by the discord bot.
3+
class Configuration
4+
attr_accessor :token, :log_mode
5+
6+
def initialize
7+
@token = nil
8+
# @log_mode = :normal
9+
end
10+
end
11+
end

env.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DISCORD_BOT_TOKEN=<insert bot token here>

lib/tasks/discord.rake

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace :discord do
2+
desc "Configure and run the discord bot"
3+
task start_bot: :environment do
4+
bot.run
5+
end
6+
end
7+
8+
def bot
9+
@bot ||= begin
10+
bot_wrapper = Discord::Bot.configure do |config|
11+
config.token = ENV.fetch("DISCORD_BOT_TOKEN")
12+
end
13+
bot_wrapper.bot
14+
end
15+
end

0 commit comments

Comments
 (0)