Skip to content

Commit 6410f5a

Browse files
authored
fix user presence feature + fix some warnings (#115)
1 parent fb83ac2 commit 6410f5a

2 files changed

Lines changed: 31 additions & 12 deletions

File tree

bot.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ local client = discordia.Client({
1212
cacheAllMembers = true,
1313
-- logLevel = 4,
1414
gatewayIntents = bit.bor(
15-
-- All intents except guildIntegrations and guildPresences
15+
-- All intents except guildIntegrations
1616
enums.gatewayIntent.guilds,
1717
enums.gatewayIntent.guildMembers,
1818
enums.gatewayIntent.guildModeration,
@@ -25,7 +25,9 @@ local client = discordia.Client({
2525
enums.gatewayIntent.guildMessageTyping,
2626
enums.gatewayIntent.directMessage,
2727
enums.gatewayIntent.directMessageRections,
28-
enums.gatewayIntent.directMessageTyping
28+
enums.gatewayIntent.directMessageTyping,
29+
-- privileged intents
30+
enums.gatewayIntent.guildPresences
2931
)
3032
})
3133

module_userinfo.lua

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,24 @@
22
-- This file is part of the "Not a Bot" application
33
-- For conditions of distribution and use, see copyright notice in LICENSE
44

5-
local Date = Discordia.Date
6-
75
Module.Name = "userinfo"
86

9-
-- We have to precede special chars with an \ to prevent discord from replacing them with the corresponding emoji :<color>_circle:
10-
local discordStatus = { online = "\\🟢 Online", dnd = "\\🔴 Do Not Disturb", idle = "\\🟡 Idle", offline = "\\⚪ Offline" }
7+
8+
-- We have to precede special chars with an \ to prevent discord
9+
-- from replacing them with the corresponding emoji :<color>_circle:
10+
local discordStatus = {
11+
online = "\\🟢 Online", dnd = "\\🔴 Do Not Disturb", idle = "\\🟡 Idle", offline = "\\⚪ Offline"
12+
}
1113
local DEFAULT_COLOR = 0 -- Default color value, 0 == black
1214
local JOIN_ORDER_WINDOW = 7 -- Number of members to show in "Join order" field
15+
local Date = Discordia.Date
1316
local intents = Discordia.enums.gatewayIntent
1417
-- Privileged intent, must be checked before use
1518
local has_guild_presences_intent = (bit.band(Bot.Client:getIntents(), intents.guildPresences) ~= 0)
1619

1720
-- The highest role with color ~= black defines the color of the username
1821
local function getMemberColor(sortedRoles)
19-
for i, v in ipairs(sortedRoles) do
22+
for _, v in ipairs(sortedRoles) do
2023
if v.color ~= DEFAULT_COLOR then
2124
return v.color
2225
end
@@ -47,11 +50,22 @@ local function buildMemberEmbed(member)
4750
if has_guild_presences_intent then
4851
local presence = discordStatus[member.status]
4952
description =
50-
string.format("__`Fullname:`__ `%s`\n__`Nickname:`__ `%s`\n__`Presence:`__ %s\n__`Created at:`__ <t:%s:f>\n__`Joined at:`__ <t:%s:f>\n",
53+
string.format([[
54+
__`Fullname:`__ `%s`
55+
__`Nickname:`__ `%s`
56+
__`Presence:`__ %s
57+
__`Created at:`__ <t:%s:f>
58+
__`Joined at:`__ <t:%s:f>
59+
]],
5160
fullName, member.name, presence, createdAt, joinedAt)
5261
else
5362
description =
54-
string.format("__`Fullname:`__ `%s`\n__`Nickname:`__ `%s`\n__`Created at:`__ <t:%s:f>\n__`Joined at:`__ <t:%s:f>\n",
63+
string.format([[
64+
__`Fullname:`__ `%s`
65+
__`Nickname:`__ `%s`
66+
__`Created at:`__ <t:%s:f>
67+
__`Joined at:`__ <t:%s:f>
68+
]],
5569
fullName, member.name, createdAt, joinedAt)
5670
end
5771

@@ -62,7 +76,7 @@ local function buildMemberEmbed(member)
6276
table.sort(roles, function (a, b) return a.position > b.position end)
6377

6478
local roleNames = {}
65-
for k, v in pairs(roles) do
79+
for _, v in pairs(roles) do
6680
table.insert(roleNames, string.format("`%s`", v.name))
6781
end
6882

@@ -118,16 +132,19 @@ function Module:OnLoaded()
118132
return commandMessage:reply({ embed = buildMemberEmbed(commandMessage.member) })
119133
end
120134

135+
local targetMember, targetUser
136+
local err
121137
local guild = commandMessage.guild
122-
local targetMember, err = Bot:DecodeMember(guild, targetUserId)
138+
139+
targetMember, err = Bot:DecodeMember(guild, targetUserId)
123140

124141
if targetMember then
125142
return commandMessage:reply({ embed = buildMemberEmbed(targetMember) })
126143
elseif err == "Invalid user id" then
127144
return commandMessage:reply(err)
128145
else
129146
-- Not a member of this guild, trying to get info of the user
130-
local targetUser, err = Bot:DecodeUser(targetUserId)
147+
targetUser, err = Bot:DecodeUser(targetUserId)
131148

132149
if targetUser then
133150
return commandMessage:reply({ embed = buildUserEmbed(targetUser) })

0 commit comments

Comments
 (0)