Module: Onebot::Utils

Extended by:
Utils
Included in:
Utils
Defined in:
lib/Core/Utils.rb

Overview

各种工具包

Example:

Onebot::Utils.log str, Logger::INFO

Instance Method Summary collapse

Instance Method Details

#cqEscape(msg) ⇒ String

消息转义& -> & [ -> [ ] -> ]

Parameters:

Returns:



30
31
32
33
34
35
# File 'lib/Core/Utils.rb', line 30

def cqEscape(msg)
  msg.gsub!('&', '&')
  msg.gsub!('[', '[')
  msg.gsub!(']', ']')
  msg
end

#cqParse(cqmsg) ⇒ Array

CQ码解析, 将字符串格式转换成 Onebot v11 的消息段数组格式

Parameters:

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/Core/Utils.rb', line 55

def cqParse(cqmsg)
  msgary = []
  cqary = cqmsg.scan(/\[CQ:(.*?),(.*?)\]/m)
  isCode = false
  i = 0
  temp = ''
  cqmsg.each_char do |c|
    if isCode
      if c == ']'
        isCode = false
        matches = cqary[i]
        cqcode = { type: matches[0], data: {} }
        matches[1].split(',').each do |arg|
          args = arg.split('=')
          cqcode[:data][args[0].to_sym] = args[1]
        end
        msgary << cqcode
      end
    elsif c == '['
      msgary << { type: 'text', data: { text: cqEscape(temp) } }
      temp = ''
      isCode = true
    else
      temp << c
    end
  end
  msgary << { type: 'text', data: { text: cqEscape(temp) } } unless temp.empty?
  msgary
end

#cqUnescape(msg) ⇒ String

消息反转义& -> &amp; [ -> &#91; ] -> &#93;

Parameters:

Returns:



44
45
46
47
48
49
# File 'lib/Core/Utils.rb', line 44

def cqUnescape(msg)
  msg.gsub!('&', '&amp;')
  msg.gsub!('[', '&#91;')
  msg.gsub!(']', '&#93;')
  msg
end

#httpPost(url, ret) ⇒ String Also known as: post

post发包

Parameters:

Returns:



13
14
15
16
17
18
19
20
# File 'lib/Core/Utils.rb', line 13

def httpPost(url, ret)
  req = Net::HTTP::Post.new(url.path, { 'Content-Type' => 'application/json' })
  req.body = ret
  res = Net::HTTP.start(url.hostname, url.port) do |http|
    http.request(req)
  end
  res.body
end