Skip to content

Commit d0a3849

Browse files
authored
Merge pull request #3 from ombulabs/IIRR-13-admin-choice
[IIRR-13] Admin channel choice
2 parents 171c5d7 + b5e4fba commit d0a3849

File tree

8 files changed

+170
-0
lines changed

8 files changed

+170
-0
lines changed

app/lib/discord/events/server_create.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ def handle(event)
2121
else
2222
puts "No suitable channel found to add the bot to."
2323
end
24+
25+
create_server(server)
2426
end
2527

2628
private
@@ -39,6 +41,23 @@ def welcome_message(server_name)
3941
end
4042

4143
attr_reader :bot
44+
45+
def create_server(server)
46+
# Only create a new record if it doesn't already exist
47+
return if Server.exists?(server_id: server.id)
48+
49+
# Create the new Server record
50+
discord_server = Server.create!(
51+
server_id: server.id,
52+
name: server.name
53+
)
54+
if server.system_channel
55+
discord_server.create_channel!(
56+
channel_id: server.system_channel.id,
57+
name: server.system_channel.name
58+
)
59+
end
60+
end
4261
end
4362
end
4463
end
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module Discord
2+
module Events
3+
class SetChannel
4+
def initialize(bot)
5+
@bot = bot
6+
end
7+
8+
def listen
9+
# Listen for the set_channel command
10+
@bot.application_command(:set_channel) do |event|
11+
handle(event)
12+
end
13+
14+
# Listen for select menu event
15+
listen_to_channel_select
16+
end
17+
18+
def handle(event)
19+
user = event.user
20+
server = event.server
21+
22+
# Ensure the user has the necessary permissions to run the command
23+
unless user.permission?(:manage_server)
24+
event.respond("You do not have permission to run this command.")
25+
return
26+
end
27+
display_settings(event)
28+
end
29+
30+
31+
private
32+
33+
def display_settings(event)
34+
content = <<~MSG
35+
Here you can configure the bot to suit the needs of the community!
36+
37+
What's the best channel to send Ruby or Rails questions to?
38+
MSG
39+
channel_select = Discordrb::Components::View.new do |view|
40+
view.row do |row|
41+
row.channel_select(custom_id: "set_channel_select", placeholder: "Discussion prompts channel")
42+
end
43+
end
44+
45+
event.respond(content: content, ephemeral: true, components: channel_select)
46+
end
47+
48+
49+
50+
def listen_to_channel_select
51+
# Listen for the channel select event
52+
@bot.channel_select(custom_id: "set_channel_select") do |event|
53+
channel = event.values.first # Get the selected channel
54+
server = Server.find_by!(server_id: event.server.id)
55+
56+
existing_channel = server.channel
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.create_channel!(name: channel.name, channel_id: channel.id)
64+
end
65+
66+
# Respond with a confirmation message
67+
event.respond(content: "Thank you! Ruby or Rails questions will be sent to #{channel.name} from now on.", ephemeral: true)
68+
end
69+
end
70+
end
71+
end
72+
end

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/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_one :channel
3+
end
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: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/tasks/discord.rake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ namespace :discord do
33
task start_bot: :environment do
44
bot.run
55
end
6+
7+
desc "Clear all globally registered bot commands and re-register them"
8+
task reset_commands: :environment do
9+
# Fetch and delete all existing commands
10+
commands = bot.get_application_commands
11+
commands.each do |command|
12+
bot.delete_application_command(command.id)
13+
end
14+
15+
puts "✅ Cleared all global commands. Now re-registering..."
16+
17+
# Register the commands again
18+
bot.register_application_command(:set_channel, "Set a channel for bot to be used in.")
19+
20+
puts "✅ Re-registered commands!"
21+
end
622
end
723

824
def bot

0 commit comments

Comments
 (0)