Skip to content

Commit

Permalink
feature: update types + make these classes functional
Browse files Browse the repository at this point in the history
  • Loading branch information
4x8Matrix committed Jun 3, 2024
1 parent 914ba4f commit 381739f
Show file tree
Hide file tree
Showing 21 changed files with 127 additions and 71 deletions.
33 changes: 6 additions & 27 deletions Examples/Development.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,16 @@ local Env = require("../.env")
local DiscordSettings = DiscordLuau.SettingsBuilder.new(Env.DISCORD_BOT_TOKEN)
local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)

DiscordClient:setVerbose(true)
-- DiscordClient:setVerbose(true)

DiscordClient.eventManager.onReady:connect(function()
print(`🎉🎉 {DiscordClient.discordUser.username} is online! 🎉🎉`)

local permissions = DiscordLuau.PermissionsBuilder.new()

permissions:addPermission(DiscordLuau.PermissionsBuilder.Permissions.SendMessages)

local slashCommand = DiscordLuau.CommandBuilder
.new()
:setName("work-please")
:setDescription("Example Description")
:setGuildPermissions(permissions)
:addContext(DiscordLuau.CommandBuilder.Context.Guild)
:addContext(DiscordLuau.CommandBuilder.Context.PrivateChannel)
:addContext(DiscordLuau.CommandBuilder.Context.BotDM)
:addIntegration(DiscordLuau.CommandBuilder.IntegrationType.GuildCommand)
:addIntegration(DiscordLuau.CommandBuilder.IntegrationType.UserCommand)

DiscordClient.discordApplication
:setSlashCommandsAsync({
slashCommand,
})
:after(function(data)
print("updated, fetching current commands..")

DiscordClient.discordApplication:fetchSlashCommandsAsync():after(function(...)
print(...)
end)
end)
print(DiscordClient:fetchChannelAsync("1048686561685946489"):await())
print(DiscordClient:fetchChannelAsync("1239935584907169812"):await())
print(DiscordClient:fetchChannelAsync("1239935584907169813"):await())
print(DiscordClient:fetchChannelAsync("1246395956296155197"):await())
print(DiscordClient:fetchChannelAsync("1134076704495775844"):await())
end)

DiscordClient:connectAsync()
15 changes: 12 additions & 3 deletions Package/Classes/Objects/BaseDiscordChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,26 @@ function BaseDiscordChannel.Interface.new(
discordClient,
channelData: {
id: string,

[any]: any,
}
)
return Construct({
): BaseDiscordChannel
local self = Construct({
discordClient = discordClient,
}, BaseDiscordChannel.Prototype)
}, BaseDiscordChannel.Prototype) :: any

for key, value in channelData do
self[key] = value
end

return self
end

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

id: string,
type: number,
}

return BaseDiscordChannel.Interface
34 changes: 15 additions & 19 deletions Package/Classes/Objects/DiscordChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -39,42 +39,38 @@ function DiscordChannel.Interface.from(
id: string,
type: number,

[string]: any,
[any]: any,
}
)
local baseChannelObject: DiscordChannel

): DiscordChannel
if rawChannelData.type == ChannelType.GuildTextChannel then
baseChannelObject = GuildTextChannel.new(discordClient, rawChannelData)
return GuildTextChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.UserDMChannel then
baseChannelObject = UserDMChannel.new(discordClient, rawChannelData)
return UserDMChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildVoiceChannel then
baseChannelObject = GuildVoiceChannel.new(discordClient, rawChannelData)
return GuildVoiceChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.UserGroupChannel then
baseChannelObject = UserGroupChannel.new(discordClient, rawChannelData)
return UserGroupChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildCategory then
baseChannelObject = GuildCategoryChannel.new(discordClient, rawChannelData)
return GuildCategoryChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildAnnouncement then
baseChannelObject = GuildAnnouncementChannel.new(discordClient, rawChannelData)
return GuildAnnouncementChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildAnnouncementThread then
baseChannelObject = GuildAnnouncementThreadChannel.new(discordClient, rawChannelData)
return GuildAnnouncementThreadChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildPublicThread then
baseChannelObject = GuildPublicThreadChannel.new(discordClient, rawChannelData)
return GuildPublicThreadChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildPrivateThread then
baseChannelObject = GuildPrivateThreadChannel.new(discordClient, rawChannelData)
return GuildPrivateThreadChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildStageVoiceChannel then
baseChannelObject = GuildStageVoiceChannel.new(discordClient, rawChannelData)
return GuildStageVoiceChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildDirectory then
baseChannelObject = GuildDirectoryChannel.new(discordClient, rawChannelData)
return GuildDirectoryChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildForumChannel then
baseChannelObject = GuildForumChannel.new(discordClient, rawChannelData)
return GuildForumChannel.new(discordClient, rawChannelData)
elseif rawChannelData.type == ChannelType.GuildMediaChannel then
baseChannelObject = GuildMediaChannel.new(discordClient, rawChannelData)
return GuildMediaChannel.new(discordClient, rawChannelData)
else
error(`Unknown discord channel type '{rawChannelData.type}'`)
end

