Browse Source

change yaml configs to ruby configs

master
Alison Watson 1 year ago
parent
commit
79e6dbd495
8 changed files with 109 additions and 63 deletions
  1. +1
    -1
      .gitignore
  2. +41
    -28
      source/backends/discord.rb
  3. +16
    -15
      source/backends/irc.rb
  4. +35
    -0
      source/config_example.rb
  5. +7
    -13
      source/main.rb
  6. +2
    -2
      source/modules/base.rb
  7. +2
    -2
      source/modules/doomrla.rb
  8. +5
    -2
      source/server.rb

+ 1
- 1
.gitignore View File

@@ -1,5 +1,5 @@
*doc
working
*.yml
_config.rb
*.swp
*.vim

+ 41
- 28
source/backends/discord.rb View File

@@ -19,7 +19,7 @@ module Backend_Discord

# A Discord user.
class User < Vrobot4::Server::User
attr_reader :real # The Discordrb::User instance.
attr_reader :real # @return [Discordrb::User] the instance

# @param user [Discordrb::User] the discord user
# @param ops [Array] list of operator role IDs
@@ -43,7 +43,7 @@ module Backend_Discord

# A Discord channel.
class Channel < Vrobot4::Server::Channel
attr_reader :real # The Discordrb::Channel instance.
attr_reader :real # @return [Discordrb::Channel] the instance

# @param chan [Discordrb::Channel] the discord channel
def initialize chan
@@ -58,15 +58,20 @@ module Backend_Discord
# The server type name.
def self.type() "Discord" end

attr_reader :real # @return [Discordrb::Server] the instance

# (see Vrobot4::Server::Server#initialize)
def initialize info, bot, id
# @param real [Discordrb::Server] the instance
def initialize info, bot, real
super info, bot

@id = id
@ops = info["admins"] || []
@hop = info["halfop"] || []
@real = real
@id = real.id

@ops = info[:admins] || []
@hop = info[:halfop] || []

if mods = info["modules"]
if mods = info[:modules]
for mod in mods
load_mod mod
end
@@ -115,14 +120,14 @@ module Backend_Discord
@mprm = {chan: {}, role: {}, glob: {}}
return unless pinf
pinf.each do |pr|
mod = Vrobot4::Module.get_module_type(pr["module"])[:type]
if pr.key? "channel"
mod = Vrobot4::Module.get_module_type(pr[:module])[:type]
if pr.key? :channel
@mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan][mod]
@mprm[:chan][mod][pr["channel"]] = true
elsif pr.key? "roles"
@mprm[:role][mod] = pr["roles"]
elsif pr.key? "enable"
@mprm[:glob][mod] = pr["enable"]
@mprm[:chan][mod][pr[:channel]] = true
elsif pr.key? :roles
@mprm[:role][mod] = pr[:roles]
elsif pr.key? :enable
@mprm[:glob][mod] = pr[:enable]
end
end
end
@@ -141,19 +146,27 @@ module Backend_Discord
@servers = {}

@real = Discordrb::Bot.new \
token: info["apikey"],
client_id: info["client"]
token: info[:apikey],
client_id: info[:client]

@real.ready do |evt|
for _, serv in evt.bot.servers
add_server serv
end
end

@real.message do |evt|
if evt.server
serv = server_by_id evt.server.id
serv = get_server evt.server

m = Vrobot4::Server::Message.new \
msg: evt.message.content,
user: User.new(evt.user, @ops, @hop),
chan: Channel.new(evt.channel),
serv: serv,
reply: -> (text) {evt.respond text},
reply_b: -> (text) {evt.respond "```\n#{text}```"}
bot: self,
reply: -> (text) {evt.channel.send text},
reply_b: -> (text) {evt.channel.send "```\n#{text}```"}

serv.handle_text_cmd m
end
@@ -168,18 +181,18 @@ module Backend_Discord
protected

