-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrabblebot.rb
70 lines (62 loc) · 1.93 KB
/
rabblebot.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
67
68
69
70
require 'discordrb'
require 'pstore'
require 'yaml'
require_relative 'lib/rabblebot/rabble_bot_plugin'
# This is a simple discord bot that supports plugins
class RabbleBot
attr_accessor :token, :app_id, :bot, :brain, :plugins, :config
# expects you to set environment variables DISCORD_TOKEN and DISCORD_APP_ID
# I used env vars because something something elastic beanstalk. (lol)
def initialize
load_config
@token = ENV.fetch('DISCORD_TOKEN', @config[:DISCORD_TOKEN])
@app_id = ENV.fetch('DISCORD_APP_ID', @config[:DISCORD_APP_ID])
@bot = my_bot
@bot.info "The first few characters of the discord token are: #{@token[0, 5]}"
@bot.info "App_id is: #{@app_id}"
end
# a shim for unit testing
def my_bot
Discordrb::Bot.new token: @token, application_id: @app_id
end
def load_plugins
@bot.debug 'Loading plugins'
RabbleBotPlugin.bot = @bot
RabbleBotPlugin.require_plugins
@plugins = RabbleBotPlugin.plugins.map do |plugin|
@bot.info "Loading #{plugin}"
config_name = plugin.to_s.split('::').last.downcase
plugin.new @bot, @config[config_name]
end
@bot.debug 'Done loading plugins'
end
def load_config
config_file_path = File.join(File.dirname(File.expand_path(__FILE__)), '/brains/config.yml')
@config = YAML.load_file(config_file_path)
rescue StandardError => e
# we're using STDERR.puts here because we don't have a logger yet
STDERR.puts "Couldn't load #{config_file_path}. Error was:"
STDERR.puts e
raise e
end
# set up the event handlers, spout out an oAuth url
def bootstrap
STDERR.puts "My oauth authorization URL is: #{@bot.invite_url}"
load_plugins
end
# connect to discord and do stuff
def run
@bot.run
end
end
# Monkey Patch .info into Discordrb
unless Discordrb::Bot.respond_to? :info
module Discordrb
# a discord bot
class Bot
def info(message)
LOGGER.info(message)
end
end
end
end