Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Meoowww/DashBot
Browse files Browse the repository at this point in the history
  • Loading branch information
Nephos committed Oct 2, 2017
2 parents 86ebaff + 23bc102 commit 116bc2a
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
NAME=`ls src/*/ -d | cut -f2 -d'/'`
NAME=`ls -d src/*/ | cut -f2 -d'/'`

all: deps_opt build

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Install ``postgresql``, ``crystal``, and ``crystal-shards``

```sh
# Clone
git clone https://github.com/Nephos/DashBot
git clone https://github.com/Meoowww/DashBot
cd DashBot
# Install the libs
crystal deps install
Expand Down Expand Up @@ -37,7 +37,7 @@ TODO: Write development instructions here

## Contributing

1. Fork it ( https://github.com/Nephos/DashBot/fork )
1. Fork it ( https://github.com/Meoowww/DashBot/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
Expand All @@ -46,3 +46,4 @@ TODO: Write development instructions here
## Contributors

- [pouleta](https://github.com/Nephos) Arthur Poulet - creator, maintainer
- [Lucie](https://github.com/Lucie-Dispot) Lucie Dispot - developer
13 changes: 13 additions & 0 deletions db/migrations/20170918122903_reminders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- +micrate Up
CREATE TABLE reminders (
id SERIAL PRIMARY KEY,
author TEXT NOT NULL,
remind_time TIMESTAMP NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP NOT NULL,
checked_at TIMESTAMP,
read_at TIMESTAMP
);

-- +micrate Down
DROP TABLE reminders;
1 change: 1 addition & 0 deletions src/DashBot.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module DashBot
Plugins::AdminCommands.bind(bot)
Plugins::Points.bind(bot)
Plugins::Messages.bind(bot)
Plugins::Reminder.bind(bot)
Plugins::Rpg.bind(bot)
Plugins::Random.bind(bot)

Expand Down
10 changes: 8 additions & 2 deletions src/DashBot/plugins/admin_commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ module DashBot::Plugins::AdminCommands
include Rights

def bind(bot)
bot.on("PRIVMSG", message: /^!kick ([[:graph:]]+)(?: (.+))?/) do |msg, match|
bot.on("PRIVMSG", message: /^!kick *([[:graph:]]+)(?: (.+))?/) do |msg, match|
next if !authorize! msg, %w(admin modo), "You cannot kick, you are not an \"admin\" nor a \"modo\""
chan = CrystalIrc::Chan.new msg.arguments.first
user = CrystalIrc::User.new match.as(Regex::MatchData)[1]
bot.kick([chan], [user], match.as(Regex::MatchData)[2]?)
end.on("PRIVMSG", message: /^!(join|part) ([[:graph:]]+)/) do |msg, match|
end.on("PRIVMSG", message: /^!(join|part) *([[:graph:]]+)/) do |msg, match|
match = match.as(Regex::MatchData)
command = match[1]
next if !authorize! msg, "admin", "You cannot #{command}, you are not an \"admin\""
chan = CrystalIrc::Chan.new match[2]
bot.join([chan]) if command == "join"
bot.part([chan]) if command == "part"
end.on("PRIVMSG", message: /^!privmsg *([[:graph:]]+) *(.+)/) do |msg, match|
match = match.as(Regex::MatchData)
command = match[1]
next if !authorize! msg, "admin", "You cannot #{command}, you are not an \"admin\""
target_type = match[1].starts_with?("#") ? CrystalIrc::Chan : CrystalIrc::User
bot.privmsg target_type.new(match[1]), match[2]
end
end
end
8 changes: 4 additions & 4 deletions src/DashBot/plugins/basic_commands.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module DashBot::Plugins::BasicCommands
extend self

WHAT = %w(ce que tu dis n a aucun sens)
WHAT = %w(ce que tu dis n'a aucun sens)

def bind(bot)
bot.on("JOIN") do |msg|
Expand All @@ -15,15 +15,15 @@ module DashBot::Plugins::BasicCommands
bot.pong(msg.message)
end.on("PRIVMSG", message: /^!ping/) do |msg|
msg.reply "pong #{msg.hl}"
end.on("PRIVMSG", message: /^!roll ([^:]+)( *: *(.+))?/) do |msg, match|
end.on("PRIVMSG", message: /^!roll *([^:]+)( *: *(.+))?/) do |msg, match|
match = match.as Regex::MatchData
r = Rollable::Roll.parse(match[1]).compact!.order!
result = r.test_details
msg.reply "#{msg.hl}: [#{match[3]?}] #{result.sum} (#{r.to_s} = #{result.join(", ")})"
end.on("PRIVMSG", message: /^!call (.+)/) do |msg, match|
end.on("PRIVMSG", message: /^!call *(.+)/) do |msg, match|
match = match.as Regex::MatchData
msg.reply "I'm calling #{match[1]} right now"
end.on("PRIVMSG", message: /($| )what(^| )/) do |msg, match|
end.on("PRIVMSG", message: /(^| )what($| )/i) do |msg, match|
msg.reply WHAT.shuffle.join(" ")
end
end
Expand Down
3 changes: 1 addition & 2 deletions src/DashBot/plugins/messages.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module DashBot::Plugins::Messages
end

def bind_write(bot)
bot.on("PRIVMSG", message: /^!write (\w+) (.+)$/) do |msg, match|
bot.on("PRIVMSG", message: /^!write *(\w+) *(.+)$/) do |msg, match|
id = match.as(Regex::MatchData)[1]
message = match.as(Regex::MatchData)[2]
DB.exec "INSERT INTO messages (author, dest, content, created_at)
Expand Down Expand Up @@ -50,5 +50,4 @@ module DashBot::Plugins::Messages
msg.reply "#{msg.source_id}, you have #{count} messages" if count > 0
end
end
#
end
6 changes: 3 additions & 3 deletions src/DashBot/plugins/points.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module DashBot::Plugins::Points
end

def bind_add(bot)
bot.on("PRIVMSG", message: /^!p ([[:graph:]]+) ([[:graph:]]+)/) do |msg, match|
bot.on("PRIVMSG", message: /^!p *([[:graph:]]+) *([[:graph:]]+)/) do |msg, match|
match = match.as Regex::MatchData
DB.exec "INSERT INTO points (assigned_to, assigned_by, type, created_at)
VALUES ($1, $2, $3, NOW())", [match[2], msg.source_id, match[1].downcase]
Expand All @@ -19,12 +19,12 @@ module DashBot::Plugins::Points
end

def bind_list(bot)
bot.on("PRIVMSG", message: /^!pl ([[:graph:]]+)/) do |msg, match|
bot.on("PRIVMSG", message: /^!pl *([[:graph:]]+)/) do |msg, match|
match = match.as Regex::MatchData
points = DB.query_all("SELECT assigned_to, COUNT(*) FROM points WHERE type = $1 GROUP BY assigned_to ORDER BY COUNT(*) DESC LIMIT 5;", [match[1]], as: {String, Int64})
msg.reply "#{match[1]}: " + points.map { |point| "#{point[0]}: #{point[1]}" }.join(", ")
end
bot.on("PRIVMSG", message: /^!plu ([[:graph:]]+)/) do |msg, match|
bot.on("PRIVMSG", message: /^!plu *([[:graph:]]+)/) do |msg, match|
match = match.as Regex::MatchData
points = DB.query_all("SELECT type, COUNT(*) FROM points WHERE assigned_to = $1 GROUP BY type ORDER BY COUNT(*) DESC LIMIT 5;", [match[1]], as: {String, Int64})
msg.reply "#{match[1]}: " + points.map { |point| "#{point[0]}: #{point[1]}" }.join(", ")
Expand Down
2 changes: 1 addition & 1 deletion src/DashBot/plugins/random.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module DashBot::Plugins::Random
extend self

def bind(bot)
bot.on("PRIVMSG", message: /^!random ([[:alnum:]]+(, ?[[:alnum:]]+))+/) do |msg, match|
bot.on("PRIVMSG", message: /^!random *([[:alnum:]]+(, ?[[:alnum:]]+))+/) do |msg, match|
match = match.as(Regex::MatchData)
list = match[1]
values = list.split(/(, )|([^,] )/)
Expand Down
73 changes: 73 additions & 0 deletions src/DashBot/plugins/reminder.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require "CrystalIrc"

module DashBot::Plugins::Reminder
extend self
include Rights
include Database

def bind(bot)
bind_write bot
bind_check bot
bind_read bot
bind_signal bot
end

def bind_write(bot)
bot.on("PRIVMSG", message: /^!reminder ([[:graph:]]+) (.+)$/) do |msg, match|
dest_time = Time.adaptive_parse(match.as(Regex::MatchData)[1])
message = match.as(Regex::MatchData)[2]
DB.exec "INSERT INTO reminders (author, remind_time, content, created_at)
VALUES ($1, $2, $3, NOW())", [msg.source_id, dest_time, message]
msg.reply "Reminder set"
end
end

def bind_read(bot)
bot.on("PRIVMSG", message: /^!remindme$/) do |msg, match|
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",
[msg.source_id], as: {Int32, String, Time, String, Time, Time?, Time?}) rescue nil
if messages
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]}
if message[:remind_time] < Time.utc_now
date = message[:remind_time].to_local
if Time.now.to_s("%j") == date.to_s("%j")
date = date.to_s("%H:%M:%S")
else
date = date.to_s("%B, %d at %H:%M:%S")
end
msg.reply "#{date} -- #{message[:content]}"
DB.exec "UPDATE reminders SET read_at = NOW() WHERE id = $1", [message[:id]]
end
end
end
end
end

def bind_check(bot)
bot.on("PING") do |msg|
messages = DB.query_all(
"SELECT id, author, remind_time, content, created_at, read_at FROM reminders WHERE checked_at IS NULL AND read_at IS NULL",
as: {Int32, String, Time, String, Time, Time?}) rescue nil
if messages
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]}
if message[:remind_time] < Time.utc_now
# TODO: fix this so it finds the user's nickname and sends it to it instead
bot.privmsg CrystalIrc::User.new(message[:author]), "You have a new reminder"
DB.exec "UPDATE reminders SET checked_at = NOW() WHERE id = $1", [message[:id]]
break
end
end
end
end
end

def bind_signal(bot)
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_id], as: {Int64})
msg.reply "#{msg.source_id}, you have #{count} reminders" if count > 0
end
end
end
7 changes: 3 additions & 4 deletions src/DashBot/plugins/user_commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module DashBot::Plugins::UserCommands
end

def bind_group_rm(bot)
bot.on("PRIVMSG", message: /^!group rm (\w+) (\w+)/) do |msg, match|
bot.on("PRIVMSG", message: /^!group *rm *(\w+) *(\w+)/) do |msg, match|
next if !authorize!(msg)
match = match.as Regex::MatchData
if user_exists? match[1]
Expand All @@ -34,7 +34,7 @@ module DashBot::Plugins::UserCommands
end

def bind_group_ls(bot)
bot.on("PRIVMSG", message: /^!group (?:list|ls) (\w+)/) do |msg, match|
bot.on("PRIVMSG", message: /^!group *(?:list|ls) (\w+)/) do |msg, match|
match = match.as Regex::MatchData
if user_exists? match[1]
groups = DB.query_all("SELECT groups.name AS name FROM groups
Expand All @@ -48,7 +48,7 @@ module DashBot::Plugins::UserCommands
end

def bind_group_add(bot)
bot.on("PRIVMSG", message: /^!group add (\w+) (\w+)/) do |msg, match|
bot.on("PRIVMSG", message: /^!group *add *(\w+) *(\w+)/) do |msg, match|
next if !authorize!(msg)
match = match.as Regex::MatchData
if user_exists? match[1]
Expand All @@ -72,5 +72,4 @@ module DashBot::Plugins::UserCommands
end
end
end
#
end
26 changes: 26 additions & 0 deletions src/DashBot/time.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
struct Time
def self.adaptive_parse(txt)
t = Time.now
parsing_formats = {
"%R %-d/%m/%Y" => " #{t.day}/#{t.month}/#{t.year}",
"%T %-d/%m/%Y" => " #{t.day}/#{t.month}/#{t.year}",
"%Hh%M %-d/%m/%Y" => " #{t.day}/#{t.month}/#{t.year}",
"%kh%M %-d/%m/%Y" => " #{t.day}/#{t.month}/#{t.year}",
"%k:%M %-d/%m/%Y" => " #{t.day}/#{t.month}/#{t.year}",
"%Hh %-d/%m/%Y" => " #{t.day}/#{t.month}/#{t.year}",
"%kh %-d/%m/%Y" => " #{t.day}/#{t.month}/#{t.year}",
"%d/%m %Y" => " #{t.year}",
"%-d/%m %Y" => " #{t.year}",
"%d/%m/%Y" => "",
"%-d/%m/%Y" => "",
"%D" => "",
}

parsing_formats.each do |f, v|
begin
return Format.new(f, Time::Kind::Local).parse(txt + v).to_utc
rescue
end
end
end
end

0 comments on commit 116bc2a

Please sign in to comment.