Commit d49a6ca8 authored by Jyrki's avatar Jyrki 🐾

made the bot automatically join the channel with the most users in it

parent f2da8b6d
require_relative './mumble-ruby/client'
\ No newline at end of file
module Mumble
class Client
alias super_init initialize
# ./lib/mumble-ruby/client.rb:14
def initialize(host, port=64738, username="RubyClient", password="")
@after_callbacks = Hash.new { |h, k| h[k] = [] }
super_init host, port, username, password
yield(@config) if block_given?
end
# Joins the channel with the most users
# @param default_channel [String] The channel to join if there are no other users on the Mumble server.
def join_channel_with_most_users(default_channel = APP_CONFIG['mumble']['channel'])
# this is probably the best one-liner I've ever written in Ruby so far; and it even works! --nilsding
chans_with_users = users.values.map{ |x| channels[x.channel_id] unless x == me}.compact.inject(Hash.new(0)){ |h, c| h[c] += 1; h }.sort_by{ |_k, v| v }.reverse
if chans_with_users.empty?
writeln! "[Mumble] Looks like we're alone, joining channel \033[1m#{default_channel}\033[0m."
join_channel default_channel
return
end
chan_with_most_users = chans_with_users.first
writeln! "[Mumble] Joining channel \033[1m#{chan_with_most_users[0].name}\033[0m, as there are #{chan_with_most_users[1]} users in it."
join_channel chan_with_most_users[0]
end
Messages.all_types.each do |msg_type|
# I needed a way to guarantee that a block runs after a specific callback finished.
define_method "after_#{msg_type}" do |&block|
@after_callbacks[msg_type] << block
end
end
private
# ./lib/mumble-ruby/client.rb:138
def run_callbacks(sym, *args)
@callbacks[sym].each { |c| c.call *args }
@after_callbacks[sym].each { |c| c.call *args }
end
# small hack to not spam the console output
def writeln!(msg)
puts msg unless msg == @last_message
@last_message = msg
end
end
end
\ No newline at end of file
......@@ -7,6 +7,7 @@ require "mumble-ruby"
$LOAD_PATH.unshift File.expand_path './lib', File.dirname(__FILE__)
require "irc_colors"
require "ext/mumble-ruby"
VERSION = "0.2.4"
......@@ -170,15 +171,23 @@ def start(irc, mumble)
#end of on_text_message
mumble.on_user_state do |state|
next unless state.include? "name"
next unless mumble.connected?
next unless APP_CONFIG['mumble']['announce_joins']
name = state['name'].gsub("\n", '')
puts "[Mumble] #{name} connected"
APP_CONFIG['irc']['channel'].each do |channel|
irc.send_message(channel, "\x039[+]\x0F \x02#{name}\x0f connected to #{APP_CONFIG['mumble']['server']}")
next if mumble.users[state['actor']] == mumble.me
if state.include? "name"
next unless APP_CONFIG['mumble']['announce_joins']
name = state['name'].gsub("\n", '')
puts "[Mumble] #{name} connected"
APP_CONFIG['irc']['channel'].each do |channel|
irc.send_message(channel, "\x039[+]\x0F \x02#{name}\x0f connected to #{APP_CONFIG['mumble']['server']}")
end
end
end
mumble.after_user_state do |state|
next unless mumble.connected?
next if mumble.users[state['actor']] == mumble.me
mumble.join_channel_with_most_users
end
mumble.on_user_remove do |x|
next unless APP_CONFIG['mumble']['announce_joins']
......@@ -190,18 +199,22 @@ def start(irc, mumble)
irc.send_message(channel, "\x034[-]\x0F \x02#{name}\x0f disconnected from #{APP_CONFIG['mumble']['server']}")
end
end
mumble.connect
puts "[Mumble] Connected to server #{APP_CONFIG['mumble']['server']}"
mumble.after_user_remove do
mumble.join_channel_with_most_users
end
mumble.on_connected do
mumble.me.mute
mumble.me.deafen
end
sleep(2)
mumble.join_channel(APP_CONFIG['mumble']['channel'])
puts "[Mumble] Joined channel: #{APP_CONFIG['mumble']['channel']}"
mumble.on_server_sync do
puts "[Mumble] Connected to server #{APP_CONFIG['mumble']['server']}"
mumble.join_channel_with_most_users
end
mumble.connect
end
@irc_thread.join
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment