Module: Z4bot

Defined in:
lib/z4bot.rb,
lib/z4bot/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.1"
COMMIT =
`git log --format="%H" -n 1`.strip
@@BOT =
Discordrb::Commands::CommandBot.new token: ENV['Z4_DISCORD_TOKEN'], prefix: '#'
@@BOT_ASK =
{}
@@BOT_CMD =
{}
@@BOT_RES =
{}
@@STATE =
Hash.new { |h,k| h[k] = {} }
@@NEW =
lambda { |e| @@STATE[e] }
@@OBJ_NEW =
Hash.new { |h,k| h[k] = @@NEW }
@@OBJ =
Hash.new { |h,k| h[k] = @@OBJ_NEW[k] }
@@FIELDS =
{
  discord: 'passport_control',
  phone: 'telephone',
  social: 'star2',
  store: 'convenience_store',
  tips: 'moneybag',
  ngrok: 'house',
  embed: 'information_source',
  img: 'frame_photo'
}
@@KEYS =

standard fields for #i interface & contactor

{
  user: [ :name, :dob, :age, :city, :here, :grid, :since, :lvl, :xp, :gp, :job, :team, :union, :wins, :losses, :points, :turns, :played ],
  chan: [ :desc, :city, :since, :wins, :losses, :points, :turns, :played ],
  host: [ :desc ]
}

Class Method Summary collapse

Class Method Details

.ask(r, h = {}, &b) ⇒ Object

create a regex input handler.



83
84
85
# File 'lib/z4bot.rb', line 83

def self.ask(r, h={}, &b)
  @@BOT_ASK[r] = [h, b]
end

.clientObject



65
66
67
# File 'lib/z4bot.rb', line 65

def self.client
  @@BOT
end

.command(r, h = {}, &b) ⇒ Object

create a regex input handler.



79
80
81
# File 'lib/z4bot.rb', line 79

def self.command(r, h={}, &b)
  @@BOT_CMD[r] = [h, b]
end

.default(&b) ⇒ Object



43
44
45
# File 'lib/z4bot.rb', line 43

def self.default &b
  @@NEW = b
end

.event(e) ⇒ Object



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
# File 'lib/z4bot.rb', line 91

def self.event e
  aa = []
  [e.message.attachments].flatten.each { |e| aa << e.url }
  if e.server
    d = %[#{e.server.id}]
  else
    d = %[#{e.channel.id}]
  end
  # private message?
  if e.channel.name == e.user.name
    pm = true
  else
    pm = false
  end
  # process message as [cmd] msg
  t = "#{e.message.text}"
  w = t.split(" ")
  emm = []
  w.each { |e| if /\\.+/.match(e); emm << e; end }
  if /^#.+/.match(w[0])
    cmd = w.shift
    msg = w.join(" ").gsub(/<.*>/, '').gsub("  ", " ").gsub("   ", " ").gsub(/^ /, "")
  else
    msg = t.gsub(/<.*>/, '').gsub("  ", " ").gsub("   ", " ").gsub(/^ /, "")
  end
  # extract user mentions.
  us = []
  if e.message.mentions.length > 0
    e.message.mentions.each {|ee| us << "#{ee.id}" }
  end
  # extract role mentions.
  ro = []
  if e.message.role_mentions.length > 0
    e.message.role_mentions.each {|ee| ro << ee.name }
  end
  pv = []
  lvl = 0
  # update lvl by role.
  if e.author.roles.length > 0
    e.author.roles.each { |ee|
      {
        operator: 5,
        agent: 4,
        manager: 4,
        ambassador: 3,
        influencer: 2,
        character: 1,
        bartender: 1,
        door: 1,
        floor: 1
      }.each_pair { |k,v|
        if ee.name == k.to_s && lvl < v
          lvl = v
        end
      }
      pv << %[#{ee.name}]
    }
  end
  # return event hash
  h = {
    lvl: lvl,
    db: d,
    server: d,
    pm: pm,
    cmd: cmd,
    msg: msg,
    words: w,
    user: "#{e.user.id}",
    nick: "#{e.user.name}",
    chan: "#{e.channel.id}",
    channel: "#{e.channel.name}",
    users: us,
    roles: ro,
    priv: pv,
    emoji: emm,
    attachments: aa
  }
  puts "[EVENT] #{h}"
  puts "[EMOJI] #{emm}"
  return h
end

.fieldsObject



71
72
73
# File 'lib/z4bot.rb', line 71

def self.fields
  @@FIELDS
end

.init!Object

start bot



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
# File 'lib/z4bot.rb', line 173

def self.init!
  # handle commands
  @@BOT_CMD.each_pair do |k,v|
    @@BOT.command(k,v[0],&v[1])
  end
  # handle conversation
  @@BOT.message() do |e|
    t_start = Time.now.to_f
    o = []
    h = Z4bot.event(e)
    if h[:cmd] == nil
      # is question?
      if /^.*\?$/.match(h[:msg])
      # imply messages from regex &block
        @@BOT_ASK.each_pair { |r,b|
          if m = r.match(h[:msg]);
            o << b[1].call(e, m, h, b[0]);
          end
        }
      else        
        # construct response by regex &block
        @@BOT_RES.each_pair { |r,b|
          if m = r.match(h[:msg]);
            o << b[1].call(e, m, h, b[0])
          end
        }
      end
      e.respond(%[#{o.join("\n")}])
    end
  end
  # GO!
  @@PROC = Process.detach( fork { @@BOT.run } )
end

.invite_urlObject

only for joining bot to a server!



75
76
77
# File 'lib/z4bot.rb', line 75

def self.invite_url
  @@BOT.invite_url
end

.keysObject



68
69
70
# File 'lib/z4bot.rb', line 68

def self.keys
  @@KEYS
end

.obj(*u, &b) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/z4bot.rb', line 47

def self.obj *u, &b
  if block_given?
    @@OBJ_NEW[u[0]] = b
  else
    if u.length > 1
      @@OBJ[u[0]].call(u[1])
    elsif u.length > 0
      @@OBJ[u[0]]
    else
      @@OBJ
    end
  end
end

.procObject



206
207
208
# File 'lib/z4bot.rb', line 206

def self.proc
  @@PROC
end

.response(r, h = {}, &b) ⇒ Object

create a regex input mask for pre-defined responses.



88
89
90
# File 'lib/z4bot.rb', line 88

def self.response(r, h={}, &b)
  @@BOT_RES[r] = [h, b]
end

.stateObject



61
62
63
# File 'lib/z4bot.rb', line 61

def self.state
  @@STATE
end