return baseChannelObject
end

export type DiscordChannel =
Expand Down
39 changes: 39 additions & 0 deletions Package/Classes/Objects/DiscordPermission.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local Construct = require("../../Utils/Construct")
local Enumerate = require("../../Utils/Enumerate")

local PermissionsBuilder = require("../Builders/PermissionsBuilder")

--[=[
@class Builders.DiscordPermission
]=]
local DiscordPermission = {}

DiscordPermission.Interface = {}
DiscordPermission.Prototype = {}

DiscordPermission.Prototype.type = "DiscordPermission"

function DiscordPermission.Prototype.hasPermission(self: DiscordPermission, permission: number)
return table.find(self.permissions, permission) ~= nil
end

function DiscordPermission.Interface.from(bitfield: string)
local permissions = Construct({ permissions = {} }, DiscordPermission.Prototype)
local numberBitfield = tonumber(bitfield)

assert(numberBitfield, `Invalid BitField '{bitfield}'`)

for key, value in PermissionsBuilder.Permissions :: { number } do
if bit32.band(numberBitfield, value) == value then
table.insert(permissions.permissions, value)
end
end

return permissions
end

export type DiscordPermission = typeof(DiscordPermission.Prototype) & {
permissions: { number },
}

return DiscordPermission.Interface
2 changes: 1 addition & 1 deletion Package/Classes/Objects/GuildAnnouncementChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function GuildAnnouncementChannel.Interface.new(
return Construct(channelStruct, GuildAnnouncementChannel.Prototype)
end)

return Extend(self, BaseGuildTextChannel.new(discordClient, channelData))
return (Extend(self, BaseGuildTextChannel.new(discordClient, channelData)) :: unknown) :: GuildAnnouncementChannel
end

export type GuildAnnouncementChannel =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function GuildAnnouncementThreadChannel.Interface.new(
return Construct(channelStruct, GuildAnnouncementThreadChannel.Prototype)
end)

return Extend(self, BaseGuildThread.new(discordClient, channelData))
return (Extend(self, BaseGuildThread.new(discordClient, channelData)) :: unknown) :: GuildAnnouncementThreadChannel
end

export type GuildAnnouncementThreadChannel =
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Objects/GuildCategoryChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function GuildCategoryChannel.Interface.new(
discordClient = discordClient,
}, GuildCategoryChannel.Prototype)

return Extend(channelObject, BaseDiscordChannel.new(discordClient, channelData))
return (Extend(channelObject, BaseDiscordChannel.new(discordClient, channelData)) :: unknown) :: GuildCategoryChannel
end

export type GuildCategoryChannel = BaseDiscordChannel.BaseDiscordChannel & typeof(GuildCategoryChannel.Prototype) & {
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Objects/GuildDirectoryChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function GuildDirectoryChannel.Interface.new(
return Construct(channelStruct, GuildDirectoryChannel.Prototype)
end)

return Extend(self, BaseGuildTextChannel.new(discordClient, channelData))
return (Extend(self, BaseGuildTextChannel.new(discordClient, channelData)) :: unknown) :: GuildDirectoryChannel
end

export type GuildDirectoryChannel = BaseGuildTextChannel.BaseGuildTextChannel & typeof(GuildDirectoryChannel.Prototype) & {
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Objects/GuildForumChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function GuildForumChannel.Interface.new(
discordClient = discordClient,
}, GuildForumChannel.Prototype)

return Extend(channelObject, BaseDiscordChannel.new(discordClient, channelData))
return (Extend(channelObject, BaseDiscordChannel.new(discordClient, channelData)) :: unknown) :: GuildForumChannel
end

export type GuildForumChannel = BaseDiscordChannel.BaseDiscordChannel & typeof(GuildForumChannel.Prototype) & {
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Objects/GuildMediaChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function GuildMediaChannel.Interface.new(
discordClient = discordClient,
}, GuildMediaChannel.Prototype)

return Extend(channelObject, BaseDiscordChannel.new(discordClient, channelData))
return (Extend(channelObject, BaseDiscordChannel.new(discordClient, channelData)) :: unknown) :: GuildMediaChannel
end

export type GuildMediaChannel = BaseDiscordChannel.BaseDiscordChannel & typeof(GuildMediaChannel.Prototype) & {
Expand Down
4 changes: 3 additions & 1 deletion Package/Classes/Objects/GuildPrivateThreadChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function GuildPrivateThreadChannel.Interface.new(
discordClient = discordClient,
}, GuildPrivateThreadChannel.Prototype)

return Extend(channelObject, BaseGuildThread.new(discordClient, channelData))
return (
Extend(channelObject, BaseGuildThread.new(discordClient, channelData)) :: unknown
) :: GuildPrivateThreadChannel
end

export type GuildPrivateThreadChannel = BaseGuildThread.BaseGuildThread & typeof(GuildPrivateThreadChannel.Prototype) & {
Expand Down
4 changes: 3 additions & 1 deletion Package/Classes/Objects/GuildPublicThreadChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function GuildPublicThreadChannel.Interface.new(
discordClient = discordClient,
}, GuildPublicThreadChannel.Prototype)

return Extend(channelObject, BaseGuildThread.new(discordClient, channelData))
return (
Extend(channelObject, BaseGuildThread.new(discordClient, channelData)) :: unknown
) :: GuildPublicThreadChannel
end

export type GuildPublicThreadChannel = BaseGuildThread.BaseGuildThread & typeof(GuildPublicThreadChannel.Prototype) & {
Expand Down
4 changes: 3 additions & 1 deletion Package/Classes/Objects/GuildStageVoiceChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ function GuildStageVoiceChannel.Interface.new(
discordClient = discordClient,
}, GuildStageVoiceChannel.Prototype)

return Extend(channelObject, BaseGuildVoiceChannel.new(discordClient, channelData))
return (
Extend(channelObject, BaseGuildVoiceChannel.new(discordClient, channelData)) :: unknown
) :: GuildStageVoiceChannel
end

export type GuildStageVoiceChannel =
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Objects/GuildTextChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function GuildTextChannel.Interface.new(
discordClient = discordClient,
}, GuildTextChannel.Prototype)

return Extend(channelObject, BaseGuildTextChannel.new(discordClient, channelData))
return (Extend(channelObject, BaseGuildTextChannel.new(discordClient, channelData)) :: unknown) :: GuildTextChannel
end

export type GuildTextChannel = BaseGuildTextChannel.BaseGuildTextChannel & typeof(GuildTextChannel.Prototype) & {
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Objects/GuildVoiceChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function GuildVoiceChannel.Interface.new(
discordClient = discordClient,
}, GuildVoiceChannel.Prototype)

return Extend(channelObject, BaseGuildVoiceChannel.new(discordClient, channelData))
return (Extend(channelObject, BaseGuildVoiceChannel.new(discordClient, channelData)) :: unknown) :: GuildVoiceChannel
end

export type GuildVoiceChannel = BaseGuildVoiceChannel.BaseGuildVoiceChannel & typeof(GuildVoiceChannel.Prototype) & {
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Objects/UserDMChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function UserDMChannel.Interface.new(
discordClient = discordClient,
}, UserDMChannel.Prototype)

return Extend(channelObject, BaseUserChannel.new(discordClient, channelData))
return (Extend(channelObject, BaseUserChannel.new(discordClient, channelData)) :: unknown) :: UserDMChannel
end

export type UserDMChannel = BaseUserChannel.BaseUserChannel & typeof(UserDMChannel.Prototype) & {
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Objects/UserGroupChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function UserGroupChannel.Interface.new(
discordClient = discordClient,
}, UserGroupChannel.Prototype)

return Extend(channelObject, BaseUserChannel.new(discordClient, channelData))
return (Extend(channelObject, BaseUserChannel.new(discordClient, channelData)) :: unknown) :: UserGroupChannel
end

export type UserGroupChannel = BaseUserChannel.BaseUserChannel & typeof(UserGroupChannel.Prototype) & {
Expand Down
4 changes: 2 additions & 2 deletions Package/Utils/Extend.luau
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ return function<S, M>(class0: S, class1: M): S & M & { super: M }
end,

__mode = "",
__type = class.type or "GenericClass",
__type = `{class0.type} ({class1.type})`,

-- __eq = ...
-- __gc = ...

__tostring = function()
return `DiscordLuau<'{class.type or "GenericClass"}'>`
return `DiscordLuau<'{`{class0.type} ({class1.type})` or "GenericClass"}'>`
end,
})

