Skip to content

Commit eb9c00a

Browse files
FionaDLABizzinotto
authored andcommitted
IIRR-13 Reconfigure the db for channel and server
1 parent c8d4a3f commit eb9c00a

File tree

9 files changed

+65
-30
lines changed

9 files changed

+65
-30
lines changed

app/lib/discord/events/server_create.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,19 @@ def welcome_message(server_name)
4444

4545
def create_server(server)
4646
# Only create a new record if it doesn't already exist
47-
return if DiscordServer.exists?(discord_id: server.id)
47+
return if Server.exists?(server_id: server.id)
4848

49-
# Create the new DiscordServer record
50-
DiscordServer.create!(
51-
discord_id: server.id,
52-
system_channel_id: server.system_channel&.id, # Optional, if available
53-
send_questions_channel_id: server.system_channel&.id # Optional, if available
49+
# Create the new Server record
50+
discord_server = Server.create!(
51+
server_id: server.id,
52+
name: server.name
5453
)
54+
if server.system_channel
55+
discord_server.channels.create!(
56+
channel_id: server.system_channel.id,
57+
name: server.system_channel.name
58+
)
59+
end
5560
end
5661
end
5762
end

app/lib/discord/events/set_channel.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,17 @@ def listen_to_channel_select
5151
# Listen for the channel select event
5252
@bot.channel_select(custom_id: "set_channel_select") do |event|
5353
channel = event.values.first # Get the selected channel
54-
server = DiscordServer.find_by!(discord_id: event.server.id)
54+
server = Server.find_by!(server_id: event.server.id)
5555

56-
# Update the server with the new channel ID
57-
server.update(send_questions_channel_id: channel.id)
56+
existing_channel = server.channels.find_by(channel_id: channel.id)
57+
58+
if existing_channel
59+
# If the channel exists, update it
60+
existing_channel.update!(name: channel.name, channel_id: channel.id)
61+
else
62+
# If the channel doesn't exist, create a new one associated with the server
63+
server.channels.create!(name: channel.name, channel_id: channel.id)
64+
end
5865

5966
# Respond with a confirmation message
6067
event.respond(content: "Thank you! Ruby or Rails questions will be sent to #{channel.name} from now on.", ephemeral: true)

app/models/channel.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Channel < ApplicationRecord
2+
belongs_to :server
3+
end

app/models/discord_server.rb

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/models/server.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Server < ApplicationRecord
2+
has_many :channels
3+
end

db/migrate/20250318172735_create_discord_servers.rb

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateServers < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :servers do |t|
4+
t.string :server_id, null: false
5+
t.string :name
6+
7+
t.timestamps
8+
end
9+
add_index :servers, :server_id, unique: true
10+
end
11+
end
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CreateChannels < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :channels do |t|
4+
t.references :server, null: false, foreign_key: true
5+
t.string :channel_id, null: false
6+
t.string :name
7+
8+
t.timestamps
9+
end
10+
end
11+
end

db/schema.rb

Lines changed: 16 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)