Class: MarxBot

Inherits:
Object
  • Object
show all
Defined in:
lib/marxbot.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, auto_stop: false, stop_time: 60*15) ⇒ MarxBot

Returns a new instance of MarxBot.



11
12
13
14
15
16
17
18
19
# File 'lib/marxbot.rb', line 11

def initialize(token:, auto_stop:false, stop_time:60*15)
  @token = token
  @auto_stop = auto_stop
  @stop_time = stop_time
  @bot = Discordrb::Bot.new token: @token
  @noun_path = File.join(File.dirname(__FILE__), "noun.json")
  @adjective_path = File.join(File.dirname(__FILE__), "adjective.json")
  @slogan_path = File.join(File.dirname(__FILE__), "slogans.json")
end

Instance Attribute Details

#auto_stopObject (readonly)

Returns the value of attribute auto_stop.



10
11
12
# File 'lib/marxbot.rb', line 10

def auto_stop
  @auto_stop
end

#botObject (readonly)

Returns the value of attribute bot.



10
11
12
# File 'lib/marxbot.rb', line 10

def bot
  @bot
end

#stop_timeObject (readonly)

Returns the value of attribute stop_time.



10
11
12
# File 'lib/marxbot.rb', line 10

def stop_time
  @stop_time
end

Instance Method Details

#adjective_dictObject



53
54
55
# File 'lib/marxbot.rb', line 53

def adjective_dict
  return JSON.parse(File.read(@adjective_path))
end

#get_adjectiveObject



33
34
35
36
37
# File 'lib/marxbot.rb', line 33

def get_adjective()
  adjectives = adjective_dict["adjs"]
  adj_choice = Random.new.rand(adjectives.length)
  return adjectives[adj_choice]
end

#get_lewdObject



57
58
59
60
61
# File 'lib/marxbot.rb', line 57

def get_lewd()
  lewd_page = Nokogiri::HTML(URI.open('https://rule34.xxx/index.php?page=post&s=random'))
  lewd_link = lewd_page.css('img')[2].attributes["src"].value
  return lewd_link
end

#get_nounObject



39
40
41
42
43
# File 'lib/marxbot.rb', line 39

def get_noun()
  nouns = noun_dict["nouns"]
  noun_choice = Random.new.rand(nouns.length)
  return nouns[noun_choice]
end

#get_sloganObject



27
28
29
30
31
# File 'lib/marxbot.rb', line 27

def get_slogan()
  all_slogans = slogan_dict["slogans"]
  slogan_number = Random.new.rand(all_slogans.length)
  return all_slogans[slogan_number]
end

#noun_dictObject



49
50
51
# File 'lib/marxbot.rb', line 49

def noun_dict
  return JSON.parse(File.read(@noun_path))
end

#runObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/marxbot.rb', line 63

def run()
  @bot.message(content: 'ping') do |event|
    event.respond 'pong'
  end

  @bot.message(containing: '!sleep') do |event|
     event.respond("\"Good night Moon.\"")
     @bot.stop
  end

  @bot.message(containing: '!slogan') do |event|
    puts "event: #{event.user.id.class}"
      event.respond("\"#{get_slogan}\"")
  end

  @bot.message(containing: '!kneel') do |event|
    has_specific = event.message.to_s.include?(">>")
    specific = event.message.to_s.split(">>").last
    puts "event: #{event.user.id.class}"

    event.server.members.each{ |member|
      begin
        current = member.nick
        if has_specific
          new_name = specific
        else
          new_name = "#{get_adjective} #{get_noun}"
        end

        member.set_nickname(new_name)
        if member.online? && false
          member.pm(
            "Know your place! You have been assigned #{new_name}. _All Glory to CHAOS_\n
            \"#{get_slogan}\""
          )
        end
      rescue Exception => e
        puts "big OOPS #{e}"
      end
    }
     event.respond("\"#{get_slogan}\"")
  end

  @bot.message(containing: '!normal') do |event|
    event.server.members.each{ |member|
      begin
         puts member.nick
        current = member.nick
        new_name = member.username
        member.set_nickname(new_name)
      rescue Exception => e
        puts "big OOPS #{e}"
      end
    }

    response_list = [
      "no fun.",
      "Boooooo",
      "BOOOOOO!",
      "GAY",
      "that's not very cash money of you.",
      "Nark.",
      get_lewd
    ]

    event.respond(response_list.sample)
  end

  @bot.message(containing: '!silence') do |event|
    # if event.user.id.to_s == ENV['KEVIN_ID']
      event.server.members.each{ |member|
        begin
          if member.voice_channel
            puts "name: #{member.display_name}"
            member.server_mute
            if member.online? && false
              member.pm(
                "Silence.\n
                \"#{get_slogan}\""
              )
            end
          end
        rescue Exception => e
          puts "big OOPS #{e}"
        end
      }
      event.respond("\"#{get_slogan}\"")
    # end
  end

  @bot.message(containing: '!speak') do |event|
    # if event.user.id.to_s == ENV['KEVIN_ID']
      event.server.members.each{ |member|
        begin
          if member.voice_channel
            puts "name: #{member.display_name}"
            member.server_unmute
            if member.online? && false
              member.pm(
                "You speak because I let you speak.\n
                \"#{get_slogan}\""
              )
            end
          end
        rescue Exception => e
          puts "big OOPS #{e}"
        end
      }
      event.respond("\"#{get_slogan}\"")
    # end
  end

  @bot.message(containing: '!lewd') do |event|
    begin
      if event.channel.name == "super-weenie-hut-juniors"
        event.respond("https://media.giphy.com/media/5ftsmLIqktHQA/giphy.gif")
        event.respond("no lewds in super-weenie-hut-juniors")
      else
        event.respond(get_lewd)
      end
      puts "event: #{event}"
    rescue Exception => e
      puts "big OOPS #{e}"
    end
  end

  @bot.message(containing: '!biglewd') do |event|
    begin
      history = event.channel.history(limit = 100)
      lewd_history = history.select{|message| message.content.include?("!lewd")}
      puts "event: #{lewd_history.length}"
      20.times{ |n|
        sleep(5)
        event.respond(get_lewd)
      }
      event.respond("fine.")
    rescue Exception => e
      puts "big OOPS #{e}"
    end
  end

  @bot.message(containing: '!chaste') do |event|
    begin
      history = event.channel.history(limit = 100)
      lewd_history = history.select{|message| message.content.include?("rule34.xxx")}
      if lewd_history.length < 2
        lewd_history << history.find{|message| message.content.include?("!lewd")}
      end
      puts "event: #{lewd_history.length}"
      event.channel.delete_messages(lewd_history)
      event.respond("fine.")
    rescue Exception => e
      puts "big OOPS #{e}"
    end
  end

  @bot.message(containing: '!fortune') do |event|
    begin
      fortune_teller = ::Tarot::Fortune.new()
      fortune = fortune_teller.tell_fortune()
      card_str = fortune[:cards].map{|card| "#{card.flipped ? "Reversed ":""} #{card.name}"}.join("\n")
      result_str = fortune[:result]
      event.respond("\`\`\`#{card_str}\`\`\`")
      event.respond(result_str)
    rescue Exception => e
      puts "big OOPS #{e}"
      event.respond(e)
    end
  end

  if @auto_stop
    threads = []
    threads << Thread.new { @bot.run }
    threads << Thread.new { self.stop_eventually() }
    threads.each(&:join)
  else
    @bot.run
  end
end

#slogan_dictObject



45
46
47
# File 'lib/marxbot.rb', line 45

def slogan_dict
  return JSON.parse(File.read(@slogan_path))
end

#stop_eventuallyObject



21
22
23
24
25
# File 'lib/marxbot.rb', line 21

def stop_eventually()
  sleep(@stop_time)
  @bot.stop
  puts "$$$ The Bot should be stopped $$$"
end