Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
Arthur POULET | efaf244bb3 |
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"BasicCommands": {
|
||||
"enabled": true
|
||||
}
|
||||
}
|
|
@ -2,8 +2,11 @@
|
|||
|
||||
require "crirc"
|
||||
require "rollable"
|
||||
require "./DashBot/*"
|
||||
require "./DashBot/plugins/*"
|
||||
require "yaml"
|
||||
require "json"
|
||||
|
||||
class DashBot
|
||||
end
|
||||
|
||||
# Extention of `String`.
|
||||
module DashBot::Source
|
||||
|
@ -24,21 +27,44 @@ class String
|
|||
include DashBot::Source
|
||||
end
|
||||
|
||||
module DashBot
|
||||
module Loader
|
||||
def fetch_configuration(file)
|
||||
configuration = File.read(file)
|
||||
if file.ends_with?(".yml") || file.ends_with?(".yaml")
|
||||
YAML.parse(configuration)
|
||||
else
|
||||
JSON.parse(configuration)
|
||||
end
|
||||
end
|
||||
|
||||
macro bind_plugins(config, *plugins)
|
||||
{% for plugin in plugins %}
|
||||
plugin = Plugins::{{plugin.id}}
|
||||
plugin_config = Hash(String, String).new
|
||||
config[{{plugin}}].each { |k, v| plugin_config[k.to_s] = v.to_s }
|
||||
plugin_instance = plugin.new(plugin_config)
|
||||
plugin_instance.bind(bot) if plugin_instance.enabled?
|
||||
{% end %}
|
||||
end
|
||||
|
||||
extend self
|
||||
end
|
||||
|
||||
require "./DashBot/*"
|
||||
require "./DashBot/plugins/*"
|
||||
|
||||
class DashBot
|
||||
def initialize
|
||||
@config = Hash(String, Hash(String, String)).new
|
||||
end
|
||||
|
||||
def start
|
||||
Arguments.new.use
|
||||
client = Crirc::Network::Client.new(ip: "irc.mozilla.org", port: 6667_u16, ssl: false, nick: "Dasshyx#{rand(1..9)}", read_timeout: 300_u16)
|
||||
client.connect
|
||||
client.start do |bot|
|
||||
Plugins::BasicCommands.bind(bot)
|
||||
Plugins::UserCommands.bind(bot)
|
||||
Plugins::AdminCommands.bind(bot)
|
||||
Plugins::Points.bind(bot)
|
||||
Plugins::Messages.bind(bot)
|
||||
Plugins::Reminder.bind(bot)
|
||||
Plugins::Rpg.bind(bot)
|
||||
Plugins::Random.bind(bot)
|
||||
# Plugins::Anarchism.bind(bot)
|
||||
config = Loader.fetch_configuration("config.json")
|
||||
Loader.bind_plugins(config, "BasicCommands", "UserCommands", "AdminCommands", "Points", "Messages", "Reminder", "Rpg", "Random", "Anarchism"
|
||||
|
||||
bot.on_ready do
|
||||
bot.join (ARGV.empty? ? ["#equilibre"] : ARGV).map { |chan| Crirc::Protocol::Chan.new(chan) }
|
||||
|
@ -56,13 +82,11 @@ module DashBot
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
extend self
|
||||
end
|
||||
|
||||
loop do
|
||||
begin
|
||||
DashBot.start
|
||||
DashBot.new.start
|
||||
rescue err
|
||||
STDERR.puts err
|
||||
STDOUT.puts err
|
||||
|
|
|
@ -4,16 +4,13 @@ require "pg"
|
|||
# Load env
|
||||
Dotenv.load
|
||||
|
||||
module DashBot
|
||||
module DashBot::Database
|
||||
DB = PG.connect(ENV["PG_URL"])
|
||||
extend self
|
||||
|
||||
module Database
|
||||
extend self
|
||||
|
||||
def user_exists?(name)
|
||||
n = DB.query_one("SELECT COUNT(*) AS count FROM users WHERE name=$1", [name], as: {Int64})
|
||||
raise "User \"#{name}\" registered #{n} times !!" if n > 1
|
||||
n == 1
|
||||
end
|
||||
def user_exists?(name)
|
||||
n = DB.query_one("SELECT COUNT(*) AS count FROM users WHERE name=$1", [name], as: {Int64})
|
||||
raise "User \"#{name}\" registered #{n} times !!" if n > 1
|
||||
n == 1
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,38 @@
|
|||
class DashBot::Plugin
|
||||
@configuration : Hash(String, String)
|
||||
|
||||
# def bind(bot)
|
||||
|
||||
def initialize(config : Hash(String, String))
|
||||
@configuration = { "enabled" => "false" }
|
||||
config.each { |k, v| @configuration[k] = v }
|
||||
end
|
||||
|
||||
def initialize
|
||||
@configuration = { "enabled" => "false" }
|
||||
end
|
||||
|
||||
def enable
|
||||
@configuration["enabled"] = "true"
|
||||
end
|
||||
|
||||
def disable
|
||||
@configuration["enabled"] = "false"
|
||||
end
|
||||
|
||||
def enabled?
|
||||
@configuration["enabled"] == "true"
|
||||
end
|
||||
|
||||
def disabled?
|
||||
!enabled?
|
||||
end
|
||||
|
||||
def configure(key : String, value : String)
|
||||
@configuration[key] = value
|
||||
end
|
||||
|
||||
def configuration(key : String)
|
||||
@configuration[key]? || ""
|
||||
end
|
||||
end
|
|
@ -1,7 +1,7 @@
|
|||
module DashBot::Plugins::Anarchism
|
||||
extend self
|
||||
@@is_op = false
|
||||
@@current_chan : Crirc::Protocol::Chan|Nil = nil
|
||||
@@current_chan : Crirc::Protocol::Chan | Nil = nil
|
||||
@@user_list = Array(Crirc::Protocol::User).new
|
||||
|
||||
def bind(bot)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
module DashBot::Plugins::BasicCommands
|
||||
extend self
|
||||
require "../plugin"
|
||||
|
||||
class DashBot::Plugins::BasicCommands < DashBot::Plugin
|
||||
WHAT = %w(ce que tu dis n'a aucun sens)
|
||||
|
||||
def bind(bot)
|
||||
|
|
|
@ -1,21 +1,19 @@
|
|||
module DashBot
|
||||
module Rights
|
||||
def authorized?(msg, group = "admin")
|
||||
n = DB.query_one("SELECT COUNT(*) FROM groups
|
||||
INNER JOIN users ON groups.user_name = users.name WHERE groups.name = $1 AND users.name = $2",
|
||||
[group, msg.source.source_id], as: {Int64})
|
||||
n == 1
|
||||
end
|
||||
module DashBot::Rights
|
||||
def authorized?(msg, group = "admin")
|
||||
n = DB.query_one("SELECT COUNT(*) FROM groups
|
||||
INNER JOIN users ON groups.user_name = users.name WHERE groups.name = $1 AND users.name = $2",
|
||||
[group, msg.source.source_id], as: {Int64})
|
||||
n == 1
|
||||
end
|
||||
|
||||
def authorized?(msg, groups : Array(String))
|
||||
groups.any? { |group| authorized? msg, group }
|
||||
end
|
||||
def authorized?(msg, groups : Array(String))
|
||||
groups.any? { |group| authorized? msg, group }
|
||||
end
|
||||
|
||||
def authorize!(bot, msg : Crirc::Protocol::Message, group = "admin", reply = "Unauthorized")
|
||||
return true if authorized? msg, group
|
||||
bot.reply msg, reply
|
||||
false
|
||||
end
|
||||
def authorize!(bot, msg : Crirc::Protocol::Message, group = "admin", reply = "Unauthorized")
|
||||
return true if authorized? msg, group
|
||||
bot.reply msg, reply
|
||||
false
|
||||
end
|
||||
|
||||
extend self
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
module DashBot
|
||||
class DashBot
|
||||
VERSION = "0.1.0"
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue