Skip to content

Commit

Permalink
Update DashBot to use Crirc instead of CrystalIrc
Browse files Browse the repository at this point in the history
  • Loading branch information
Nephos committed Nov 16, 2017
1 parent 275281b commit 6918755
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 110 deletions.
8 changes: 4 additions & 4 deletions shard.lock
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
version: 1.0
shards:
CrystalIrc:
github: Meoowww/CrystalIrc
commit: b6742edf45733ad4fe0ba9fa774799094c1ac8e3
crirc:
github: Meoowww/Crirc
commit: 4fd50613f425628ba2bfdaf25f3868fb731cb54e

daemonize:
github: DougEverly/daemonize.cr
commit: 03d7da27fe76ee6b77d47a66984969a308a42027

db:
github: crystal-lang/crystal-db
version: 0.4.2
version: 0.4.3

dotenv:
github: gdotdesign/cr-dotenv
Expand Down
6 changes: 3 additions & 3 deletions shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ license: MIT

dependencies:
# irc
CrystalIrc:
github: Meoowww/CrystalIrc
branch: develop
crirc:
github: Meoowww/Crirc
branch: master
# dice roll
rollable:
github: Nephos/crystal_rollable
Expand Down
61 changes: 41 additions & 20 deletions src/DashBot.cr
Original file line number Diff line number Diff line change
@@ -1,37 +1,58 @@
#!/usr/bin/env crystal

require "CrystalIrc"
require "crirc"
require "rollable"
require "./DashBot/*"
require "./DashBot/plugins/*"

# Extention of `String`.
module DashBot::Source
def source_nick : String
self.split("!")[0].to_s
end

def source_id : String
self.split("!")[1].to_s.split("@")[0].to_s
end

def source_whois : String
self.split("!")[1].to_s.split("@")[1].to_s
end
end

class String
include DashBot::Source
end

module DashBot
def start
Arguments.new.use
bot = CrystalIrc::Bot.new ip: "irc.mozilla.org", port: 6667_u16, ssl: false, nick: "Dasshyx", read_timeout: 300_u16

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)

bot.connect.on_ready do
bot.join (ARGV.empty? ? ["#equilibre"] : ARGV).map { |chan| CrystalIrc::Chan.new(chan) }
end
client = Crirc::Network::Client.new(ip: "irc.mozilla.org", port: 6667_u16, ssl: false, nick: "Dasshyxtest", 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)

bot.on_ready do
bot.join (ARGV.empty? ? ["#equilibre"] : ARGV).map { |chan| Crirc::Protocol::Chan.new(chan) }
end

loop do
begin
bot.gets do |m|
loop do
begin
m = bot.gets
break if m.nil?
STDERR.puts "[#{Time.now}] #{m}"
spawn { bot.handle(m.as(String)) }
rescue IO::Timeout
puts "Nothing happened..."
end
rescue IO::Timeout
puts "Nothing happened..."
end
end
end
Expand Down
14 changes: 7 additions & 7 deletions src/DashBot/plugins/admin_commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ module DashBot::Plugins::AdminCommands

def bind(bot)
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]
next if !authorize! bot, msg, %w(admin modo), "You cannot kick, you are not an \"admin\" nor a \"modo\""
chan = Crirc::Protocol::Chan.new msg.argument_list.first
user = Crirc::Protocol::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|
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]
next if !authorize! bot, msg, "admin", "You cannot #{command}, you are not an \"admin\""
chan = Crirc::Protocol::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
next if !authorize! bot, msg, "admin", "You cannot #{command}, you are not an \"admin\""
target_type = match[1].starts_with?("#") ? Crirc::Protocol::Chan : Crirc::Protocol::User
bot.privmsg target_type.new(match[1]), match[2]
end
end
Expand Down
14 changes: 7 additions & 7 deletions src/DashBot/plugins/basic_commands.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ module DashBot::Plugins::BasicCommands

