Module: Discordrb::API::User

Defined in:
lib/discordrb/api/user.rb

Overview

API calls for User object

Class Method Summary collapse

Class Method Details

.avatar_url(user_id, avatar_id, format = nil) ⇒ Object

Make an avatar URL from the user and avatar IDs



140
141
142
143
144
145
146
147
# File 'lib/discordrb/api/user.rb', line 140

def avatar_url(user_id, avatar_id, format = nil)
  format ||= if avatar_id.start_with?('a_')
               'gif'
             else
               'webp'
             end
  "#{Discordrb::API.cdn_url}/avatars/#{user_id}/#{avatar_id}.#{format}"
end

Make a banner URL from the user and banner IDs



150
151
152
153
154
155
156
157
# File 'lib/discordrb/api/user.rb', line 150

def banner_url(user_id, banner_id, format = nil)
  format ||= if banner_id.start_with?('a_')
               'gif'
             else
               'png'
             end
  "#{Discordrb::API.cdn_url}/banners/#{user_id}/#{banner_id}.#{format}"
end

.change_own_nickname(token, server_id, nick, reason = nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/discordrb/api/user.rb', line 33

def change_own_nickname(token, server_id, nick, reason = nil)
  Discordrb::API.request(
    :guilds_sid_members_me_nick,
    server_id, # This is technically a guild endpoint
    :patch,
    "#{Discordrb::API.api_base}/guilds/#{server_id}/members/@me/nick",
    { nick: nick }.to_json,
    Authorization: token,
    content_type: :json,
    'X-Audit-Log-Reason': reason
  )
end

.connections(token) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/discordrb/api/user.rb', line 118

def connections(token)
  Discordrb::API.request(
    :users_me_connections,
    nil,
    :get,
    "#{Discordrb::API.api_base}/users/@me/connections",
    Authorization: token
  )
end

.create_pm(token, recipient_id) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/discordrb/api/user.rb', line 104

def create_pm(token, recipient_id)
  Discordrb::API.request(
    :users_me_channels,
    nil,
    :post,
    "#{Discordrb::API.api_base}/users/@me/channels",
    { recipient_id: recipient_id }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.default_avatar(discrim_id = 0, legacy: false) ⇒ Object

Returns one of the "default" discord avatars from the CDN given a discriminator or id since new usernames TODO: Maybe change this method again after discriminator removal ?



130
131
132
133
134
135
136
137
# File 'lib/discordrb/api/user.rb', line 130

def default_avatar(discrim_id = 0, legacy: false)
  index = if legacy
            discrim_id.to_i % 5
          else
            (discrim_id.to_i >> 22) % 5
          end
  "#{Discordrb::API.cdn_url}/embed/avatars/#{index}.png"
end

.leave_server(token, server_id) ⇒ Object



80
81
82
83
84
85
86
87
88
# File 'lib/discordrb/api/user.rb', line 80

def leave_server(token, server_id)
  Discordrb::API.request(
    :users_me_guilds_sid,
    nil,
    :delete,
    "#{Discordrb::API.api_base}/users/@me/guilds/#{server_id}",
    Authorization: token
  )
end

.profile(token) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/discordrb/api/user.rb', line 21

def profile(token)
  Discordrb::API.request(
    :users_me,
    nil,
    :get,
    "#{Discordrb::API.api_base}/users/@me",
    Authorization: token
  )
end

.resolve(token, user_id) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/discordrb/api/user.rb', line 9

def resolve(token, user_id)
  Discordrb::API.request(
    :users_uid,
    nil,
    :get,
    "#{Discordrb::API.api_base}/users/#{user_id}",
    Authorization: token
  )
end

.servers(token) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/discordrb/api/user.rb', line 68

def servers(token)
  Discordrb::API.request(
    :users_me_guilds,
    nil,
    :get,
    "#{Discordrb::API.api_base}/users/@me/guilds",
    Authorization: token
  )
end

.update_current_user(token, username = :undef, avatar = :undef, banner = :undef) ⇒ Object

Update the properties of the user for the current bot. https://discord.com/developers/docs/resources/user#modify-current-user



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/discordrb/api/user.rb', line 54

def update_current_user(token, username = :undef, avatar = :undef, banner = :undef)
  Discordrb::API.request(
    :users_me,
    nil,
    :patch,
    "#{Discordrb::API.api_base}/users/@me",
    { username: username, avatar: avatar, banner: banner }.reject { |_, value| value == :undef }.to_json,
    Authorization: token,
    content_type: :json
  )
end

.update_profile(token, _email, _password, new_username, avatar, _new_password = nil) ⇒ Object



48
49
50
# File 'lib/discordrb/api/user.rb', line 48

def update_profile(token, , _password, new_username, avatar, _new_password = nil)
  update_current_user(token, new_username, avatar)
end

.user_dms(token) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/discordrb/api/user.rb', line 92

def user_dms(token)
  Discordrb::API.request(
    :users_me_channels,
    nil,
    :get,
    "#{Discordrb::API.api_base}/users/@me/channels",
    Authorization: token
  )
end