Skip to content

Commit

Permalink
feature: avoid making a PATCH request when command didn't need to be …
Browse files Browse the repository at this point in the history
…updated
  • Loading branch information
4x8Matrix committed May 15, 2024
1 parent 0ea22d7 commit de172b8
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 25 deletions.
19 changes: 15 additions & 4 deletions Examples/Commands.luau
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,31 @@ local Env = require("../.env")
local DiscordSettings = DiscordLuau.DiscordSettings.new(Env.DISCORD_BOT_TOKEN)
local DiscordClient = DiscordLuau.DiscordClient.new(DiscordSettings)

DiscordClient:setVerbose(true)

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

local permissions = DiscordLuau.DiscordPermissions.new()

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

local slashCommand = DiscordLuau.ApplicationCommand.new()
:setName("example-command")
:setDescription("Example Description")
local slashCommand0 = DiscordLuau.ApplicationCommand.new()
:setName("example-0")
:setDescription("...")
:setGuildPermissions(permissions)
:addContext(DiscordLuau.ApplicationCommand.Context.Guild)
:setType(DiscordLuau.ApplicationCommand.Type.ChatInput)

local slashCommand1 = DiscordLuau.ApplicationCommand.new()
:setName("example-1")
:setDescription("abc!")
:setGuildPermissions(permissions)
:addContext(DiscordLuau.ApplicationCommand.Context.Guild)
:setType(DiscordLuau.ApplicationCommand.Type.ChatInput)

DiscordClient.discordApplication:setSlashCommandsAsync({
slashCommand
slashCommand0, slashCommand1
}):after(function(data)
print("updated, fetching current commands..")
DiscordClient.discordApplication:fetchSlashCommandsAsync():after(function(...)
Expand Down
36 changes: 26 additions & 10 deletions Examples/Development.luau
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,32 @@ DiscordClient:setVerbose(true)
DiscordClient:on("Ready", function()
print(`🎉🎉 {DiscordClient.discordUser.username} is online! 🎉🎉`)

local channelObject = DiscordClient:fetchChannelAsync("1048686561685946489"):await()

channelObject:sendMessageAsync({
embeds = {
DiscordLuau.DiscordEmbed.new()
:setTitle("Hello, World!")
:setColor(0xFF0000)
:setDescription("A test embed object!")
}
})
local permissions = DiscordLuau.DiscordPermissions.new()

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

local slashCommand0 = DiscordLuau.ApplicationCommand.new()
:setName("example-0")
:setDescription("...")
:setGuildPermissions(permissions)
:addContext(DiscordLuau.ApplicationCommand.Context.Guild)
:setType(DiscordLuau.ApplicationCommand.Type.ChatInput)

local slashCommand1 = DiscordLuau.ApplicationCommand.new()
:setName("example-1")
:setDescription("abc!")
:setGuildPermissions(permissions)
:addContext(DiscordLuau.ApplicationCommand.Context.Guild)
:setType(DiscordLuau.ApplicationCommand.Type.ChatInput)

DiscordClient.discordApplication:setSlashCommandsAsync({
slashCommand0, slashCommand1
}):after(function(data)
print("updated, fetching current commands..")
-- DiscordClient.discordApplication:fetchSlashCommandsAsync():after(function(...)
-- print(...)
-- end)
end)
end)

DiscordClient:connectAsync()
48 changes: 38 additions & 10 deletions Package/Classes/ApplicationCommand.luau
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ ApplicationCommand.Interface.Context = Enumerate.new({
```
]=]
function ApplicationCommand.Prototype.setType(self: ApplicationCommand, commandType: number): ApplicationCommand
function ApplicationCommand.Prototype.setType(self: ApplicationCommand, commandType: number)
assert(ApplicationCommand.Interface.Type:Is(commandType), `Expected 'commandType' to of enum 'commandType'`)

self.commandType = commandType
Expand All @@ -73,7 +73,7 @@ end
```
]=]
function ApplicationCommand.Prototype.setLocalization(self: ApplicationCommand, localizationCode: string): ApplicationCommand
function ApplicationCommand.Prototype.setLocalization(self: ApplicationCommand, localizationCode: string)
self.commandLocalization = localizationCode

return self
Expand All @@ -93,7 +93,7 @@ end
```
]=]
function ApplicationCommand.Prototype.setDescription(self: ApplicationCommand, description: string): ApplicationCommand
function ApplicationCommand.Prototype.setDescription(self: ApplicationCommand, description: string)
self.commandDescription = description

return self
Expand All @@ -113,7 +113,7 @@ end
```
]=]
function ApplicationCommand.Prototype.setName(self: ApplicationCommand, name: string): ApplicationCommand
function ApplicationCommand.Prototype.setName(self: ApplicationCommand, name: string)
self.commandName = name

return self
Expand All @@ -133,7 +133,7 @@ end
```
]=]
function ApplicationCommand.Prototype.setNSFW(self: ApplicationCommand, isNSFW: boolean): ApplicationCommand
function ApplicationCommand.Prototype.setNSFW(self: ApplicationCommand, isNSFW: boolean)
self.commandNSFW = isNSFW

return self
Expand All @@ -153,7 +153,7 @@ end
```
]=]
function ApplicationCommand.Prototype.setDMPermission(self: ApplicationCommand, canDM: boolean): ApplicationCommand
function ApplicationCommand.Prototype.setDMPermission(self: ApplicationCommand, canDM: boolean)
self.commandDM = canDM