def bind(bot)
bot.on("JOIN") do |msg|
if msg.hl == bot.nick.to_s
msg.reply "Welcome everypony, what's up ? :)"
if msg.source == bot.nick.to_s
bot.reply msg, "Welcome everypony, what's up ? :)"
else
STDERR.puts "[#{Time.now}] #{msg.hl} joined the chan"
STDERR.puts "[#{Time.now}] #{msg.source} joined the chan"
end
end.on("PING") do |msg|
STDERR.puts "[#{Time.now}] PONG :#{msg.message}"
bot.pong(msg.message)
end.on("PRIVMSG", message: /^!ping/) do |msg|
msg.reply "pong #{msg.hl}"
bot.reply msg, "pong #{msg.source}"
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(", ")})"
bot.reply msg, "#{msg.source}: [#{match[3]?}] #{result.sum} (#{r.to_s} = #{result.join(", ")})"
end.on("PRIVMSG", message: /^!call +(.+)/) do |msg, match|
match = match.as Regex::MatchData
msg.reply "I'm calling #{match[1]} right now"
bot.reply msg, "I'm calling #{match[1]} right now"
end.on("PRIVMSG", message: /(^| )what($| )/i) do |msg, match|
msg.reply WHAT.shuffle.join(" ")
bot.reply msg, WHAT.shuffle.join(" ")
end
end
end
18 changes: 9 additions & 9 deletions src/DashBot/plugins/messages.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,38 @@ module DashBot::Plugins::Messages
id = match.as(Regex::MatchData)[1]
message = match.as(Regex::MatchData)[2]
DB.exec "INSERT INTO messages (author, dest, content, created_at)
VALUES ($1, $2, $3, NOW())", [msg.source_id, id, message]
# bot.privmsg User.new(msg.source_id), "Message sent to \"#{id}\""
msg.reply "Message sent to \"#{id}\""
VALUES ($1, $2, $3, NOW())", [msg.source.source_id, id, message]
# bot.privmsg User.new(msg.source.source_id), "Message sent to \"#{id}\""
bot.reply msg, "Message sent to \"#{id}\""
end
end

def bind_read(bot)
bot.on("PRIVMSG", message: /^!read$/) do |msg, match|
m = DB.query_one(
"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_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
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_id)
# user = User.new(msg.source.source_id)
date = message[:created_at]
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[:author]} -- #{message[:content]}"
bot.reply msg, "#{date} -- #{message[:author]} -- #{message[:content]}"
DB.exec "UPDATE messages SET read_at = NOW() WHERE id = $1", [message[:id]]
else
msg.reply "No message in the mailbox"
bot.reply msg, "No message in the mailbox"
end
end
end

def bind_signal(bot)
bot.on("JOIN") do |msg, _|
count = DB.query_one("SELECT COUNT(*) FROM messages WHERE read_at IS NULL and DEST = $1", [msg.source_id], as: {Int64})
msg.reply "#{msg.source_id}, you have #{count} messages" if count > 0
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
end
end
end
8 changes: 4 additions & 4 deletions src/DashBot/plugins/points.cr
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ module DashBot::Plugins::Points
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]
VALUES ($1, $2, $3, NOW())", [match[2], msg.source.source_id, match[1].downcase]
n = DB.query_one("SELECT COUNT(*) FROM points WHERE assigned_to = $1 AND type = $2",
[match[2], match[1].downcase], as: {Int64})
msg.reply "#{match[2]} has now #{n} point#{n > 1 ? "s" : ""} #{match[1]}"
bot.reply msg, "#{match[2]} has now #{n} point#{n > 1 ? "s" : ""} #{match[1]}"
end
end

def bind_list(bot)
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(", ")
bot.reply msg, "#{match[1]}: " + points.map { |point| "#{point[0]}: #{point[1]}" }.join(", ")
end
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(", ")
bot.reply msg, "#{match[1]}: " + points.map { |point| "#{point[0]}: #{point[1]}" }.join(", ")
end
end
end
4 changes: 2 additions & 2 deletions src/DashBot/plugins/random.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ module DashBot::Plugins::Random
match = match.as(Regex::MatchData)
list = match[1]
values = list.split(/(, )|([^,] )/)
msg.reply "Values: #{values}"
msg.reply values.sample
bot.reply msg, "Values: #{values}"
bot.reply msg, values.sample
end
end
end
16 changes: 7 additions & 9 deletions src/DashBot/plugins/reminder.cr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require "CrystalIrc"

