Skip to content

Commit

Permalink
feature: add 'DiscordThreadChannel' file
Browse files Browse the repository at this point in the history
  • Loading branch information
4x8Matrix committed May 24, 2024
1 parent 9c334cf commit 0984d87
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Package/Classes/Objects/DiscordChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local DiscordMessageBuilder = require("../Builders/DiscordMessageBuilder")

local DiscordTextChannel = require("DiscordTextChannel")
local DiscordVoiceChannel = require("DiscordVoiceChannel")
local DiscordThreadChannel = require("DiscordThreadChannel")
local DiscordInvite = require("DiscordInvite")

--[=[
Expand Down Expand Up @@ -214,6 +215,8 @@ function DiscordChannel.Interface.new(discordClient: any, rawChannelData: any):
baseChannelObject = DiscordTextChannel.new(discordClient, rawChannelData) :: any
elseif rawChannelData.type == 2 then
baseChannelObject = DiscordVoiceChannel.new(discordClient, rawChannelData) :: any
elseif rawChannelData.type == 11 or rawChannelData.type == 12 then
baseChannelObject = DiscordThreadChannel.new(discordClient, rawChannelData) :: any
else
baseChannelObject = rawChannelData
end
Expand Down
174 changes: 174 additions & 0 deletions Package/Classes/Objects/DiscordThreadChannel.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
local Construct = require("../../Utils/Construct")

local CacheType = require("../../Enums/CacheType")
local DiscordEndpoints = require("../../Data/DiscordEndpoints")

local Future = require("../../Vendor/Future")

local DiscordMessage = require("../Objects/DiscordMessage")
local DiscordGuildMember = require("../Objects/DiscordGuildMember")

--[=[
@class Objects.DiscordThreadChannel
DiscordThreadChannel represents a thread channel in a Discord server, providing methods to modify the channel and interact with pinned messages.
]=]
local DiscordThreadChannel = {}

DiscordThreadChannel.Prototype = {}
DiscordThreadChannel.Interface = {}

DiscordThreadChannel.Prototype.type = "DiscordThreadChannel"

--[=[
Joins the current discord bot into the thread
@method joinThreadAsync
@within Objects.DiscordThreadChannel
@return Future<unknown> -- A future the bot has joined the thread channel
]=]
function DiscordThreadChannel.Prototype.joinThreadAsync(self: DiscordThreadChannel)
return Future.try(function()
return self.discordClient.discordGateway:putAsync(string.format(DiscordEndpoints.BotJoinThreadChannel, self.id), { }):await()
end)
end

--[=[
Adds the passed member to the thread channel
@method addMemberToThreadAsync
@param userId string
@within Objects.addMemberToThreadAsync
@return Future<unknown> -- A future the bot has joined the thread channel
]=]
function DiscordThreadChannel.Prototype.addMemberToThreadAsync(self: DiscordThreadChannel, userId: string)
return Future.try(function()
return self.discordClient.discordGateway:putAsync(string.format(DiscordEndpoints.BotAddMemberToThreadChannel, self.id, userId), { }):await()
end)
end

--[=[
Leaves the thread channel if the discord bot is in the channel.
@method leaveThreadAsync
@within Objects.DiscordThreadChannel
@return Future<unknown> -- A future the bot has joined the thread channel
]=]
function DiscordThreadChannel.Prototype.leaveThreadAsync(self: DiscordThreadChannel)
return Future.try(function()
return self.discordClient.discordGateway:deleteAsync(string.format(DiscordEndpoints.BotLeaveThreadChannel, self.id), { }):await()
end)
end

--[=[
Removes the passed user id from the current thread channel
@method removeMemberFromThreadAsync
@param userId string
@within Objects.DiscordThreadChannel
@return Future<unknown> -- A future the bot has joined the thread channel
]=]
function DiscordThreadChannel.Prototype.removeMemberFromThreadAsync(self: DiscordThreadChannel, userId: string)
return Future.try(function()
return self.discordClient.discordGateway:putAsync(string.format(DiscordEndpoints.BotRemoveMemberFromThreadChannel, self.id, userId), { }):await()
end)
end

--[=[
Returns a list of members that are active in the current thread channel
@method removeMemberFromThreadAsync
@param userId string
@within Objects.DiscordThreadChannel
@return Future<{ DiscordGuildMember }> -- A future the bot has joined the thread channel
]=]
function DiscordThreadChannel.Prototype.getThreadMembersAsync(self: DiscordThreadChannel)
return Future.try(function()
local memberObjects = self.discordClient.discordGateway:getAsync(string.format(DiscordEndpoints.BotGetThreadMembers, self.id), { }):await()

for key, memberObject in memberObjects do
memberObjects[key] = DiscordGuildMember.new(self.discordClient, memberObject.user.id, self.guildId, memberObject)
end

return memberObjects
end)
end

--[=[
Modifies the thread channel settings asynchronously.
@method modifyAsync
@param channelSchema DiscordTextChannelSchema -- The schema containing the modifications to be applied.
@within Objects.DiscordThreadChannel
@return Future<unknown> -- A future that resolves when the modifications are complete.
]=]
function DiscordThreadChannel.Prototype.modifyAsync(self: DiscordThreadChannel, channelSchema)
return Future.try(function()

end)
end

--[=[
Retrieves the pinned messages in the thread channel asynchronously.
@method getPinnedMessagesAsync
@within Objects.DiscordThreadChannel
@return Future<{[number]: DiscordMessage}> -- A future that resolves to an array of pinned messages.
]=]
function DiscordThreadChannel.Prototype.getPinnedMessagesAsync(self: DiscordThreadChannel)
return Future.try(function()
local messages = self.discordClient.discordGateway:getAsync(string.format(DiscordEndpoints.BotGetPinnedMessages, self.id), { }):await()

for index, messageData in messages do
messages[index] = DiscordMessage.new(self.discordClient, messageData)
end

return messages
end)
end

--[=[
Creates a new instance of DiscordThreadChannel.
@function new
@param discordClient any -- The Discord client instance.
@param channelData table -- The data for the channel to be created.
@within Objects.DiscordThreadChannel
@return DiscordThreadChannel -- A new instance of DiscordThreadChannel.
]=]
function DiscordThreadChannel.Interface.new(discordClient: any, channelData: {
id: string,
recipients: { unknown },
}): DiscordThreadChannel
local self = discordClient.discordCache:getDataOr(CacheType.DiscordThreadChannel, channelData.id, function()
return Construct({
id = channelData.id,

discordClient = discordClient
}, DiscordThreadChannel.Prototype)
end)

if channelData then
for index, value in channelData do
self[index] = value
end
end

return self
end

export type DiscordThreadChannel = typeof(DiscordThreadChannel.Prototype) & {
discordClient: any,

type: number,
nsfw: boolean,
id: string,
flags: number,
name: string,
position: number,
rateLimitPerUser: number,
lastMessageId: string,
guildId: string
}

return DiscordThreadChannel.Interface
6 changes: 6 additions & 0 deletions Package/Data/DiscordEndpoints.luau
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ return {
BotGetPinnedMessages = "channels/%s/pins",
BotEditChannelPermissions = "channels/%s",

BotGetThreadMembers = "channels/%s/thread-members",
BotJoinThreadChannel = "channels/%s/thread-members/@me",
BotAddMemberToThreadChannel = "channels/%s/thread-members/%s",
BotLeaveThreadChannel = "channels/%s/thread-members/@me",
BotRemoveMemberFromThreadChannel = "channels/%s/thread-members/%s",

BotBulkDeleteMessages = "channels/%s/messages/bulk-delete",

CreateGlobalApplicationCommand = "applications/%s/commands",
Expand Down
2 changes: 2 additions & 0 deletions Package/Enums/CacheType.luau
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local CacheType = {
DiscordMember = "DiscordMember",
DiscordInvite = "DiscordInvite",
AutoModeration = "AutoModeration",
DiscordThreadChannel = "DiscordThreadChannel"
}

export type CacheType = "Application"
Expand All @@ -28,5 +29,6 @@ export type CacheType = "Application"
| "DiscordMember"
| "DiscordInvite"
| "AutoModeration"
| "DiscordThreadChannel"

return CacheType

0 comments on commit 0984d87

Please sign in to comment.