Skip to content

Commit

Permalink
feature: refactor event system to support luau types!
Browse files Browse the repository at this point in the history
  • Loading branch information
4x8Matrix committed May 18, 2024
1 parent 9d7e4fb commit 4b86ad2
Show file tree
Hide file tree
Showing 20 changed files with 249 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Examples/Commands.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)

DiscordClient:setVerbose(true)

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

local permissions = DiscordLuau.DiscordPermissions.new()
Expand Down
2 changes: 1 addition & 1 deletion Examples/Components.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local Env = require("../.env")
local DiscordSettings = DiscordLuau.DiscordSettings.new(Env.DISCORD_BOT_TOKEN)
local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)

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

local discordChannel = DiscordClient:fetchChannelAsync("1048686561685946489"):await()
Expand Down
8 changes: 4 additions & 4 deletions Examples/Development.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)

DiscordClient:setVerbose(true)

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

local guild = DiscordClient:fetchGuildAsync("737382889947136000"):await()
Expand Down Expand Up @@ -38,9 +38,9 @@ DiscordClient:on("Ready", function()
slashCommand0, slashCommand1
}):after(function(data)
print("updated, fetching current commands..")
-- DiscordClient.discordApplication:fetchSlashCommandsAsync():after(function(...)
-- print(...)
-- end)
DiscordClient.discordApplication:fetchSlashCommandsAsync():after(function(...)
print(...)
end)
end)
end)

Expand Down
4 changes: 2 additions & 2 deletions Examples/Message.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local Env = require("../.env")
local DiscordSettings = DiscordLuau.DiscordSettings.new(Env.DISCORD_BOT_TOKEN)
local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)