module DashBot::Plugins::Reminder
extend self
include Rights
Expand All @@ -17,16 +15,16 @@ module DashBot::Plugins::Reminder
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"
VALUES ($1, $2, $3, NOW())", [msg.source.source_id, dest_time, message]
bot.reply msg, "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
[msg.source.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]}
Expand All @@ -37,7 +35,7 @@ module DashBot::Plugins::Reminder
else
date = date.to_s("%B, %d at %H:%M:%S")
end
msg.reply "#{date} -- #{message[:content]}"
bot.reply msg, "#{date} -- #{message[:content]}"
DB.exec "UPDATE reminders SET read_at = NOW() WHERE id = $1", [message[:id]]
end
end
Expand All @@ -55,7 +53,7 @@ module DashBot::Plugins::Reminder
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"
bot.privmsg Crirc::Protocol::User.new(message[:author]), "You have a new reminder"
DB.exec "UPDATE reminders SET checked_at = NOW() WHERE id = $1", [message[:id]]
break
end
Expand All @@ -66,8 +64,8 @@ module DashBot::Plugins::Reminder

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
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
end
end
end
18 changes: 9 additions & 9 deletions src/DashBot/plugins/rpg/music.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ module DashBot::Plugins::Rpg::Music
bot.on("PRIVMSG", message: /^!music +add +([[:graph:]]+) +([[:graph:]]+)/) do |msg, match|
msg_match = match.as Regex::MatchData
DB.exec("INSERT INTO musics (owner, category, url, created_at)
VALUES ($1, $2, $3, NOW())", [msg.source_id, msg_match[1], msg_match[2]])
msg.reply "Music successfully added."
VALUES ($1, $2, $3, NOW())", [msg.source.source_id, msg_match[1], msg_match[2]])
bot.reply msg, "Music successfully added."
end
end

Expand All @@ -24,10 +24,10 @@ module DashBot::Plugins::Rpg::Music
musics = DB.query_all("SELECT url FROM musics WHERE category = $1", [msg_match[1]], as: {String})

if msg_match[2].to_i > musics.size
msg.reply "This music doesn't exist."
bot.reply msg, "This music doesn't exist."
else
DB.exec("DELETE FROM musics WHERE url = $1", [musics[msg_match[2].to_i - 1]["url"]])
msg.reply "Music successfully deleted."
bot.reply msg, "Music successfully deleted."
end
end
end
Expand All @@ -36,13 +36,13 @@ module DashBot::Plugins::Rpg::Music
bot.on("PRIVMSG", message: /^!music +(?:list|ls) +$/) do |msg|
# Do not trigger if the user was asking for a specific category
categories = DB.query_all("SELECT DISTINCT category FROM musics", as: {String}).join(", ")
msg.reply "The following music categories exist: #{categories}"
bot.reply msg, "The following music categories exist: #{categories}"
end

bot.on("PRIVMSG", message: /^!music +(?:list|ls) +([[:graph:]]+)/) do |msg, match|
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(", ")
msg.reply "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

Expand All @@ -51,17 +51,17 @@ module DashBot::Plugins::Rpg::Music
bot.on("PRIVMSG", message: /^!music play +([[:graph:]]+) +$/) do |msg, match|
msg_match = match.as Regex::MatchData
musics = DB.query_all("SELECT url FROM musics WHERE category = $1", [msg_match[1]], as: {String})
msg.reply "#{musics.sample}"
bot.reply msg, "#{musics.sample}"
end

# Select a specific music
bot.on("PRIVMSG", message: /^!music play +([[:graph:]]+) +([[:graph:]]+)/) do |msg, match|
msg_match = match.as Regex::MatchData
musics = DB.query_all("SELECT url FROM musics WHERE category = $1", [msg_match[1]], as: {String})
if msg_match[2].to_i > musics.size
msg.reply "Error: this track doesn't exist (yet)."
bot.reply msg, "Error: this track doesn't exist (yet)."
else
msg.reply "#{musics[msg_match[2].to_i - 1]}"
bot.reply msg, "#{musics[msg_match[2].to_i - 1]}"
end
end
end
Expand Down

0 comments on commit 6918755

Please sign in to comment.