Expand Down
26 changes: 22 additions & 4 deletions Package/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ local DiscordGuildRole = require("Classes/Objects/DiscordGuildRole")
local DiscordInteraction = require("Classes/Objects/DiscordInteraction")
local DiscordInvite = require("Classes/Objects/DiscordInvite")
local DiscordMessage = require("Classes/Objects/DiscordMessage")
local DiscordTextChannel = require("Classes/Objects/DiscordTextChannel")
local DiscordVoiceChannel = require("Classes/Objects/DiscordVoiceChannel")
local GuildAnnouncementChannel = require("Classes/Objects/GuildAnnouncementChannel")
local GuildAnnouncementThreadChannel = require("Classes/Objects/GuildAnnouncementThreadChannel")
local GuildCategoryChannel = require("Classes/Objects/GuildCategoryChannel")
local GuildDirectoryChannel = require("Classes/Objects/GuildDirectoryChannel")
local GuildForumChannel = require("Classes/Objects/GuildForumChannel")
local GuildMediaChannel = require("Classes/Objects/GuildMediaChannel")
local GuildPrivateThreadChannel = require("Classes/Objects/GuildPrivateThreadChannel")
local GuildPublicThreadChannel = require("Classes/Objects/GuildPublicThreadChannel")
local GuildStageVoiceChannel = require("Classes/Objects/GuildStageVoiceChannel")
local UserDMChannel = require("Classes/Objects/UserDMChannel")
local UserGroupChannel = require("Classes/Objects/UserGroupChannel")
local DiscordUser = require("Classes/Objects/DiscordUser")

-- Components
Expand Down Expand Up @@ -194,9 +203,18 @@ export type DiscordGuildRole = DiscordGuildRole.DiscordGuildRole
export type DiscordInteraction = DiscordInteraction.DiscordInteraction
export type DiscordInvite = DiscordInvite.DiscordInvite
export type DiscordMessage = DiscordMessage.DiscordMessage
export type DiscordTextChannel = DiscordTextChannel.DiscordTextChannel
export type DiscordVoiceChannel = DiscordVoiceChannel.DiscordVoiceChannel
export type DiscordUser = DiscordUser.DiscordUser
export type GuildAnnouncementChannel = GuildAnnouncementChannel.GuildAnnouncementChannel
export type GuildAnnouncementThreadChannel = GuildAnnouncementThreadChannel.GuildAnnouncementThreadChannel
export type GuildCategoryChannel = GuildCategoryChannel.GuildCategoryChannel
export type GuildDirectoryChannel = GuildDirectoryChannel.GuildDirectoryChannel
export type GuildForumChannel = GuildForumChannel.GuildForumChannel
export type GuildMediaChannel = GuildMediaChannel.GuildMediaChannel
export type GuildPrivateThreadChannel = GuildPrivateThreadChannel.GuildPrivateThreadChannel
export type GuildPublicThreadChannel = GuildPublicThreadChannel.GuildPublicThreadChannel
export type GuildStageVoiceChannel = GuildStageVoiceChannel.GuildStageVoiceChannel
export type UserDMChannel = UserDMChannel.UserDMChannel
export type UserGroupChannel = UserGroupChannel.UserGroupChannel
--#endregion

return DiscordLuau
2 changes: 1 addition & 1 deletion aftman.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# To add a new tool, add an entry to this table.
[tools]
lune = "filiptibell/[email protected].5"
lune = "filiptibell/[email protected].0"
stylua = "JohnnyMorganz/[email protected]"
selene = "Kampfkarren/[email protected]"
moonwave = "evaera/[email protected]"
13 changes: 11 additions & 2 deletions init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,17 @@ export type DiscordGuildRole = DiscordLuau.DiscordGuildRole
export type DiscordInteraction = DiscordLuau.DiscordInteraction
export type DiscordInvite = DiscordLuau.DiscordInvite
export type DiscordMessage = DiscordLuau.DiscordMessage
export type DiscordTextChannel = DiscordLuau.DiscordTextChannel
export type DiscordVoiceChannel = DiscordLuau.DiscordVoiceChannel
export type GuildAnnouncementChannel = DiscordLuau.GuildAnnouncementChannel
export type GuildAnnouncementThreadChannel = DiscordLuau.GuildAnnouncementThreadChannel
export type GuildCategoryChannel = DiscordLuau.GuildCategoryChannel
export type GuildDirectoryChannel = DiscordLuau.GuildDirectoryChannel
export type GuildForumChannel = DiscordLuau.GuildForumChannel
export type GuildMediaChannel = DiscordLuau.GuildMediaChannel
export type GuildPrivateThreadChannel = DiscordLuau.GuildPrivateThreadChannel
export type GuildPublicThreadChannel = DiscordLuau.GuildPublicThreadChannel
export type GuildStageVoiceChannel = DiscordLuau.GuildStageVoiceChannel
export type UserDMChannel = DiscordLuau.UserDMChannel
export type UserGroupChannel = DiscordLuau.UserGroupChannel
export type DiscordUser = DiscordLuau.DiscordUser

return DiscordLuau

0 comments on commit 381739f

Please sign in to comment.