Skip to content

Commit aa0fc7c

Browse files
FionaDLABizzinotto
authored andcommitted
IIRR-13 Add ability for admin to change the channel
1 parent 171c5d7 commit aa0fc7c

6 files changed

Lines changed: 137 additions & 0 deletions

File tree

app/lib/discord/events/server_create.rb

Lines changed: 14 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,18 @@ 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 DiscordServer.exists?(discord_id: server.id)
48+
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
54+
)
55+
end
4256
end
4357
end
4458
end
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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 = DiscordServer.find_by!(discord_id: event.server.id)
55+
56+
# Update the server with the new channel ID
57+
server.update(send_questions_channel_id: channel.id)
58+
59+
# Respond with a confirmation message
60+
event.respond(content: "Thank you! Ruby or Rails questions will be sent to #{channel.name} from now on.", ephemeral: true)
61+
end
62+
end
63+
end
64+
end
65+
end
66+

app/models/discord_server.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# To house data and logic related to the discord servers that the bot is added to.
2+
class DiscordServer < ApplicationRecord
3+
4+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class CreateDiscordServers < ActiveRecord::Migration[8.0]
2+
def change
3+
create_table :discord_servers do |t|
4+
t.string :discord_id
5+
t.string :system_channel_id
6+
t.string :send_questions_channel_id
7+
8+
t.timestamps
9+
end
10+
add_index :discord_servers, :discord_id, unique: true
11+
end
12+
end

db/schema.rb

Lines changed: 25 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)