Skip to content

Commit e149191

Browse files
committed
IIRR-12 Add an event for when an admin adds the bot to a server
1 parent bf250e0 commit e149191

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

app/lib/discord/bot.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,23 @@ 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
26+
end
27+
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)
2541
end
2642
end
2743
end
28-
end
44+
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: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
admin = server.owner
17+
system_channel = server.system_channel
18+
19+
# Ensure system_channel exists and send a message to the server
20+
if system_channel
21+
system_channel.send_message("Hello #{server.name}! I am the 'Is it Ruby or Rails bot, I will be sending a question every day to see if your users know what is Ruby and what is Rails! Good luck! Admins: You can adjust which channel you want me to ask questions in by using the '/set_channel' command.")
22+
else
23+
puts "No suitable channel found to add the bot to."
24+
end
25+
end
26+
27+
private
28+
29+
attr_reader :bot
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)