Class: CQHttp::Utils

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

Overview

各种工具包

Example:

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#fileLoggerLogger

Returns 文件Logger.

Returns:

  • (Logger)

    文件Logger


13
# File 'lib/Bot/Utils.rb', line 13

attr_accessor :stdLogger, :fileLogger, :loggerFile

#loggerFileString

Returns Logger文件地址.

Returns:

  • (String)

    Logger文件地址


13
# File 'lib/Bot/Utils.rb', line 13

attr_accessor :stdLogger, :fileLogger, :loggerFile

#stdLoggerLogger

Returns 终端Logger.

Returns:

  • (Logger)

    终端Logger


13
14
15
# File 'lib/Bot/Utils.rb', line 13

def stdLogger
  @stdLogger
end

Class Method Details

.cq_parse(cqmsg) ⇒ Hash

CQ码解析

Parameters:

  • cqmsg (String)

Returns:

  • (Hash)

90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/Bot/Utils.rb', line 90

def cq_parse(cqmsg)
  cqary = []
  cqmsg.scan(/\[CQ:(.*?),(.*?)\]/m).each do |matches|
    cqcode = { type: matches[0], data: {} }
    matches[1].split(',').each do |arg|
      args = arg.split('=')
      cqcode[:data][args[0].to_sym] = args[1]
    end
    cqary << cqcode
  end
  cqary
end

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

post发包

Parameters:

  • url (URI)
  • ret (String)

Returns:

  • (String)

48
49
50
51
52
53
54
55
# File 'lib/Bot/Utils.rb', line 48

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

.initLogger(loggerFile = nil) ⇒ Object

初始化日志

Parameters:

  • loggerFile (String) (defaults to: nil)

19
20
21
22
23
# File 'lib/Bot/Utils.rb', line 19

def initLogger(loggerFile=nil)
  @loggerFile = loggerFile
  @stdLogger = setLogger(Logger.new(STDOUT))
  @fileLogger = setLogger(Logger.new(@loggerFile, 'daily')) if @loggerFile
end

.log(str, severity = Logger::INFO, app = "RUBY-CQHTTP") ⇒ Object

输出日志

Parameters:

  • str (String)
  • severity (Logger::INFO, Logger::DEBUG, Logger::WARN, Logger::ERROR) (defaults to: Logger::INFO)
  • app (String) (defaults to: "RUBY-CQHTTP")

38
39
40
41
# File 'lib/Bot/Utils.rb', line 38

def log(str, severity=Logger::INFO, app="RUBY-CQHTTP")
  @stdLogger.log(severity, str, app)
  @fileLogger.log(severity, str, app) if @loggerFile
end

.msg_change(msg) ⇒ String

消息转义

&amp; -> &

&#91; -> [

&#93; -> ]

Parameters:

  • msg (String)

Returns:

  • (String)

65
66
67
68
69
70
# File 'lib/Bot/Utils.rb', line 65

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

.msg_change!(msg) ⇒ String

消息反转义

& -> &amp;

[ -> &#91;

] -> &#93;

Parameters:

  • msg (String)

Returns:

  • (String)

79
80
81
82
83
84
# File 'lib/Bot/Utils.rb', line 79

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

.setLoggerLevel(loggerLevel) ⇒ Object

设置日志等级

Parameters:

  • loggerLevel (String)

28
29
30
31
# File 'lib/Bot/Utils.rb', line 28

def setLoggerLevel(loggerLevel)
  @stdLogger.level = loggerLevel
  @fileLogger.level = loggerLevel if @loggerFile
end