return self
Expand All @@ -173,7 +173,7 @@ end
```
]=]
function ApplicationCommand.Prototype.setGuildPermissions(self: ApplicationCommand, permissionObject: DiscordPermissions.DiscordPermissions): ApplicationCommand
function ApplicationCommand.Prototype.setGuildPermissions(self: ApplicationCommand, permissionObject: DiscordPermissions.DiscordPermissions)
self.commandPermissions = permissionObject

return self
Expand All @@ -193,12 +193,32 @@ end
```
]=]
function ApplicationCommand.Prototype.addOption(self: ApplicationCommand, commandObject: ApplicationCommandOption.ApplicationCommandOptions): ApplicationCommand
function ApplicationCommand.Prototype.addOption(self: ApplicationCommand, commandObject: ApplicationCommandOption.ApplicationCommandOptions)
table.insert(self.options, commandObject)

return self
end

--[=[
@method addOption
@within ApplicationCommand
@param commandObject !ApplicationCommandOption
@return ApplicationCommand
(yet-to-do!)
```lua
```
]=]
function ApplicationCommand.Prototype.setDefaultPermissionEnabled(self: ApplicationCommand, enabled: boolean)
self.defaultPermissionEnabled = enabled

return self
end

--[=[
@method addContext
@within ApplicationCommand
Expand All @@ -213,7 +233,7 @@ end
```
]=]
function ApplicationCommand.Prototype.addContext(self: ApplicationCommand, context: number): ApplicationCommand
function ApplicationCommand.Prototype.addContext(self: ApplicationCommand, context: number)
table.insert(self.contexts, context)

return self
Expand All @@ -233,6 +253,7 @@ end
]=]
function ApplicationCommand.Prototype.toJSONObject(self: ApplicationCommand)
local permissions = "0"
local contexts = {}
local options = {}

for _, commandOption in self.options do
Expand All @@ -243,17 +264,22 @@ function ApplicationCommand.Prototype.toJSONObject(self: ApplicationCommand)
permissions = self.commandPermissions:getValue()
end

for index, context in self.contexts do
contexts[tostring(index)] = context
end

return {
type = self.commandType,
name = self.commandName,
description = self.commandDescription,

nsfw = self.commandNSFW,

default_permission = self.defaultPermissionEnabled,
default_member_permissions = permissions,
dm_permission = self.commandDM,

contexts = self.contexts,
contexts = contexts,

name_localizations = self.commandLocalization,
description_localizations = self.commandLocalization,
Expand Down Expand Up @@ -291,6 +317,8 @@ export type ApplicationCommand = typeof(ApplicationCommand.Prototype) & {
commandType: number,
commandNSFW: boolean,

defaultPermissionEnabled: boolean?,

contexts: { number },
commandPermissions: DiscordPermissions.DiscordPermissions,
options: { ApplicationCommandOption.ApplicationCommandOptions }
Expand Down
28 changes: 27 additions & 1 deletion Package/Classes/Internal/DiscordApplication.luau
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
local Construct = require("../../Utils/Construct")
local IsUnique = require("../../Utils/Dictionary/IsUnique")

local EventIn = require("../Middleware/EventIn")

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

Expand Down Expand Up @@ -106,7 +109,26 @@ function DiscordApplication.Prototype.setSlashCommandsAsync(self: DiscordApplica
end

for commandId: string, command: ApplicationCommand.ApplicationCommand in commandsToUpdate do
self:editSlashCommandAsync(commandId, command):await()
local existingCommandJson

for _, commandObject in allCommands do
if commandObject.id == commandId then
existingCommandJson = commandObject

break
end
end

local existingCommandJsonClone = table.clone(existingCommandJson)
local existingCommandClone = self.eventInMiddleware:processJSON(command:toJSONObject() :: any)

-- fixme: contexts seem to be reported incorrectly?
existingCommandClone.contexts = nil
existingCommandJsonClone.contexts = nil

if IsUnique(existingCommandClone, existingCommandJsonClone) then
self:editSlashCommandAsync(commandId, command):await()
end
end

for _, command: ApplicationCommand.ApplicationCommand in commandsToCreate do
Expand All @@ -123,6 +145,8 @@ function DiscordApplication.Interface.new(discordClient: any, applicationData: {
return Construct({
id = applicationData.id,
flags = applicationData.flags,

eventInMiddleware = EventIn.new(),

discordClient = discordClient
}, DiscordApplication.Prototype)
Expand All @@ -133,6 +157,8 @@ export type DiscordApplication = typeof(DiscordApplication.Prototype) & {
id: string,
flags: number,

eventInMiddleware: EventIn.EventIn,

discordClient: any,
}

Expand Down
19 changes: 19 additions & 0 deletions Package/Utils/Dictionary/IsUnique.luau
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local IsUnique

IsUnique = function(dictionary0: { [any]: any }, dictionary1: { [any]: any })
for key, value in dictionary0 do
if type(value) == "table" then
if IsUnique(value, dictionary1[key]) then
return true
end
else
if value ~= dictionary1[key] then
return true
end
end
end

return false
end

return IsUnique

0 comments on commit de172b8

Please sign in to comment.