Created
July 29, 2014 23:07
-
-
Save flotwig/601049c0b8710e99b0fd to your computer and use it in GitHub Desktop.
irc.snoonet.org ##random
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
require 'cinch' | |
require 'open-uri' | |
require 'json' | |
require 'date' | |
class Rando | |
include Cinch::Plugin | |
match /raw (.+)/, method: :do_raw | |
match /refresh/, method: :do_refresh | |
match /index/, method: :do_index_get | |
match /index (.+)/, method: :do_index_set | |
match /random/, method: :do_chan_get | |
listen_to :join, method: :do_join | |
listen_to :connect, method: :do_connect | |
def do_chan_get(m) | |
m.reply('Random channel which user is not in, banned from, invite-only or keyed: '+get_random_channel(m.user).to_s) | |
end | |
def do_index_get(m) | |
m.reply('User not oper, cannot issue commands.') and return unless m.user.oper? | |
m.reply('index: '+@index) | |
end | |
def do_index_set(m,value) | |
m.reply('User not oper, cannot issue commands.') and return unless m.user.oper? | |
@index = value | |
do_config_get(m) | |
end | |
def do_connect(*) | |
@index = 'https://stats.snoonet.org/rest/service.php/channels/biggest/150' | |
@age = 0 | |
@topic_base = '05C04h08a03n11n02e12l06 r05o04u08l03e11t02t12e06! Join ##random and discover a new random channel to hang out in!' | |
@latest_channels = Array.new | |
end | |
def do_raw(m,line) | |
m.reply('User not oper, cannot issue commands.') and return unless m.user.oper? | |
m.reply('Issuing raw command...') | |
@bot.irc.send(line) | |
m.reply('Raw command issued.') | |
end | |
def do_refresh(m) | |
m.reply('User not oper, cannot issue commands.') and return unless m.user.oper? | |
get_channels | |
m.reply('Requested new channel list. '+@channels.length.to_s+' channels found from index.') | |
end | |
def do_join(m) | |
return if m.user.nick == bot.nick | |
m.reply('Join detected.') | |
if Time.now.to_i > (@age + 30) | |
get_channels | |
m.reply('Requested new channel list, old one out of date. '+@channels.length.to_s+' channels found.') | |
end | |
new_channel = get_random_channel(m.user) | |
m.reply('Joining user to channel '+new_channel.to_s+'.') unless new_channel.nil? | |
@bot.irc.send('SAJOIN '+m.user.nick+' '+new_channel.to_s) unless new_channel.nil? | |
@latest_channels.slice!(0,4) | |
@latest_channels.unshift(new_channel.to_s) | |
m.channel.topic=(@topic_base+' Latest channel: '+@latest_channels.join(', ')) | |
m.reply('Removing user from ##random.') | |
@bot.irc.send('REMOVE '+m.user.nick+' ##random :Joined user to random channel '+new_channel.to_s+'. Bye!') unless m.user.oper? | |
m.reply('User is oper, did not remove.') if m.user.oper? | |
end | |
def get_random_channel(user) | |
@channels.shuffle! | |
@channels.each do |channel| | |
unless user.channels.include? Channel(channel['channel']) | |
channelX = Channel(channel['channel']) | |
next if channelX.modes['i'] or | |
channelX.modes['k'] | |
channelX.bans.each do |ban| | |
next if ban.match(user) | |
end | |
return Channel(channel['channel']) | |
end | |
end | |
end | |
def get_channels | |
request = JSON.load(open(@index)) | |
@channels = request.to_a if request | |
@age = Time.now.to_i | |
end | |
end | |
bot = Cinch::Bot.new do | |
configure do |c| | |
c.server = "irc.snoonet.org" | |
c.nick = "rando" | |
c.channels = ["##random"] | |
c.plugins.plugins = [Rando] | |
end | |
$age = DateTime.new(2000,1,1) | |
end | |
bot.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment