From 0ed413c61d2492a32e5d753cf3bc1e7f6113be23 Mon Sep 17 00:00:00 2001 From: Archina Date: Mon, 19 Dec 2016 23:39:34 -0600 Subject: [PATCH 1/3] Admin command * Added a rudimental admin command to the bot * config.yaml can now be extended by an attribute admins that can contain a list of permanent admins --- mobot.rb | 152 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 86 insertions(+), 66 deletions(-) diff --git a/mobot.rb b/mobot.rb index abcb31a..aab29c5 100644 --- a/mobot.rb +++ b/mobot.rb @@ -7,11 +7,17 @@ #Load config using YAML config = YAML.load_file("config.yaml") - #Global database accessible by all threads, technically a problem, but honestly what are the chances? $members = [] +$admins = [] $missions = [] +if config.key? "admins" + config["admins"] = config.fetch( "admins", [] ) +end + +$admins.concat config["admins"] + #Main bot definition mobot = Cinch::Bot.new do @@ -56,7 +62,7 @@ def add_trivia(amount) def add_uno(amount) @credits = @credits + amount + 50 end - + #Toggles per user daily variable - NOT responsible for threaded timing def daily_claim() @@ -107,63 +113,63 @@ def mission_reset() end def buy(item, recipient, m) if item == "kick" - if @credits > 999 - @credits = @credits - 1000 - m.channel.kick(recipient) - val = "User kicked!" - else - val = "You need 1000 credits to kick someone!" - end + if @credits > 999 + @credits = @credits - 1000 + m.channel.kick(recipient) + val = "User kicked!" + else + val = "You need 1000 credits to kick someone!" + end end if item == "devoice" - if @credits > 1999 - @credits = @credits - 2000 - m.channel.devoice(recipient) - val = "User devoiced!" - else - val = "You need 2000 credits to devoice someone!" - end + if @credits > 1999 + @credits = @credits - 2000 + m.channel.devoice(recipient) + val = "User devoiced!" + else + val = "You need 2000 credits to devoice someone!" + end end if item == "DEX" - amount = 450 + 50 * @dex - if @credits > amount - 1 - @credits = @credits - amount - @dex = @dex + 1 - val = "DEX upgraded! Your DEX is now #{@dex}!" - else - val = "You need #{amount} credits to upgrade your DEX!" - end - end + amount = 450 + 50 * @dex + if @credits > amount - 1 + @credits = @credits - amount + @dex = @dex + 1 + val = "DEX upgraded! Your DEX is now #{@dex}!" + else + val = "You need #{amount} credits to upgrade your DEX!" + end + end if item == "INT" - amount = 450 + 50 * @int - if @credits > amount - 1 - @credits = @credits - amount - @int = @int + 1 - val = "INT upgraded! Your INT is now #{@int}!" - else - val = "You need #{amount} credits to upgrade your INT!" - end - end + amount = 450 + 50 * @int + if @credits > amount - 1 + @credits = @credits - amount + @int = @int + 1 + val = "INT upgraded! Your INT is now #{@int}!" + else + val = "You need #{amount} credits to upgrade your INT!" + end + end if item == "STR" - amount = 450 + 50 * @str - if @credits > amount - 1 - @credits = @credits - amount - @str = @str + 1 - val = "STR upgraded! Your STR is now #{@str}!" - else - val = "You need #{amount} credits to upgrade your STR!" - end - end + amount = 450 + 50 * @str + if @credits > amount - 1 + @credits = @credits - amount + @str = @str + 1 + val = "STR upgraded! Your STR is now #{@str}!" + else + val = "You need #{amount} credits to upgrade your STR!" + end + end if item == "LCK" - amount = 450 + 50 * @lck - if @credits > amount - 1 - @credits = @credits - amount - @lck = @lck + 1 - val = "LCK upgraded! Your LCK is now #{@lck}!" - else - val = "You need #{amount} credits to upgrade your LCK!" - end - end + amount = 450 + 50 * @lck + if @credits > amount - 1 + @credits = @credits - amount + @lck = @lck + 1 + val = "LCK upgraded! Your LCK is now #{@lck}!" + else + val = "You need #{amount} credits to upgrade your LCK!" + end + end val end end @@ -184,9 +190,9 @@ def get_user(user, mem) begin $members = Marshal.load File.read('./database') rescue - puts "Failed to load database!" + puts "Failed to load database!" end - + #Initial Bot Config configure do |c| c.realname = config['config']['realname'] @@ -200,10 +206,9 @@ def get_user(user, mem) c.plugins.options[Cinch::Plugins::Identify] = { :password => config['config']['password'], :type => :nickserv, - } + } end - #$missions.push(Mission.new("The Jade Figurine - Lysana & Undertaker", "DEX", 70, "You find a rare jade figurine that belongs to a Chinese politician. He send a group of ninjas to hunt you down and reclaim it.", "Despite all odds your agile maneuvering is enough to allude the ninjas. Impressed, they report back to their leader, and you are allowed to keep the figurine.", "Unable to escape the ninjas, you are forced to leave the figurine behind. As you do, the ninjas stop their chase, and you come home to binge on healbot syringes, licking your wounds on the close escape.")) $missions.concat(loadMissions()) $members.each do |i| @@ -221,8 +226,8 @@ def get_user(user, mem) on :message, /^SEND (#.+)/ do |m, args| lst = args.split(' ') - contents = lst[1..lst.length].join(' ') - Channel(lst[0]).send(contents) + contents = lst[1..lst.length].join(' ') + Channel(lst[0]).send(contents) end on :message, /^MESSAGE (.+)/ do |m, args| @@ -243,9 +248,9 @@ def get_user(user, mem) end on :message, /^.credits/ do |m| - lst = m.message.split(' ') - if lst.length > 1 - m.reply mobot.get_user(lst[1], $members).credits + lst = m.message.split(' ') + if lst.length > 1 + m.reply mobot.get_user(lst[1], $members).credits else m.reply mobot.get_user(m.user.to_s, $members).credits end @@ -271,7 +276,7 @@ def get_user(user, mem) if not user.crew == "%NONE" statblock = [] mobot.get_user(user.crew, $members).crew_array.each do |i| - statblock.push(mobot.get_user(i, $members).get_stats) + statblock.push(mobot.get_user(i, $members).get_stats) end stats = [1, 1, 1, 1] statblock.each do |i| @@ -332,7 +337,7 @@ def get_user(user, mem) m.reply "You already went on a mission recently! Take a break for a minute or three." end end - + on :message, ".help" do |m| User(m.user.to_s).send("Hi! I'm mobot! I allow you to save up credits via playing games with other bots in irc such as UNOBot or Trivia, and eventually spend them to devoice or kick other $members, even if you don't have permission to do so. Try doing '.daily' to get started.") @@ -387,7 +392,7 @@ def get_user(user, mem) lst = arg.split(' ') amount = lst[0] recipient = lst[1] - if m.user.to_s == 'varzeki' + if $admins.include? m.user.to_s user = mobot.get_user(recipient, $members) user.credits = user.credits + amount.to_i m.reply "User credited!" @@ -397,6 +402,22 @@ def get_user(user, mem) end end + on :message, /^.admin (.+)/ do |m, arg| + lst = arg.split(' ') + recipient = lst[0] + if !(config["admins"].include? recipient) && $admins.include? m.user.to_s + if $admins.include? recipient + $admins.delete_at($admins.find_index(recipient)) + m.reply "#{recipient} was removed from admins." + else + $admins.push(recipient) + m.reply "#{recipient} was added as an admin." + end + else + m.reply "You don't have permission to do that!" + end + end + on :message, /^.crew (.+)/ do |m, arg| lst = arg.split(' ') user = mobot.get_user(m.user.to_s, $members) @@ -500,7 +521,7 @@ def get_user(user, mem) end - + on :message, /^.rob (.+)/ do |m, arg| lst = arg.split(' ') robber = mobot.get_user(m.user.to_s, $members) @@ -573,4 +594,3 @@ def get_user(user, mem) #Start mobot.start - From fd75757414aff11ea23295aa6c550c5e1643f71f Mon Sep 17 00:00:00 2001 From: Archina Date: Mon, 19 Dec 2016 23:39:34 -0600 Subject: [PATCH 2/3] Admin command * Added a rudimental admin command to the bot * config.yaml can now be extended by an attribute admins that can contain a list of permanent admins --- mobot.rb | 165 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 72 deletions(-) diff --git a/mobot.rb b/mobot.rb index 2780e44..b6f2975 100644 --- a/mobot.rb +++ b/mobot.rb @@ -7,11 +7,17 @@ #Load config using YAML config = YAML.load_file("config.yaml") - #Global database accessible by all threads, technically a problem, but honestly what are the chances? $members = [] +$admins = [] $missions = [] +if config.key? "admins" + config["admins"] = config.fetch( "admins", [] ) +end + +$admins.concat config["admins"] + #Main bot definition mobot = Cinch::Bot.new do @@ -109,67 +115,67 @@ def daily_reset() def mission_reset() @mission = false end - def buy(item, recipient, m) - if item == "kick" - if @credits > 999 - @credits = @credits - 1000 - m.channel.kick(recipient) - val = "User kicked!" - else - val = "You need 1000 credits to kick someone!" + def buy(item, recipient, m) + if item == "kick" + if @credits > 999 + @credits = @credits - 1000 + m.channel.kick(recipient) + val = "User kicked!" + else + val = "You need 1000 credits to kick someone!" + end + end + if item == "devoice" + if @credits > 1999 + @credits = @credits - 2000 + m.channel.devoice(recipient) + val = "User devoiced!" + else + val = "You need 2000 credits to devoice someone!" + end + end + if item == "DEX" + amount = 450 + 50 * @dex + if @credits > amount - 1 + @credits = @credits - amount + @dex = @dex + 1 + val = "DEX upgraded! Your DEX is now #{@dex}!" + else + val = "You need #{amount} credits to upgrade your DEX!" + end end + if item == "INT" + amount = 450 + 50 * @int + if @credits > amount - 1 + @credits = @credits - amount + @int = @int + 1 + val = "INT upgraded! Your INT is now #{@int}!" + else + val = "You need #{amount} credits to upgrade your INT!" + end end - if item == "devoice" - if @credits > 1999 - @credits = @credits - 2000 - m.channel.devoice(recipient) - val = "User devoiced!" - else - val = "You need 2000 credits to devoice someone!" - end + if item == "STR" + amount = 450 + 50 * @str + if @credits > amount - 1 + @credits = @credits - amount + @str = @str + 1 + val = "STR upgraded! Your STR is now #{@str}!" + else + val = "You need #{amount} credits to upgrade your STR!" + end end - if item == "DEX" - amount = 450 + 50 * @dex - if @credits > amount - 1 - @credits = @credits - amount - @dex = @dex + 1 - val = "DEX upgraded! Your DEX is now #{@dex}!" - else - val = "You need #{amount} credits to upgrade your DEX!" - end - end - if item == "INT" - amount = 450 + 50 * @int - if @credits > amount - 1 - @credits = @credits - amount - @int = @int + 1 - val = "INT upgraded! Your INT is now #{@int}!" - else - val = "You need #{amount} credits to upgrade your INT!" - end - end - if item == "STR" - amount = 450 + 50 * @str - if @credits > amount - 1 - @credits = @credits - amount - @str = @str + 1 - val = "STR upgraded! Your STR is now #{@str}!" - else - val = "You need #{amount} credits to upgrade your STR!" + if item == "LCK" + amount = 450 + 50 * @lck + if @credits > amount - 1 + @credits = @credits - amount + @lck = @lck + 1 + val = "LCK upgraded! Your LCK is now #{@lck}!" + else + val = "You need #{amount} credits to upgrade your LCK!" + end end - end - if item == "LCK" - amount = 450 + 50 * @lck - if @credits > amount - 1 - @credits = @credits - amount - @lck = @lck + 1 - val = "LCK upgraded! Your LCK is now #{@lck}!" - else - val = "You need #{amount} credits to upgrade your LCK!" - end - end - val - end + val + end end @@ -188,7 +194,7 @@ def get_user(user, mem) begin $members = Marshal.load File.read('./database') rescue - puts "Failed to load database!" + puts "Failed to load database!" end #Initial Bot Config @@ -207,7 +213,6 @@ def get_user(user, mem) } end - #$missions.push(Mission.new("The Jade Figurine - Lysana & Undertaker", "DEX", 70, "You find a rare jade figurine that belongs to a Chinese politician. He send a group of ninjas to hunt you down and reclaim it.", "Despite all odds your agile maneuvering is enough to allude the ninjas. Impressed, they report back to their leader, and you are allowed to keep the figurine.", "Unable to escape the ninjas, you are forced to leave the figurine behind. As you do, the ninjas stop their chase, and you come home to binge on healbot syringes, licking your wounds on the close escape.")) $missions.concat(loadMissions()) $members.each do |i| @@ -225,8 +230,8 @@ def get_user(user, mem) on :message, /^SEND (#.+)/ do |m, args| lst = args.split(' ') - contents = lst[1..lst.length].join(' ') - Channel(lst[0]).send(contents) + contents = lst[1..lst.length].join(' ') + Channel(lst[0]).send(contents) end on :message, /^MESSAGE (.+)/ do |m, args| @@ -397,16 +402,32 @@ def get_user(user, mem) on :message, /^.cmod (.+)/ do |m, arg| lst = arg.split(' ') - amount = lst[0] - recipient = lst[1] - if m.user.to_s == 'varzeki' - user = mobot.get_user(recipient, $members) - user.credits = user.credits + amount.to_i - m.reply "User credited!" - mobot.update_db($members) - else - m.reply "You don't have permission to do that!" - end + amount = lst[0] + recipient = lst[1] + if $admins.include? m.user.to_s + user = mobot.get_user(recipient, $members) + user.credits = user.credits + amount.to_i + m.reply "User credited!" + mobot.update_db($members) + else + m.reply "You don't have permission to do that!" + end + end + + on :message, /^.admin (.+)/ do |m, arg| + lst = arg.split(' ') + recipient = lst[0] + if !(config["admins"].include? recipient) && $admins.include? m.user.to_s + if $admins.include? recipient + $admins.delete_at($admins.find_index(recipient)) + m.reply "#{recipient} was removed from admins." + else + $admins.push(recipient) + m.reply "#{recipient} was added as an admin." + end + else + m.reply "You don't have permission to do that!" + end end on :message, /^.crew (.+)/ do |m, arg| From 9e3ae5eb43f5daca4f85e414386f9b4a9e0abcf5 Mon Sep 17 00:00:00 2001 From: Archina Date: Tue, 20 Dec 2016 08:24:04 -0600 Subject: [PATCH 3/3] Small control flow issue * Fixed small issue with the control flow * Fixed key query being inverse * Fixed skeleton being broken --- config.yaml | 2 +- mobot.rb | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/config.yaml b/config.yaml index 23685dd..29cabf5 100644 --- a/config.yaml +++ b/config.yaml @@ -2,6 +2,6 @@ config: server: irc.server.tld port: 6667 nick: default_nickname - channels: ['#put, '#your', '#channels', "#here"] + channels: ['#put', '#your', '#channels', "#here"] realname: default_realname password: password diff --git a/mobot.rb b/mobot.rb index b6f2975..07fd102 100644 --- a/mobot.rb +++ b/mobot.rb @@ -12,7 +12,7 @@ $admins = [] $missions = [] -if config.key? "admins" +if !config.key? "admins" config["admins"] = config.fetch( "admins", [] ) end @@ -24,7 +24,6 @@ #Holds threads created by methods - mostly timers threads = [] - #Should be called whenever the database is changed for any reason def update_db(cont) db = File.open('./database', 'w') @@ -417,7 +416,7 @@ def get_user(user, mem) on :message, /^.admin (.+)/ do |m, arg| lst = arg.split(' ') recipient = lst[0] - if !(config["admins"].include? recipient) && $admins.include? m.user.to_s + if (!config["admins"].include? recipient) && ($admins.include? m.user.to_s) if $admins.include? recipient $admins.delete_at($admins.find_index(recipient)) m.reply "#{recipient} was removed from admins."