Skip to content

Commit

Permalink
feature: implement support for modifying a Discord Guild Member
Browse files Browse the repository at this point in the history
  • Loading branch information
4x8Matrix committed May 16, 2024
1 parent 92f66a4 commit 312eabe
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
80 changes: 79 additions & 1 deletion Package/Classes/Internal/DiscordGuildMember.luau
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local Datetime = require("../../Std/Datetime")

local Construct = require("../../Utils/Construct")

local DiscordEndpoints = require("../../Enums/DiscordEndpoints")
Expand Down Expand Up @@ -26,10 +28,86 @@ function DiscordGuildMember.Prototype.kickAsync(self: DiscordGuildMember, delete
}):await()
end

function DiscordGuildMember.Prototype.banAsync(self: DiscordGuildMember, deleteMessagesSeconds: number?)
function DiscordGuildMember.Prototype.banAsync(self: DiscordGuildMember)
return self.discordClient.discordGateway:deleteAsync(string.format(DiscordEndpoints.BotRemoveGuildMember, self.guildId, self.user.id)):await()
end

function DiscordGuildMember.Prototype.unbanAsync(self: DiscordGuildMember)
return self.discordClient.discordGateway:deleteAsync(string.format(DiscordEndpoints.BotRemoveGuildMemberBan, self.guildId, self.user.id)):await()
end

function DiscordGuildMember.Prototype.addRole(self: DiscordGuildMember, roleId: string)
return self.discordClient.discordGateway:putAsync(string.format(DiscordEndpoints.BotAddGuildMemberRole, self.guildId, self.user.id, roleId)):await()
end

function DiscordGuildMember.Prototype.removeRole(self: DiscordGuildMember, roleId: string)
return self.discordClient.discordGateway:deleteAsync(string.format(DiscordEndpoints.BotRemoveGuildMemberRole, self.guildId, self.user.id, roleId)):await()
end

function DiscordGuildMember.Prototype.timeoutAsync(self: DiscordGuildMember, seconds: number)
return self:modifyAsync({
timeoutUntil = seconds,
})
end

function DiscordGuildMember.Prototype.muteAsync(self: DiscordGuildMember)
return self:modifyAsync({
mute = true,
})
end

function DiscordGuildMember.Prototype.unmuteAsync(self: DiscordGuildMember)
return self:modifyAsync({
mute = false,
})
end

function DiscordGuildMember.Prototype.deafenAsync(self: DiscordGuildMember)
return self:modifyAsync({
deaf = true,
})
end

function DiscordGuildMember.Prototype.undeafenAsync(self: DiscordGuildMember)
return self:modifyAsync({
deaf = false,
})
end

function DiscordGuildMember.Prototype.setNicknameAsync(self: DiscordGuildMember, nickname: string)
return self:modifyAsync({
nickname = nickname,
})
end

function DiscordGuildMember.Prototype.modifyAsync(self: DiscordGuildMember, modificationsTable: {
nickname: string?,
roles: { string }?,
mute: boolean?,
deaf: boolean?,
channelId: string?,
timeoutUntil: number?,
flags: number?
})
local communication_disabled_until

if modificationsTable.timeoutUntil then
communication_disabled_until = Datetime.fromUnixTimestamp(
os.time() + modificationsTable.timeoutUntil
):toIsoDate()
end

return self.discordClient.discordGateway:patchAsync(string.format(DiscordEndpoints.BotModifyGuildMember, self.guildId, self.user.id), {
nick = modificationsTable.nickname,
roles = modificationsTable.roles,
mute = modificationsTable.mute,
deaf = modificationsTable.deaf,
channel_id = modificationsTable.channelId,
communication_disabled_until = communication_disabled_until,
flags = modificationsTable.flags
}):await()
end

function DiscordGuildMember.Interface.new(discordClient: any, userId: string, guildId: string, memberData: { [any]: any })
local self = discordClient.discordCache:getDataOr(CacheType.DiscordMember, `{guildId}-{userId}`, function()
return Construct({
Expand Down
4 changes: 4 additions & 0 deletions Package/Enums/DiscordEndpoints.luau
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ return {
BotGetGuildMembers = "guilds/%s/members?limit=%s&after=%s",
BotBanGuildMember = "guilds/%s/members/%s",
BotRemoveGuildMember = "guilds/%s/members/%s",
BotRemoveGuildMemberBan = "guilds/%s/bans/%s",
BotAddGuildMemberRole = "guilds/%s/members/%s/roles/%s",
BotRemoveGuildMemberRole = "guilds/%s/members/%s/roles/%s",
BotModifyGuildMember = "guilds/%s/members/%s",
BotSearchGuildMembers = "guilds/%s/members/search?query=%s&limit=%s",
BotModifyGuildChannelPosition = "channels/%s/channels",

Expand Down
3 changes: 3 additions & 0 deletions Package/Std/Datetime.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
return setmetatable({ }, {
__index = require("@lune/datetime")
})

0 comments on commit 312eabe

Please sign in to comment.