DiscordClient:on("Message", function(message: DiscordLuau.DiscordMessage)
DiscordClient.eventManager.onMessage:connect(function(message)
print(`DiscordUser '{message.author.username}' has said; {message.content}`)

if string.find(string.lower(message.content), "hello") then
Expand All @@ -18,7 +18,7 @@ DiscordClient:on("Message", function(message: DiscordLuau.DiscordMessage)
end
end)

DiscordClient:on("Ready", function()
DiscordClient.eventManager.onReady:connect(function()
print(`🎉🎉 {DiscordClient.discordUser.username} is online! 🎉🎉`)
end)

Expand Down
8 changes: 4 additions & 4 deletions Examples/Modal.luau
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local Env = require("../.env")
local DiscordSettings = DiscordLuau.DiscordSettings.new(Env.DISCORD_BOT_TOKEN)
local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)

DiscordClient:on("Interaction", function(interaction: DiscordLuau.DiscordInteraction)
DiscordClient.eventManager.onInteraction:connect(function(interaction)
if interaction.data.name == "example-command" then
local discordModal = DiscordLuau.DiscordModal.new("response-modal")

Expand Down Expand Up @@ -63,17 +63,17 @@ DiscordClient:on("Interaction", function(interaction: DiscordLuau.DiscordInterac
end
end)

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

local permissions = DiscordLuau.DiscordPermissions.new()

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

local slashCommand = DiscordLuau.ApplicationCommand.new()
:setName("example-command")
:setDescription("Example Description")
:SetGuildPermissions(permissions)
:setGuildPermissions(permissions)

DiscordClient.discordApplication:setSlashCommandsAsync({
slashCommand
Expand Down
2 changes: 1 addition & 1 deletion Examples/Precence.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local Env = require("../.env")
local DiscordSettings = DiscordLuau.DiscordSettings.new(Env.DISCORD_BOT_TOKEN)
local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)

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

local discordPresence = DiscordLuau.DiscordPresence.new()
Expand Down
6 changes: 3 additions & 3 deletions Package/Classes/ApplicationCommand.luau
Original file line number Diff line number Diff line change
Expand Up @@ -300,12 +300,12 @@ end
```
]=]
function ApplicationCommand.Interface.new()
return (Construct({
function ApplicationCommand.Interface.new(): ApplicationCommand
return Construct({
choices = {},
options = {},
contexts = {}
}, ApplicationCommand.Prototype) :: unknown) :: ApplicationCommand
}, ApplicationCommand.Prototype) :: any
end

export type ApplicationCommand = typeof(ApplicationCommand.Prototype) & {
Expand Down
4 changes: 2 additions & 2 deletions Package/Classes/ApplicationCommandOption.luau
Original file line number Diff line number Diff line change
Expand Up @@ -382,11 +382,11 @@ end
```
]=]
function ApplicationCommandOptions.Interface.new()
function ApplicationCommandOptions.Interface.new(): ApplicationCommandOptions
return Construct({
choices = {},
options = {}
}, ApplicationCommandOptions.Prototype)
}, ApplicationCommandOptions.Prototype) :: any
end

export type ApplicationCommandOptions = typeof(ApplicationCommandOptions.Prototype) & {
Expand Down
66 changes: 45 additions & 21 deletions Package/Classes/DiscordClient.luau
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ local DiscordVoiceChannel = require("Internal/DiscordVoiceChannel")
local DiscordMessage = require("Internal/DiscordMessage")
local DiscordApplication = require("Internal/DiscordApplication")
local DiscordInteraction = require("Internal/DiscordInteraction")
local EventManager = require("Internal/EventManager")

local Console = require("../Vendor/Console")
local Future = require("../Vendor/Future")
Expand Down Expand Up @@ -164,7 +165,7 @@ function DiscordClient.Prototype.connectAsync(self: DiscordClient)
elseif eventName == WebsocketEvents.GuildMemberUnbanned then
data.user = DiscordUser.new(self, data.user)

self.onEvent:fire(DiscordEvents.GuildMemberBanned, data)
self.onEvent:fire(DiscordEvents.GuildMemberUnbanned, data)
elseif eventName == WebsocketEvents.GuildMemberJoined then
data.user = DiscordUser.new(self, data.user)

Expand Down Expand Up @@ -234,22 +235,6 @@ function DiscordClient.Prototype.updatePresenceAsync(self: DiscordClient, discor
end)
end

--[=[
@method on
@within DiscordClient
@return Connection
]=]
function DiscordClient.Prototype.on(self: DiscordClient, targetEventName: string, eventCallback: (...any) -> ())
return self.onEvent:connect(function(eventName: string, ...)
if eventName ~= targetEventName then
return
end

eventCallback(...)
end)
end

--[=[
@method setVerbose
@within DiscordClient
Expand All @@ -274,8 +259,8 @@ end
end)
```
]=]
function DiscordClient.Interface.new(discordSettings: DiscordSettings.DiscordSettings)
local self = (Construct({
function DiscordClient.Interface.new(discordSettings: DiscordSettings.DiscordSettings): DiscordClient
local self = Construct({
--[=[
@prop discordSettings DiscordSettings
@within DiscordClient
Expand Down Expand Up @@ -323,13 +308,20 @@ function DiscordClient.Interface.new(discordSettings: DiscordSettings.DiscordSet
@within DiscordClient
]=]
onEvent = Signal.new(),
}, DiscordClient.Prototype) :: unknown) :: DiscordClient

}, DiscordClient.Prototype) :: any

--[=[
@prop discordGateway DiscordGateway
@within DiscordClient
]=]
self.discordGateway = DiscordGateway.new(self)
self.discordGateway = DiscordGateway.new(self :: any)

--[=[
@prop eventManager EventManager
@within DiscordClient
]=]
self.eventManager = EventManager.new(self :: any)

return self
end
Expand All @@ -341,6 +333,8 @@ export type DiscordClient = typeof(DiscordClient.Prototype) & {
discordSettings: DiscordSettings.DiscordSettings,
discordCache: DiscordCache.DiscordCache,

eventManager: EventManager.EventManager,

websocketUrl: string,
shardCount: number,
maxConcurrency: number,
Expand All @@ -353,6 +347,36 @@ export type DiscordClient = typeof(DiscordClient.Prototype) & {

discordUser: DiscordUser.DiscordUser,
discordApplication: DiscordApplication.DiscordApplication,

events: {
Ready: Signal.Signal<nil>,

Message: Signal.Signal<DiscordMessage.DiscordMessage>,
MessageChanged: Signal.Signal<nil>,
MessageDeleted: Signal.Signal<nil>,
MessageBulkDeleted: Signal.Signal<nil>,

ChannelCreate: Signal.Signal<nil>,
ChannelUpdate: Signal.Signal<nil>,
ChannelDelete: Signal.Signal<nil>,

UserUpdated: Signal.Signal<nil>,

ChannelPinsUpdate: Signal.Signal<nil>,

GuildCreate: Signal.Signal<nil>,
GuildUpdate: Signal.Signal<nil>,
GuildDelete: Signal.Signal<nil>,

GuildMemberBanned: Signal.Signal<nil>,
GuildMemberUnbanned: Signal.Signal<nil>,

GuildMemberJoined: Signal.Signal<nil>,
GuildMemberLeft: Signal.Signal<nil>,
GuildMemberUpdated: Signal.Signal<nil>,

Interaction: Signal.Signal<nil>,
}
}

return DiscordClient.Interface
4 changes: 2 additions & 2 deletions Package/Classes/DiscordModal.luau
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ end
```
]=]
function DiscordModal.Interface.new(modalId: string)
function DiscordModal.Interface.new(modalId: string): DiscordModal
return Construct({
components = {},
modalId = modalId
}, DiscordModal.Prototype)
}, DiscordModal.Prototype) :: any
end