# Adds a server to the hash.
# @param id [Integer] id of the server
# @return [Discordrb::Server]
def add_server id
Vrobot4.log :DEBUG, "initializing server #{id}"
@servers[id] = Server.new @info[id] || {}, self, id
# @param serv [Discordrb::Server] server instance
# @return [Server]
def add_server serv
Vrobot4.log :DEBUG, "initializing server #{serv.id}"
@servers[serv.id] = Server.new @info[serv.id] || {}, self, serv
end

# Finds a server in the hash, potentially adding it.
# @param id [Integer] id of the server
# @return [Discordrb::Server]
def server_by_id id
@servers[id] || add_server(id)
# @param serv [Discordrb::Server] server instance
# @return [Server]
def get_server serv
@servers[serv.id] || add_server(serv)
end
end
end


+ 16
- 15
source/backends/irc.rb View File

@@ -55,9 +55,9 @@ module Backend_IRC
# (see Vrobot4::Server::Server#initialize)
def initialize info, bot
super
@id = Vrobot4.hash_str info["server"].downcase
@id = Vrobot4.hash_str info[:server].downcase

for mod in info["modules"] do
for mod in info[:modules] do
load_mod mod
end
end
@@ -74,14 +74,14 @@ module Backend_IRC
@mprm = {chan: {}, role: {}, glob: {}}
return unless pinf
pinf.each do |pr|
mod = Vrobot4::Module.get_module_type(pr["module"])[:type]
if pr.key? "channel"
mod = Vrobot4::Module.get_module_type(pr[:module])[:type]
if pr.key? :channel
@mprm[:chan][mod] = ChannelPerms.new unless @mprm[:chan][mod]
@mprm[:chan][mod][pr["channel"].downcase] = true
elsif pr.key? "roles"
@mprm[:role][mod] = pr["roles"]
elsif pr.key? "enable"
@mprm[:glob][mod] = pr["enable"]
@mprm[:chan][mod][pr[:channel].downcase] = true
elsif pr.key? :roles
@mprm[:role][mod] = pr[:roles]
elsif pr.key? :enable
@mprm[:glob][mod] = pr[:enable]
end
end
end
@@ -102,12 +102,12 @@ module Backend_IRC

@bot = Cinch::Bot.new do
configure do |cfg|
cfg.server = info["server"]
cfg.nick = info["nick"] || "vrobot4"
cfg.port = info["port"] if info.key? "port"
cfg.password = info["pass"] if info.key? "pass"
cfg.modes = info["modes"] if info.key? "modes"
cfg.channels = info["channels"] if info.key? "channels"
cfg.server = info[:server]
cfg.nick = info[:nick] || "vrobot4"
cfg.port = info[:port] if info.key? :port
cfg.password = info[:pass] if info.key? :pass
cfg.modes = info[:modes] if info.key? :modes
cfg.channels = info[:channels] if info.key? :channels
cfg.realname = "vrobot4"
cfg.user = "vrobot4"
cfg.message_split_start = "… "
@@ -122,6 +122,7 @@ module Backend_IRC
user: User.new(evt.user, evt.channel),
chan: Channel.new(evt.channel),
serv: this.serv,
bot: this,
reply: -> (text) {evt.reply text},
reply_b: -> (text) {evt.reply text}



+ 35
- 0
source/config_example.rb View File

