-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.rb
executable file
·66 lines (49 loc) · 1.73 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'discordrb'
require 'docopt'
require 'yaml'
doc = <<~DOCOPT
The Bishop moderation bot.
Usage:
#{__FILE__} [--config-file=FILE] [--roles-file=FILE]
Options:
-h --help Show this screen.
-c --config-file FILE Path to config file [default: config.yml]
-r --roles-file FILE Path to roles file [default: roles.yml]
DOCOPT
OPTIONS = Docopt.docopt(doc)
Discordrb::LOGGER.debug "Options: #{OPTIONS}"
CONFIG = YAML.load_file(OPTIONS['--config-file'])
ROLES = YAML.load_file(OPTIONS['--roles-file'])
# initial load of command modules
Dir.glob(File.join('modules', '*.rb')).each { |f| load f }
bot = Discordrb::Commands::CommandBot.new token: CONFIG['token'], prefix: CONFIG['prefix']
def reload_modules(bot)
# remove all current handlers (except !help)
bot.clear!
bot.commands.keys.grep_v(:help).each { |c| bot.remove_command(c) }
# "unload" modules by un-defining container name
Bishop::Modules.constants.each { |m| Bishop::Modules.send(:remove_const, m) }
# force reload with load instead of require
Dir.glob(File.join('modules', '*.rb')).each { |f| load f }
# re-register handlers into bot
Bishop::Modules.constants.each do |m|
Discordrb::LOGGER.info "Loading #{m}"
bot.include! Bishop::Modules.const_get(m)
end
end
# load modules
reload_modules(bot)
# register slash commands
# this only needs to happen once so dont do it on !reload
Bishop::Modules::SlashCommands.register_commands(bot)
bot.ready do
bot.listening = 'Evan'
Discordrb::LOGGER.info "Logged in as #{bot.profile.username}"
# ensure role message is up to date
SharedRoleComponents.update_select_message(bot)
end
# start bot
at_exit { bot.stop }
bot.run