export type DiscordModal = typeof(DiscordModal.Prototype) & {
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/DiscordPresence.luau
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ end
```
]=]
function DiscordPresence.Interface.new()
function DiscordPresence.Interface.new(): DiscordPresence
return Construct({
activities = {}
}, DiscordPresence.Prototype)
Expand Down
2 changes: 1 addition & 1 deletion Package/Classes/Interface/ActionRowComponent.luau
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function ActionRowComponent.Prototype.toJSONObject(self: ActionRowComponent)
}
end

function ActionRowComponent.Interface.new()
function ActionRowComponent.Interface.new(): ActionRowComponent
return Construct({
components = { }
}, ActionRowComponent.Prototype)
Expand Down
4 changes: 2 additions & 2 deletions Package/Classes/Interface/ButtonComponent.luau
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ function ButtonComponent.Prototype.toJSONObject(self: ButtonComponent)
}
end

function ButtonComponent.Interface.new(buttonId: string)
function ButtonComponent.Interface.new(buttonId: string): ButtonComponent
return Construct({
buttonId = buttonId
}, ButtonComponent.Prototype)
}, ButtonComponent.Prototype) :: any
end

export type ButtonComponent = typeof(ButtonComponent.Prototype) & {
Expand Down
4 changes: 2 additions & 2 deletions Package/Classes/Interface/SelectionComponent.luau
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ function SelectionComponent.Prototype.toJSONObject(self: SelectionComponent)
}
end

function SelectionComponent.Interface.new(selectionId: string)
function SelectionComponent.Interface.new(selectionId: string): SelectionComponent
return Construct({
selectionId = selectionId,
choices = {}
}, SelectionComponent.Prototype)
}, SelectionComponent.Prototype) :: any
end

export type SelectionComponent = typeof(SelectionComponent.Prototype) & {
Expand Down
4 changes: 2 additions & 2 deletions Package/Classes/Interface/TextInputComponent.luau
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ function TextInputComponent.Prototype.toJSONObject(self: TextInputComponent)
}
end

function TextInputComponent.Interface.new(textInputId: string)
function TextInputComponent.Interface.new(textInputId: string): TextInputComponent
return Construct({
textInputId = textInputId
}, TextInputComponent.Prototype)
}, TextInputComponent.Prototype) :: any
end

export type TextInputComponent = typeof(TextInputComponent.Prototype) & {
Expand Down
4 changes: 2 additions & 2 deletions Package/Classes/Internal/DiscordChannel.luau
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function DiscordChannel.Prototype.setPositionAsync(self: DiscordChannel, positio
end)
end

function DiscordChannel.Interface.new(discordClient: any, rawChannelData: any)
function DiscordChannel.Interface.new(discordClient: any, rawChannelData: any): DiscordChannel
-- https://discord.com/developers/docs/resources/channel#channel-object-channel-types
-- todo: support other channel types lol

Expand All @@ -128,7 +128,7 @@ function DiscordChannel.Interface.new(discordClient: any, rawChannelData: any)
baseChannelObject = rawChannelData
end

return (Inherit(DiscordChannel.Prototype, baseChannelObject) :: unknown) :: DiscordChannel
return Inherit(DiscordChannel.Prototype, baseChannelObject)
end

export type DiscordChannel = (DiscordTextChannel.DiscordTextChannel | DiscordVoiceChannel.DiscordVoiceChannel) & typeof(DiscordChannel.Prototype)
Expand Down
Loading

0 comments on commit 4b86ad2

Please sign in to comment.