"
File.open(filename, "a") { |f| f.write(html) }
end
end
end
end
SotdUpdate = Class.new do
include Cinch::Plugin
extend SotdParse
match /supdate ([^ ]+)(?: (.+))?/
match /sotd (https?:\/\/[^ ]+)(?: (.+))?/
match /sotdupdate ([^ ]+)(?: (.+))?/
def execute(m, link, desc)
@link = URI.parse(link)
unless desc
@display = SotdParse.parse(@link)
else
@display = desc
end
if @display == 2 then
m.reply "This doesn't look like a supported link. Let ~caff know if this should be added, or rerun SOTD adding a title to this link. !supdate "
elsif @display == 1 then
m.reply "This doesn't appear to be a valid link. Sorry!"
elsif !@display.is_a? String then
m.reply "Something seems to have gone wrong. Let ~caff know."
else
rows = $db.execute "insert into sotd ( username, nick, link, display ) VALUES ( ?, ?, ?, ? )", [m.user.user, m.user.nick, @link.to_s, @display]
m.reply "Done! Your song of the day has been updated to #{@display}"
# bitly = Bitly.new($config['bitly']['login'], $config['bitly']['api_key'])
# shortlink = bitly.shorten(@link.to_s, :history => 1)
client = Mastodon::REST::Client.new(base_url: 'https://tiny.tilde.website', bearer_token: $config['mastodon']['access_token'])
client.create_status("#{m.user.user} has updated their #SOTD: #{@display} < #{@link.to_s} >")
end
end
end
class SotdStats
include Cinch::Plugin
extend Invisibleify
# match "sotdstats"
match /sotdstats/
def execute(m)
rows = $db.execute <<-SQL
SELECT username, COUNT(username) FROM sotd
GROUP BY username
ORDER BY COUNT(username) DESC
LIMIT 10;
SQL
totalcount = $db.execute <<-SQL
SELECT COUNT(id) FROM sotd;
SQL
rep = rows.map { |r| "#{Invisibleify.invisibleify(r[0])}: #{r[1]}" }
m.reply "Top SOTD users: #{ rep * ", " }"
m.reply "Total SOTD count: #{totalcount[0][0]}"
end
end
class SotdGet
include Cinch::Plugin
extend Invisibleify
match "sotd"
match /sotd ([a-zA-Z0-9]+)/
match /listsotd/
def execute(m, username = "")
if username.to_s.empty? then
if m.channel? and m.channel.name == "#tildetown" then
m.reply "Check out http://tilde.town/~severak/town_radio.html, or run this command in another channel such as #music, #bots, or #sotd for a list of current SOTDs"
else
rows = $db.execute <<-SQL
SELECT sotd.username, sotd.display, sotd.link, sotd.created_at
FROM sotd sotd
INNER JOIN (
SELECT MAX(created_at) created_at, username
FROM sotd
WHERE created_at > DATETIME('now', '-2 days')
GROUP BY username
) AS s1
ON sotd.username = s1.username
AND sotd.created_at = s1.created_at
ORDER BY sotd.created_at DESC
SQL
rows.each do |row|
time = DateTime.parse(row[3]).to_time
period = TimeLord::Period.new(time, Time.now).to_words
m.reply "#{Invisibleify.invisibleify(row[0])}: #{row[1]} <#{row[2]}> (#{period})"
sleep 0.25
end
end
else
rows = $db.execute <<-SQL
SELECT username, display, link, created_at
FROM sotd
WHERE username='#{username}' OR username='~#{username}'
ORDER BY created_at DESC
LIMIT 1;
SQL
rows.each do |row|
time = DateTime.parse(row[3]).to_time
period = TimeLord::Period.new(time, Time.now).to_words
m.reply "#{Invisibleify.invisibleify(row[0])}: #{row[1]} <#{row[2]}> (#{period})"
sleep 0.25
end
end
end
end
class SotdAll
include Cinch::Plugin
match /allmysotd(?: (\d+))?/
def execute(m, page = 0)
if m.channel? then
return m.reply "Please message this command to me directly as it is quite verbose. Try /msg sotdbot !allmysotd "
end
page ||= 1
page = [page.to_i - 1, 0].max
pagelength = 10.to_f
usercount = $db.execute "SELECT COUNT(*) FROM SOTD WHERE username='#{m.user.user}' LIMIT 1;"
count = usercount[0][0]
@totalpages = count / pagelength
@totalpages = @totalpages.ceil
if count < (page.to_i * pagelength) then
return m.reply "No more pages."
else
rows = $db.execute <<-SQL
SELECT display, link, created_at
FROM sotd
WHERE username='#{m.user.user}'
ORDER BY created_at DESC
LIMIT #{pagelength}
OFFSET #{pagelength * page};
SQL
rows.each do |row|
time = DateTime.parse(row[2]).to_time
period = TimeLord::Period.new(time, Time.now).to_words
m.reply "#{row[0]} <#{row[1]}> (#{period})"
sleep 0.25
end
m.reply("Page #{page + 1} of #{@totalpages} (#{count} songs total)")
end
end
end
bot = Cinch::Bot.new do
configure do |c|
c.server = "localhost"
if ARGV.include? "--debug" then
c.channels = ["#sotd"]
else
c.channels = ["#tildetown", "#bots", "#sotd", "#music"]
end
c.nick = "sotdbot"
c.realname = "sotdbot"
c.user = "sotdbot"
c.plugins.prefix = "!"
c.plugins.plugins = [
SotdHelp, SotdUpdate, SotdStats, SotdGet, SotdTest, SotdAll, RegenHTML
]
end
end
bot.start