Compare commits

...

3 Commits

Author SHA1 Message Date
Arthur POULET a5737d9afb
title_reader: Add plugin to read titles 2021-03-27 12:11:23 +01:00
Arthur POULET 384121ec1c
db: Update all DB calls 2021-03-27 12:11:05 +01:00
Arthur POULET 9770315954
doc: Update forge urls 2021-03-27 11:39:12 +01:00
12 changed files with 53 additions and 39 deletions

View File

@ -11,9 +11,9 @@ release:
test: test:
crystal spec crystal spec
deps: deps:
shards install shards install --ignore-crystal-version
deps_update: deps_update:
shards update shards update --ignore-crystal-version
deps_opt: deps_opt:
@[ -d lib/ ] || make deps @[ -d lib/ ] || make deps
doc: doc:

View File

@ -1,6 +1,6 @@
# DashBot # DashBot
TODO: Write a description here A good bot, or a collection of plugins for Crirc network client.
## Installation ## Installation
@ -40,7 +40,7 @@ TODO: Write development instructions here
## Contributing ## Contributing
1. Fork it ( https://github.com/Meoowww/DashBot/fork ) 1. Fork it ( https://git.sceptique.eu/Sceptique/DashBot/fork )
2. Create your feature branch (git checkout -b my-new-feature) 2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature') 3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature) 4. Push to the branch (git push origin my-new-feature)
@ -48,5 +48,5 @@ TODO: Write development instructions here
## Contributors ## Contributors
- [pouleta](https://github.com/Nephos) Arthur Poulet - creator, maintainer - [Sceptique](https://git.sceptique.eu/Sceptique) Arthur Poulet - creator, maintainer
- [Lucie](https://github.com/Lucie-Dispot) Lucie Dispot - developer - [Lucie](https://github.com/Lucie-Dispot) Lucie Dispot - developer

View File

@ -1,12 +1,8 @@
version: 2.0 version: 2.0
shards: shards:
crirc: crirc:
git: https://github.com/meoowww/crirc.git git: https://git.sceptique.eu/Sceptique/Crirc
version: 0.5.1+git.commit.c21010a9d9d3ef9067b5610ec4cb4ea389fdc508 version: 0.5.1+git.commit.1aab6b69c7e9cca6780bc335d7b79739e201f1dd
daemonize:
git: https://github.com/dougeverly/daemonize.cr.git
version: 0.1.0+git.commit.03d7da27fe76ee6b77d47a66984969a308a42027
db: db:
git: https://github.com/crystal-lang/crystal-db.git git: https://github.com/crystal-lang/crystal-db.git
@ -14,7 +10,7 @@ shards:
dotenv: dotenv:
git: https://github.com/gdotdesign/cr-dotenv.git git: https://github.com/gdotdesign/cr-dotenv.git
version: 0.7.0 version: 1.0.0
micrate: micrate:
git: https://github.com/juanedi/micrate.git git: https://github.com/juanedi/micrate.git
@ -29,8 +25,8 @@ shards:
version: 0.23.1 version: 0.23.1
rollable: rollable:
git: https://github.com/nephos/crystal_rollable.git git: https://git.sceptique.eu/Sceptique/rollable
version: 1.0.0+git.commit.050af10ac2076293e79c69b511d1f3e0e0835f86 version: 1.0.0+git.commit.84ef972fec468d03381a815675b7a906b792458b
sqlite3: sqlite3:
git: https://github.com/crystal-lang/crystal-sqlite3.git git: https://github.com/crystal-lang/crystal-sqlite3.git

View File

@ -11,11 +11,11 @@ crystal: 1.0.0
dependencies: dependencies:
# irc # irc
crirc: crirc:
github: Meoowww/Crirc git: https://git.sceptique.eu/Sceptique/Crirc
branch: master branch: master
# dice roll # dice roll
rollable: rollable:
github: Nephos/crystal_rollable git: https://git.sceptique.eu/Sceptique/rollable
branch: master branch: master
# Sql # Sql
pg: pg:

View File

@ -44,7 +44,8 @@ module DashBot
Plugins::Reminder.bind(bot) Plugins::Reminder.bind(bot)
Plugins::Rpg.bind(bot) Plugins::Rpg.bind(bot)
Plugins::Random.bind(bot) Plugins::Random.bind(bot)
# Plugins::Anarchism.bind(bot) Plugins::Anarchism.bind(bot)
Plugins::TitleReader.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) }

View File

@ -16,7 +16,7 @@ module DashBot::Plugins::Messages
id = match.as(Regex::MatchData)[1] id = match.as(Regex::MatchData)[1]
message = match.as(Regex::MatchData)[2] message = match.as(Regex::MatchData)[2]
DB.exec "INSERT INTO messages (author, dest, content, created_at) DB.exec "INSERT INTO messages (author, dest, content, created_at)
VALUES ($1, $2, $3, NOW())", [msg.source.source_id, id, message] VALUES ($1, $2, $3, NOW())", msg.source.source_id, id, message
# bot.privmsg User.new(msg.source.source_id), "Message sent to \"#{id}\"" # bot.privmsg User.new(msg.source.source_id), "Message sent to \"#{id}\""
bot.reply msg, "Message sent to \"#{id}\"" bot.reply msg, "Message sent to \"#{id}\""
end end
@ -26,7 +26,7 @@ module DashBot::Plugins::Messages
bot.on("PRIVMSG", message: /^!read$/, doc: {"!read", "!read read the inbox"}) do |msg, match| bot.on("PRIVMSG", message: /^!read$/, doc: {"!read", "!read read the inbox"}) do |msg, match|
m = DB.query_one( m = DB.query_one(
"SELECT id, author, dest, content, created_at, read_at FROM messages WHERE read_at IS NULL AND dest = $1 "SELECT id, author, dest, content, created_at, read_at FROM messages WHERE read_at IS NULL AND dest = $1
ORDER BY created_at ASC LIMIT 1", [msg.source.source_id], as: {Int32, String, String, String, Time, Time?}) rescue nil ORDER BY created_at ASC LIMIT 1", msg.source.source_id, as: {Int32, String, String, String, Time, Time?}) rescue nil
if m if m
message = {id: m[0], author: m[1], dest: m[2], content: m[3], created_at: m[4], read_at: m[5]} message = {id: m[0], author: m[1], dest: m[2], content: m[3], created_at: m[4], read_at: m[5]}
# user = User.new(msg.source.source_id) # user = User.new(msg.source.source_id)
@ -46,7 +46,7 @@ module DashBot::Plugins::Messages
def bind_signal(bot) def bind_signal(bot)
bot.on("JOIN") do |msg, _| bot.on("JOIN") do |msg, _|
count = DB.query_one("SELECT COUNT(*) FROM messages WHERE read_at IS NULL and DEST = $1", [msg.source.source_id], as: {Int64}) count = DB.query_one("SELECT COUNT(*) FROM messages WHERE read_at IS NULL and DEST = $1", msg.source.source_id, as: {Int64})
bot.reply msg, "#{msg.source.source_id}, you have #{count} messages" if count > 0 bot.reply msg, "#{msg.source.source_id}, you have #{count} messages" if count > 0
end end
end end

View File

@ -24,7 +24,7 @@ module DashBot::Plugins::Reminder
bot.on("PRIVMSG", message: /^!remindme$/, doc: {"!remindme", "!remindme"}) do |msg, match| bot.on("PRIVMSG", message: /^!remindme$/, doc: {"!remindme", "!remindme"}) do |msg, match|
messages = DB.query_all( messages = DB.query_all(
"SELECT id, author, remind_time, content, created_at, checked_at, read_at FROM reminders WHERE read_at IS NULL AND author = $1", "SELECT id, author, remind_time, content, created_at, checked_at, read_at FROM reminders WHERE read_at IS NULL AND author = $1",
[msg.source.source_id], as: {Int32, String, Time, String, Time, Time?, Time?}) rescue nil msg.source.source_id, as: {Int32, String, Time, String, Time, Time?, Time?}) rescue nil
if messages if messages
messages.each do |m| messages.each do |m|
message = {id: m[0], author: m[1], remind_time: m[2], content: m[3], created_at: m[4], read_at: m[5]} message = {id: m[0], author: m[1], remind_time: m[2], content: m[3], created_at: m[4], read_at: m[5]}
@ -64,7 +64,7 @@ module DashBot::Plugins::Reminder
def bind_signal(bot) def bind_signal(bot)
bot.on("JOIN") do |msg, _| bot.on("JOIN") do |msg, _|
count = DB.query_one("SELECT COUNT(*) FROM reminders WHERE read_at IS NULL and checked_at IS NOT NULL and author = $1", [msg.source.source_id], as: {Int64}) count = DB.query_one("SELECT COUNT(*) FROM reminders WHERE read_at IS NULL and checked_at IS NOT NULL and author = $1", msg.source.source_id, as: {Int64})
bot.reply msg, "#{msg.source.source_id}, you have #{count} reminders" if count > 0 bot.reply msg, "#{msg.source.source_id}, you have #{count} reminders" if count > 0
end end
end end

View File

@ -13,7 +13,7 @@ module DashBot::Plugins::Rpg::Music
bot.on("PRIVMSG", message: /^!music +add +([[:graph:]]+) +([[:graph:]]+)/) do |msg, match| bot.on("PRIVMSG", message: /^!music +add +([[:graph:]]+) +([[:graph:]]+)/) do |msg, match|
msg_match = match.as Regex::MatchData msg_match = match.as Regex::MatchData
DB.exec("INSERT INTO musics (owner, category, url, created_at) DB.exec("INSERT INTO musics (owner, category, url, created_at)
VALUES ($1, $2, $3, NOW())", [msg.source.source_id, msg_match[1], msg_match[2]]) VALUES ($1, $2, $3, NOW())", msg.source.source_id, msg_match[1], msg_match[2])
bot.reply msg, "Music successfully added." bot.reply msg, "Music successfully added."
end end
end end
@ -21,12 +21,12 @@ module DashBot::Plugins::Rpg::Music
def bind_del_music(bot) def bind_del_music(bot)
bot.on("PRIVMSG", message: /^!music +delete +([[:graph:]]+) +([[:graph:]]+)/) do |msg, match| bot.on("PRIVMSG", message: /^!music +delete +([[:graph:]]+) +([[:graph:]]+)/) do |msg, match|
msg_match = match.as Regex::MatchData msg_match = match.as Regex::MatchData
musics = DB.query_all("SELECT url FROM musics WHERE category = $1", [msg_match[1]], as: {String}) musics = DB.query_all("SELECT url FROM musics WHERE category = $1", msg_match[1], as: {String})
if msg_match[2].to_i > musics.size if msg_match[2].to_i > musics.size
bot.reply msg, "This music doesn't exist." bot.reply msg, "This music doesn't exist."
else else
DB.exec("DELETE FROM musics WHERE url = $1", [musics[msg_match[2].to_i - 1]["url"]]) DB.exec("DELETE FROM musics WHERE url = $1", musics[msg_match[2].to_i - 1]["url"])
bot.reply msg, "Music successfully deleted." bot.reply msg, "Music successfully deleted."
end end
end end
@ -41,7 +41,7 @@ module DashBot::Plugins::Rpg::Music
bot.on("PRIVMSG", message: /^!music +(?:list|ls) +([[:graph:]]+)/) do |msg, match| bot.on("PRIVMSG", message: /^!music +(?:list|ls) +([[:graph:]]+)/) do |msg, match|
msg_match = match.as Regex::MatchData msg_match = match.as Regex::MatchData
urls = DB.query_all("SELECT url FROM musics WHERE category = $1 LIMIT 5", [msg_match[1]], as: {String}).join(", ") urls = DB.query_all("SELECT url FROM musics WHERE category = $1 LIMIT 5", msg_match[1], as: {String}).join(", ")
bot.reply msg, "The following musics are present in the category #{msg_match[1]}: #{urls}" bot.reply msg, "The following musics are present in the category #{msg_match[1]}: #{urls}"
end end
end end
@ -50,14 +50,14 @@ module DashBot::Plugins::Rpg::Music
# Select a random music # Select a random music
bot.on("PRIVMSG", message: /^!music play +([[:graph:]]+) +$/) do |msg, match| bot.on("PRIVMSG", message: /^!music play +([[:graph:]]+) +$/) do |msg, match|
msg_match = match.as Regex::MatchData msg_match = match.as Regex::MatchData
musics = DB.query_all("SELECT url FROM musics WHERE category = $1", [msg_match[1]], as: {String}) musics = DB.query_all("SELECT url FROM musics WHERE category = $1", msg_match[1], as: {String})
bot.reply msg, "#{musics.sample}" bot.reply msg, "#{musics.sample}"
end end
# Select a specific music # Select a specific music
bot.on("PRIVMSG", message: /^!music play +([[:graph:]]+) +([[:graph:]]+)/) do |msg, match| bot.on("PRIVMSG", message: /^!music play +([[:graph:]]+) +([[:graph:]]+)/) do |msg, match|
msg_match = match.as Regex::MatchData msg_match = match.as Regex::MatchData
musics = DB.query_all("SELECT url FROM musics WHERE category = $1", [msg_match[1]], as: {String}) musics = DB.query_all("SELECT url FROM musics WHERE category = $1", msg_match[1], as: {String})
if msg_match[2].to_i > musics.size if msg_match[2].to_i > musics.size
bot.reply msg, "Error: this track doesn't exist (yet)." bot.reply msg, "Error: this track doesn't exist (yet)."
else else

View File

@ -25,7 +25,7 @@ module DashBot::Plugins::Rpg::Roll
# check if name already exists # check if name already exists
n = DB.query_one("SELECT COUNT(*) FROM dies WHERE name = $1 AND owner = $2", n = DB.query_one("SELECT COUNT(*) FROM dies WHERE name = $1 AND owner = $2",
[msg_match[1], msg.source.source_id], as: {Int64}) msg_match[1], msg.source.source_id, as: {Int64})
if n > 0 if n > 0
bot.reply msg, "Error: #{msg_match[1]} already exists. Remove it or use another name." bot.reply msg, "Error: #{msg_match[1]} already exists. Remove it or use another name."
@ -44,7 +44,7 @@ module DashBot::Plugins::Rpg::Roll
msg_match = match.as Regex::MatchData msg_match = match.as Regex::MatchData
begin begin
roll = DB.query_one("SELECT roll FROM dies WHERE name = $1 AND owner = $2", roll = DB.query_one("SELECT roll FROM dies WHERE name = $1 AND owner = $2",
[msg_match[1], msg.source.source_id], as: {String}) msg_match[1], msg.source.source_id, as: {String})
rescue rescue
bot.reply msg, "Roll #{msg_match[1]} does not exist." bot.reply msg, "Roll #{msg_match[1]} does not exist."
next next
@ -62,7 +62,7 @@ module DashBot::Plugins::Rpg::Roll
match = match.as Regex::MatchData match = match.as Regex::MatchData
limit = (match[2]? || "8").to_i limit = (match[2]? || "8").to_i
all_rolls = DB.query_all("SELECT owner, roll FROM dies WHERE name = $1", all_rolls = DB.query_all("SELECT owner, roll FROM dies WHERE name = $1",
[match[1]], as: {String, String}) match[1], as: {String, String})
rolls = all_rolls.shift(limit) rolls = all_rolls.shift(limit)
bot.reply msg, "#{all_rolls.size} rolls will not be launch." if !all_rolls.empty? bot.reply msg, "#{all_rolls.size} rolls will not be launch." if !all_rolls.empty?

View File

@ -0,0 +1,17 @@
require "http/client"
require "xml"
module DashBot::Plugins::TitleReader
extend self
def bind(bot)
bot.on("PRIVMSG", message: /(http[^ ]+)/, doc: {"any url", "look a this http://url/"}) do |msg, match|
match = match.as Regex::MatchData
url = match[1]
response = HTTP::Client.get url
xml = XML.parse response.body
title_node = xml.xpath_node("//title")
bot.reply msg, "(#{title_node.text})" if title_node
end
end
end

View File

@ -20,9 +20,9 @@ module DashBot::Plugins::UserCommands
match = match.as Regex::MatchData match = match.as Regex::MatchData
if user_exists? match[1] if user_exists? match[1]
groups = DB.query_all("SELECT groups.id AS id, groups.name AS name FROM groups groups = DB.query_all("SELECT groups.id AS id, groups.name AS name FROM groups
INNER JOIN users ON groups.user_name = users.name WHERE users.name = $1", [match[1]], as: {Int32, String}) INNER JOIN users ON groups.user_name = users.name WHERE users.name = $1", match[1], as: {Int32, String})
if idx = groups.index { |e| e[1] == match[2] } if idx = groups.index { |e| e[1] == match[2] }
DB.exec "DELETE FROM groups WHERE id = $1", [groups[idx][0]] DB.exec "DELETE FROM groups WHERE id = $1", groups[idx][0]
bot.reply msg, "The user \"#{match[1]}\" has lost the group \"#{match[2]}\"" bot.reply msg, "The user \"#{match[1]}\" has lost the group \"#{match[2]}\""
else else
bot.reply msg, "The user \"#{match[1]}\" does not belongs to the group \"#{match[2]}\"" bot.reply msg, "The user \"#{match[1]}\" does not belongs to the group \"#{match[2]}\""
@ -38,7 +38,7 @@ module DashBot::Plugins::UserCommands
match = match.as Regex::MatchData match = match.as Regex::MatchData
if user_exists? match[1] if user_exists? match[1]
groups = DB.query_all("SELECT groups.name AS name FROM groups groups = DB.query_all("SELECT groups.name AS name FROM groups
INNER JOIN users ON groups.user_name = users.name WHERE users.name = $1", [match[1]], as: {String}) INNER JOIN users ON groups.user_name = users.name WHERE users.name = $1", match[1], as: {String})
groups = groups.join(", ") groups = groups.join(", ")
bot.reply msg, "User \"#{match[1]}\" has the groups : #{groups}" bot.reply msg, "User \"#{match[1]}\" has the groups : #{groups}"
else else
@ -52,7 +52,7 @@ module DashBot::Plugins::UserCommands
next if !authorize! bot, msg next if !authorize! bot, msg
match = match.as Regex::MatchData match = match.as Regex::MatchData
if user_exists? match[1] if user_exists? match[1]
DB.exec "INSERT INTO groups (user_name, name) VALUES ($1, $2)", [match[1], match[2]] DB.exec "INSERT INTO groups (user_name, name) VALUES ($1, $2)", match[1], match[2]
bot.reply msg, "The user \"#{match[1]}\" has gained the group \"#{match[2]}\"" bot.reply msg, "The user \"#{match[1]}\" has gained the group \"#{match[2]}\""
else else
bot.reply msg, "User \"#{match[1]}\" not registered" bot.reply msg, "User \"#{match[1]}\" not registered"
@ -66,9 +66,9 @@ module DashBot::Plugins::UserCommands
bot.reply msg, "Cannot register \"#{msg.source.source_id}\" twice" bot.reply msg, "Cannot register \"#{msg.source.source_id}\" twice"
else else
bot.reply msg, "Register \"#{msg.source.source_id}\"" bot.reply msg, "Register \"#{msg.source.source_id}\""
is_admin = DB.query_one("SELECT COUNT(*) FROM users", [] of Int64, as: {Int64}) == 0 is_admin = DB.query_one("SELECT COUNT(*) FROM users", as: {Int64}) == 0
DB.exec "INSERT INTO users (name) VALUES ($1)", [msg.source.source_id] DB.exec "INSERT INTO users (name) VALUES ($1)", msg.source.source_id
DB.exec "INSERT INTO groups (user_name, name) VALUES ($1, $2)", [msg.source.source_id, is_admin ? "admin" : "default"] DB.exec "INSERT INTO groups (user_name, name) VALUES ($1, $2)", msg.source.source_id, is_admin ? "admin" : "default"
end end
end end
end end

View File

@ -3,7 +3,7 @@ module DashBot
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