class BotUtils

Constants

API_URI

Discord API URI

Attributes

conn[RW]
tokens[RW]

Public Class Methods

new(tokens) click to toggle source

@param tokens [Array] @return [BotUtils]

# File lib/rsu.rb, line 114
def initialize tokens
    @conn = Net::HTTP::Persistent.new

    @tokens = tokens

    @logOut, @logErr = Logger.new(STDOUT), Logger.new(STDERR)
end

Public Instance Methods

join(inviteCode) click to toggle source

Joins the server @note invite code looks something like CAf8RYd​ @param inviteCode [String]

# File lib/rsu.rb, line 91
def join inviteCode
    reqs = @tokens.map {|token|
        Net::HTTP::Post.new(
            API_URI + "invites/#{inviteCode}",
            'Authorization' => token)
    }
    # request templates

    reqs.each {|req|
        requestAndCheck req
        # join and show some info
    }
    # join the server for each bot
end
kill() click to toggle source

Closes connection

# File lib/rsu.rb, line 107
def kill
    @conn.shutdown
    @logErr.fatal 'Connection closed'
end
messageVerify(channelId, msg) click to toggle source

Sends one message @param channelId [String, Integer] @param msg [Hash] @example Use it like this:

BotUtilsInstance.messageVerify 691235051233758221, {'content'=>'Hi all!'}
# File lib/rsu.rb, line 44
def messageVerify channelId, msg
    reqs = @tokens.map {|token|
        Net::HTTP::Post.new(
            API_URI + "channels/#{channelId}/messages",
            'Content-Type'  => 'application/json',
            'Authorization' => token)
    }
    # request templates

    reqs.each {|req|
            req.body = JSON.generate msg
            # preparing the request body

            requestAndCheck req
            # send message and show some info
    }
    # send message for each bot
end
reactionVerify(channelId, messageId, emoji) click to toggle source

Adds reaction to message @note emoji takes the form of name:id for custom guild emoji, or Unicode characters @param channelId [String, Integer] @param messageId [String, Integer] @param emoji [String] @example Use it like this:

BotUtilsInstance.reactionVerify 321225024233758221, 531176006445826099, 'emoji_31:224266534235745949'
# File lib/rsu.rb, line 70
def reactionVerify channelId, messageId, emoji
    emoji = URI.encode_www_form_component emoji
    # URL encode emoji

    reqs = @tokens.map {|token|
        Net::HTTP::Put.new(
            API_URI + "channels/#{channelId}/messages/#{messageId}/reactions/#{emoji}/@me",
            'Authorization' => token)
    }
    # request templates

    reqs.each {|req|
        requestAndCheck req
        # add reaction and show some info
    }
    # add reaction for each bot
end
spam(channelId) click to toggle source

Spams in the specified channel @param channelId [String, Integer]

# File lib/rsu.rb, line 17
def spam channelId
    reqs = @tokens.map {|token|
        Net::HTTP::Post.new(
            API_URI + "channels/#{channelId}/messages",
            'Content-Type'  => 'application/json',
            'Authorization' => token)
    }
    # request templates

    loop {
        reqs.each {|req|
            req.body = JSON.generate('content'=>generate)
            # generate new message

            requestAndCheck req
            # send new message and show some info
        }
        # generate and send message for each bot
    }
    # do the same again
end

Private Instance Methods

generate() click to toggle source
# File lib/rsu.rb, line 124
def generate
    range = (' '..'~').to_a
    2000.times.map {
        range[rand(range.length)]
    }.join
    # using this because SecureRandom's random_bytes and Random's bytes not working good with JSON.generate
end
requestAndCheck(req) click to toggle source
# File lib/rsu.rb, line 132
def requestAndCheck req
    rawResp = @conn.request(API_URI, req).body
    # request

    return if rawResp.nil?
    # return back if no response

    resp = JSON.parse rawResp
    # translate raw response to json

    @logErr.error resp['message'] unless resp['message'].nil?
    # check response for errors
end