@@ -0,0 +1,35 @@
module Config
def self.get
{
debug: 0,

load: %w[
./backends/discord.rb
./backends/irc.rb

./modules/util.rb
],

bots: [
{
type: "Discord",
client: your_bot_client_id_here,
apikey: your_api_key_here,
admins: [your_personal_client_id_here],
your_server_id_here => {
modules: %w[Utilities],
},
},
{
type: "IRC",
server: your_server_here,
port: 6667,
channels: %w[#my_cool_channel],
modules: %w[Utilities],
}
]
}
end
end

## EOF

+ 7
- 13
source/main.rb View File

@@ -3,8 +3,6 @@ require './module.rb'
require './server.rb'
require './robots.rb'

require 'yaml'

module Vrobot4
private

@@ -13,7 +11,7 @@ module Vrobot4
# ["type"] The type by name of the bot.
# @return [Vrobot4::Robots::Bot] the loaded bot
def self.load_bot info
type = info["type"]
type = info[:type]
Robots::get_bot_type(type).new info
end

@@ -37,30 +35,26 @@ module Vrobot4
public

# Runs the program.
# @param cfg [IO] configuration file to read
# @param cfg [String] configuration file to read
# @return [void]
def self.main cfg
Thread.abort_on_exception = true

log :INFO, "vrobot version #{Version}"

begin
cfg = YAML.load cfg.read
rescue
log :ERROR, "error reading bot config: #{$!}"
return
end
load cfg
cfg = Config.get

Vrobot4.set_debug(cfg["debug"] || 0)
Vrobot4.set_debug(cfg[:debug] || 0)

cfg["load"].each do |mod|
cfg[:load].each do |mod|
load mod, true
end

begin
bots = []

cfg["bots"].each do |info|
cfg[:bots].each do |info|
bots << load_bot(info)
end



+ 2
- 2
source/modules/base.rb View File

@@ -43,9 +43,9 @@ class Mod_Base < Vrobot4::Module::Module
end

def c_modr m, argv
argv = check_args argv.split, "S", "S"
argv = argv.split
m.serv.drop_mod argv[0]
load argv[1], true if argv.length > 1
load argv[1], true if argv[1]
m.serv.load_mod argv[0]
end



+ 2
- 2
source/modules/doomrla.rb View File

@@ -102,8 +102,8 @@ class Mod_DoomRLA < Vrobot4::Module::Module
def reload_info
@winfo = []
@minfo = []
@info["wep_info"].each {|wi| @winfo += JSON.parse(open(wi).read)}
@info["mon_info"].each {|mi| @minfo += JSON.parse(open(mi).read)}
@info[:wep_info].each {|wi| @winfo += JSON.parse(open(wi).read)}
@info[:mon_info].each {|mi| @minfo += JSON.parse(open(mi).read)}
end
end



+ 5
- 2
source/server.rb View File

@@ -21,12 +21,14 @@ module Vrobot4::Server
attr_reader :user # @return [User] user that triggered this message
attr_reader :chan # @return [Channel] channel this message was sent to
attr_reader :serv # @return [Server] server this message was sent to
attr_reader :bot # @return [Vrobot4::Robots::Bot] bot this was sent to

# @param info [Hash] A hash containing message info. Keys may be omitted.
# [:msg] Plaintext of message.
# [:user] User that triggered this message.
# [:chan] Channel that this message was sent to.
# [:serv] Server this message was sent to.
# [:bot] Bot this message was sent to.
# [:reply] Method that sends a message to the specified channel.
# [:reply_b] Method that sends a large message to the specified channel.
def initialize(**info)
@@ -34,6 +36,7 @@ module Vrobot4::Server
@user = info[:user]
@chan = info[:chan]
@serv = info[:serv]
@bot = info[:bot]
@reply = info[:reply]
@reply_b = info[:reply_b]
end
@@ -65,7 +68,7 @@ module Vrobot4::Server
@bot = bot
@info = info
@modules = [Mod_Base.new(nil)]
load_permissions info["permissions"]
load_permissions info[:permissions]
end

# Loads and initializes a module into the load list.
@@ -77,7 +80,7 @@ module Vrobot4::Server
mt[:servflags] and mt[:servflags] !~ flags then
raise ArgumentError, "Module #{mod} is not valid for this server"
end
@modules << mt[:type].new(@info[mod])
@modules << mt[:type].new(@info[mod.to_sym])
end

# Drops a module from the load list.


Loading…
Cancel
Save