Skip to content

Commit 171c5d7

Browse files
authored
Merge pull request #7 from ombulabs/IIRR-11-setup
IIRR-11: Setup
2 parents a4d2e19 + 5cbfb65 commit 171c5d7

File tree

5 files changed

+63
-2
lines changed

5 files changed

+63
-2
lines changed

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
ruby-3.4.2
1+
3.4.2

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ PLATFORMS
389389
aarch64-linux-musl
390390
arm-linux-gnu
391391
arm-linux-musl
392+
arm64-darwin-23
392393
arm64-darwin-24
393394
x86_64-linux
394395
x86_64-linux-gnu

app/lib/discord/bot.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,24 @@ def initialize(configuration)
2222
@bot.ready do
2323
Rails.logger.info "✅ Bot is online and connected to Discord!"
2424
puts "✅ Bot is online and connected to Discord!"
25+
setup
2526
end
2627
end
28+
29+
private
30+
31+
def setup
32+
turn_events_on
33+
end
34+
35+
def turn_events_on
36+
event_dir = Rails.root.join("app/lib/discord/events")
37+
Dir.foreach(event_dir) do |file|
38+
next if file == "." || file == ".." # Skip '.' and '..'
39+
event_class = "Discord::Events::#{file.chomp(".rb").camelize}".constantize
40+
event_class.new(@bot).try(:listen)
41+
end
42+
puts "Events registered. Bot is listening..."
43+
end
2744
end
2845
end

app/lib/discord/configuration.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class Configuration
55

66
def initialize
77
@token = nil
8-
# @log_mode = :normal
98
end
109
end
1110
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
module Discord
2+
module Events
3+
# This class listens and handles the event triggered when the bot joins a server.
4+
class ServerCreate
5+
def initialize(bot) # Ensure we accept a bot instance here
6+
@bot = bot
7+
end
8+
9+
def listen
10+
@bot.server_create { |event| handle(event) }
11+
end
12+
13+
def handle(event)
14+
# Send the introduction message to the main server channel.
15+
server = event.server
16+
system_channel = server.system_channel
17+
18+
# Ensure system_channel exists and send a message to the server
19+
if system_channel
20+
system_channel.send_message("", false, welcome_message(server.name))
21+
else
22+
puts "No suitable channel found to add the bot to."
23+
end
24+
end
25+
26+
private
27+
28+
def welcome_message(server_name)
29+
embed = Discordrb::Webhooks::Embed.new(
30+
title: "Hello #{server_name}!",
31+
description: "I am the 'Is it Ruby or Rails' bot! I will be sending a question every day to see if you know what is Ruby and what is Rails! Good luck!",
32+
color: 0xe60000
33+
)
34+
embed.add_field(name: "🧩 Daily Puzzles", value: "You can only answer a question once, but you can revisit older questions you haven't answered yet.")
35+
embed.add_field(name: "🏆 Leaderboard", value: "I'll send you a leaderboard at the end of each week, so you know who's the Ruby or Rails expert of the week!")
36+
embed.add_field(name: "⚙️ Admin Commands", value: "")
37+
embed.add_field(name: "/set_channel", value: "Specify which channel I should post to within the server.")
38+
embed
39+
end
40+
41+
attr_reader :bot
42+
end
43+
end
44+
end

0 commit comments

Comments
 (0)