|
| 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 |
0 commit comments