Compare commits
1 Commits
Author | SHA1 | Date |
---|---|---|
Arthur POULET | efaf244bb3 |
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"BasicCommands": {
|
||||||
|
"enabled": true
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,8 +2,11 @@
|
||||||
|
|
||||||
require "crirc"
|
require "crirc"
|
||||||
require "rollable"
|
require "rollable"
|
||||||
require "./DashBot/*"
|
require "yaml"
|
||||||
require "./DashBot/plugins/*"
|
require "json"
|
||||||
|
|
||||||
|
class DashBot
|
||||||
|
end
|
||||||
|
|
||||||
# Extention of `String`.
|
# Extention of `String`.
|
||||||
module DashBot::Source
|
module DashBot::Source
|
||||||
|
@ -24,21 +27,44 @@ class String
|
||||||
include DashBot::Source
|
include DashBot::Source
|
||||||
end
|
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
|
def start
|
||||||
Arguments.new.use
|
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 = 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.connect
|
||||||
client.start do |bot|
|
client.start do |bot|
|
||||||
Plugins::BasicCommands.bind(bot)
|
config = Loader.fetch_configuration("config.json")
|
||||||
Plugins::UserCommands.bind(bot)
|
Loader.bind_plugins(config, "BasicCommands", "UserCommands", "AdminCommands", "Points", "Messages", "Reminder", "Rpg", "Random", "Anarchism"
|
||||||
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)
|
|
||||||
|
|
||||||
bot.on_ready do
|
bot.on_ready do
|
||||||
bot.join (ARGV.empty? ? ["#equilibre"] : ARGV).map { |chan| Crirc::Protocol::Chan.new(chan) }
|
bot.join (ARGV.empty? ? ["#equilibre"] : ARGV).map { |chan| Crirc::Protocol::Chan.new(chan) }
|
||||||
|
@ -56,13 +82,11 @@ module DashBot
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
extend self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
begin
|
begin
|
||||||
DashBot.start
|
DashBot.new.start
|
||||||
rescue err
|
rescue err
|
||||||
STDERR.puts err
|
STDERR.puts err
|
||||||
STDOUT.puts err
|
STDOUT.puts err
|
||||||
|
|
|
@ -4,16 +4,13 @@ require "pg"
|
||||||
# Load env
|
# Load env
|
||||||
Dotenv.load
|
Dotenv.load
|
||||||
|
|
||||||
module DashBot
|
module DashBot::Database
|
||||||
DB = PG.connect(ENV["PG_URL"])
|
DB = PG.connect(ENV["PG_URL"])
|
||||||
|
extend self
|
||||||
|
|
||||||
module Database
|
def user_exists?(name)
|
||||||
extend self
|
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
|
||||||
def user_exists?(name)
|
n == 1
|
||||||
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
|
end
|
||||||
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
|
module DashBot::Plugins::Anarchism
|
||||||
extend self
|
extend self
|
||||||
@@is_op = false
|
@@is_op = false
|
||||||
@@current_chan : Crirc::Protocol::Chan|Nil = nil
|
@@current_chan : Crirc::Protocol::Chan | Nil = nil
|
||||||
@@user_list = Array(Crirc::Protocol::User).new
|
@@user_list = Array(Crirc::Protocol::User).new
|
||||||
|
|
||||||
def bind(bot)
|
def bind(bot)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
module DashBot::Plugins::BasicCommands
|
require "../plugin"
|
||||||
extend self
|
|
||||||
|
|
||||||
|
class DashBot::Plugins::BasicCommands < DashBot::Plugin
|
||||||
WHAT = %w(ce que tu dis n'a aucun sens)
|
WHAT = %w(ce que tu dis n'a aucun sens)
|
||||||
|
|
||||||
def bind(bot)
|
def bind(bot)
|
||||||
|
|
|
@ -1,21 +1,19 @@
|
||||||
module DashBot
|
module DashBot::Rights
|
||||||
module Rights
|
def authorized?(msg, group = "admin")
|
||||||
def authorized?(msg, group = "admin")
|
n = DB.query_one("SELECT COUNT(*) FROM groups
|
||||||
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",
|
||||||
INNER JOIN users ON groups.user_name = users.name WHERE groups.name = $1 AND users.name = $2",
|
[group, msg.source.source_id], as: {Int64})
|
||||||
[group, msg.source.source_id], as: {Int64})
|
n == 1
|
||||||
n == 1
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def authorized?(msg, groups : Array(String))
|
def authorized?(msg, groups : Array(String))
|
||||||
groups.any? { |group| authorized? msg, group }
|
groups.any? { |group| authorized? msg, group }
|
||||||
end
|
end
|
||||||
|
|
||||||
def authorize!(bot, msg : Crirc::Protocol::Message, group = "admin", reply = "Unauthorized")
|
def authorize!(bot, msg : Crirc::Protocol::Message, group = "admin", reply = "Unauthorized")
|
||||||
return true if authorized? msg, group
|
return true if authorized? msg, group
|
||||||
bot.reply msg, reply
|
bot.reply msg, reply
|
||||||
false
|
false
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
extend self
|
extend self
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
module DashBot
|
class DashBot
|
||||||
VERSION = "0.1.0"
|
VERSION = "0.1.0"
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue