Last active
March 12, 2016 02:36
-
-
Save celediel/bb5bb81e2ee6e68ffba2 to your computer and use it in GitHub Desktop.
Silly plugin for Cinch to send love to IRC people
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# encoding=utf-8 | |
require 'sequel' | |
# These are all very annoying. | |
# rubocop:disable Metrics/LineLength, Metrics/ClassLength, Metrics/CyclomaticComplexity | |
# rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Metrics/AbcSize | |
module Cinch | |
module Plugins | |
# Send love to all your IRC friends <3 | |
# Also keep track of the love :3 | |
# TODO: Add a command to add types of hugs | |
# via IRC, without having to edit the source | |
# or add the column manually after db is created | |
class Hugs | |
include Cinch::Plugin | |
def initialize(*args) | |
super | |
# Open database, and create table if it doesn't exist | |
@db = Sequel.sqlite('riria.db') | |
@db.create_table? :hugs do | |
primary_key :id | |
String :nick | |
# String :type | |
# Integer :times | |
Integer :hugs_times | |
Integer :tight_times | |
Integer :snuggles_times | |
Integer :cuddles_times | |
Integer :hairpets_times | |
Integer :nuzzles_times | |
Integer :glomps_times | |
end | |
@hugs = @db[:hugs] | |
# The list of actions and their type (for counting) to choose from | |
# TODO: make this a part of the database, in order for the command | |
# to add a type to function properly, and remember added types | |
# String :type, String :message perhaps | |
@hug_strings = { | |
tight: 'hugs %s tight', | |
hugs: 'hugs %s', | |
snuggles: 'snuggles %s', | |
cuddles: 'cuddles %s', | |
hairpets: 'hugs %s and pets their hair', | |
nuzzles: 'nuzzles %s and purrs into their ear', | |
glomps: 'glomps %s with a victorious "Nyaa~!"' | |
} | |
end | |
match(/hug[^s?] ?(\w*)?/, method: :give_hug) | |
def give_hug(m, recipient) | |
# Remove extra whitespace for tab-completion and such | |
recipient.strip! unless recipient.nil? | |
# Enter tsun mode if told to hug self or user, or in PM | |
if m.channel.nil? || recipient.casecmp(m.user.nick).zero? || recipient.casecmp(@bot.nick).zero? | |
m.reply 'N-no, you b-baka!' | |
return | |
end | |
if recipient.empty? | |
m.reply 'Hug who?' | |
return | |
end | |
# Select type and action to reply with | |
type = @hug_strings.keys.sample | |
action = @hug_strings[type] | |
times = (type.to_s + '_times').to_sym | |
# Add to database | |
if @hugs.where(nick: recipient).all.empty? | |
# Not in db, insert | |
num_hugs = 0 | |
@hugs.insert(nick: recipient, times => num_hugs + 1) | |
else | |
# in db, get uid, then update entry | |
uid = @hugs.where(nick: recipient).first[:id] | |
num_hugs = @hugs.where(id: uid).first[times] | |
num_hugs ||= 0 # Set to zero if it's nil | |
@hugs.where(id: uid).update(times => num_hugs + 1) | |
end | |
# Okay, nothing exploded? Let's reply! | |
m.action_reply(action % recipient) | |
end | |
match(/hugs ?(\w*)?/, method: :check_hugs) | |
def check_hugs(m, query) | |
query.strip! | |
query = m.user.nick if query.empty? | |
if @hugs.where(nick: query).all.empty? | |
# Not in db | |
o = "#{query} hasn't recieved any hugs yet. :(" | |
else | |
# Populate output message with type and number | |
# Probably a hackish way to not have things like | |
# "one hugs" and the like | |
all = @hugs.where(nick: query).first | |
o = "#{query} has recieved ::" | |
unless all[:hugs_times].nil? | |
o << " #{all[:hugs_times]} hug" | |
o << 's' unless all[:hugs_times] == 1 | |
o << ' ::' | |
end | |
unless all[:tight_times].nil? | |
o << " #{all[:tight_times]} extra tight hug" | |
o << 's' unless all[:tight_times] == 1 | |
o << ' ::' | |
end | |
unless all[:snuggles_times].nil? | |
o << " #{all[:snuggles_times]} snuggle" | |
o << 's' unless all[:snuggles_times] == 1 | |
o << ' ::' | |
end | |
unless all[:cuddles_times].nil? | |
o << " #{all[:cuddles_times]} cuddle" | |
o << 's' unless all[:cuddles_times] == 1 | |
o << ' ::' | |
end | |
unless all[:hairpets_times].nil? | |
o << " #{all[:hairpets_times]} hug" | |
o.concat all[:hairpets_times] == 1 ? ' with pets' : 's with pets' | |
o << ' ::' | |
end | |
unless all[:nuzzles_times].nil? | |
o << " #{all[:nuzzles_times]} nuzzle" | |
o << 's' unless all[:nuzzles_times] == 1 | |
o << ' ::' | |
end | |
unless all[:glomps_times].nil? | |
o << " #{all[:glomps_times]} glomps" | |
o << 's' unless all[:glomps_times] == 1 | |
o << ' ::' | |
end | |
end | |
# Now that the ouptput message has been populated, let's send it! | |
m.reply(o) | |
end | |
end | |
end | |
end | |
# vim:tabstop=2 softtabstop=2 expandtab shiftwidth=2 smarttab foldmethod=syntax: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment