|
| 1 | +#!/usr/bin/env ruby |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +require_relative '../lib/pyxis' |
| 5 | +require 'discordrb' |
| 6 | +require 'discordrb/webhooks' |
| 7 | + |
| 8 | +token = File.read(ENV.fetch('PYXIS_DC_RELEASE_TOOLS_TOKEN')) |
| 9 | +bot = Discordrb::Bot.new(token: token, intents: %i[servers]) |
| 10 | + |
| 11 | +def build_command(name, description) |
| 12 | + { |
| 13 | + name: name, |
| 14 | + description: description, |
| 15 | + type: 1, # chat_input |
| 16 | + contexts: [0], # guild |
| 17 | + } |
| 18 | +end |
| 19 | + |
| 20 | +OPTION_TYPES = { |
| 21 | + string: 3, |
| 22 | + boolean: 5, |
| 23 | + numeric: 4, |
| 24 | +}.freeze |
| 25 | + |
| 26 | +def build_options(thor_options) |
| 27 | + options = [] |
| 28 | + |
| 29 | + thor_options.each_pair do |name, option| |
| 30 | + opt = { |
| 31 | + name: name, |
| 32 | + description: option.description, |
| 33 | + type: OPTION_TYPES[option.type], |
| 34 | + required: option.required, |
| 35 | + } |
| 36 | + |
| 37 | + options << opt |
| 38 | + end |
| 39 | + |
| 40 | + options |
| 41 | +end |
| 42 | + |
| 43 | +def build_subcommands(thor_class) |
| 44 | + subcommands = [] |
| 45 | + |
| 46 | + thor_class.commands.each_pair do |name, command| |
| 47 | + cmd = { |
| 48 | + type: 1, # sub_command |
| 49 | + name: name, |
| 50 | + description: command.description, |
| 51 | + options: build_options(command.options), |
| 52 | + } |
| 53 | + |
| 54 | + subcommands << cmd |
| 55 | + end |
| 56 | + |
| 57 | + subcommands |
| 58 | +end |
| 59 | + |
| 60 | +def build_from_thor(thor_class) |
| 61 | + commands = [] |
| 62 | + |
| 63 | + thor_class.commands.each_pair do |name, command| |
| 64 | + cmd = build_command(name, command.description) |
| 65 | + cmd[:options] = |
| 66 | + if thor_class.subcommand_classes.key?(name) |
| 67 | + build_subcommands(thor_class.subcommand_classes[name]) |
| 68 | + else |
| 69 | + build_options(command.options) |
| 70 | + end |
| 71 | + |
| 72 | + commands << cmd |
| 73 | + end |
| 74 | + |
| 75 | + commands |
| 76 | +end |
| 77 | + |
| 78 | +commands = build_from_thor(Pyxis::Cli) |
| 79 | + |
| 80 | +Discordrb::API::Application.bulk_overwrite_global_commands(bot.instance_variable_get(:@token), bot.profile.id, commands) |
| 81 | + |
| 82 | +stop_signals = %w[QUIT INT TERM] |
| 83 | + |
| 84 | +stop_signals.each do |signal| |
| 85 | + Signal.trap(signal) do |
| 86 | + bot.stop |
| 87 | + end |
| 88 | +end |
| 89 | + |
| 90 | +def build_command_from_event(data) |
| 91 | + parts = [] |
| 92 | + |
| 93 | + if data['type'] == 1 |
| 94 | + parts << data['name'] |
| 95 | + data['options'].each do |option| |
| 96 | + parts += build_command_from_event(option) |
| 97 | + end |
| 98 | + elsif [3, 4, 5].include?(data['type']) |
| 99 | + parts << "--#{data['name']}" |
| 100 | + parts << data['value'].to_s |
| 101 | + end |
| 102 | + |
| 103 | + parts |
| 104 | +end |
| 105 | + |
| 106 | +bot.interaction_create do |event| |
| 107 | + event.defer(ephemeral: false) |
| 108 | + |
| 109 | + begin |
| 110 | + command = build_command_from_event(event.interaction.data) |
| 111 | + SemanticLogger['DiscordBot'].info('Executing command', command: command) |
| 112 | + result = Pyxis::Cli.start(command, { debug: true }) |
| 113 | + color = '#4caf50' |
| 114 | + rescue Thor::Error, Pyxis::Error => e |
| 115 | + result = "#{e.class}\n#{e.message}" |
| 116 | + color = '#f44336' |
| 117 | + end |
| 118 | + |
| 119 | + event.edit_response(embeds: [Discordrb::Webhooks::Embed.new(description: result, color: color)]) |
| 120 | +end |
| 121 | + |
| 122 | +bot.run |
0 commit comments