Skip to content

Commit

Permalink
Interaction: refactor loading code & fixes
Browse files Browse the repository at this point in the history
- Interaction: initial reply should now return Message object on success
- enums: new interactionContextType
- Interaction: remove _api
- Interaction: fix editReply not editing initial response
- Interaction: autocomplete now can only take an array
- Interaction: new getters: appPermissions, entitlements, context
- Interaction: replyDeferred returns a message now
- resolver: fix autocomplete resolver not using the right wrapper
  • Loading branch information
Bilal2453 committed Nov 28, 2024
1 parent 5a08c2c commit 2b559fa
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 166 deletions.
8 changes: 1 addition & 7 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,8 @@
"Lua.workspace.library": [
"../deps",
"../libs",
"${3rd}/Luvit/library"
"${addons}/luvit/module/library"
],
"Lua.runtime.builtin": {
"basic": "disable",
"package": "disable",
"string": "disable",
"table": "disable"
},
"Lua.diagnostics.globals": [
"p",
"args"
Expand Down
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
# Introduction

discordia-interactions is a library that makes receiving [Discord Interactions](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-interaction-callback-type) over WebSocket connection possible in the Luvit Discord wrapper [Discordia 2](https://github.com/SinisterRectus/discordia), as well as providing the required methods to respond to the interaction. Although this library does not implement any specific feature such as Message Components or Slash Commands, it act as the base of supporting said features, for example, I've built [discordia-components](https://github.com/Bilal2453/discordia-components/) upon this library, which fully provide support for Message Components (such as buttons and select menus).
discordia-interactions is an extension library that enables [Discordia](https://github.com/SinisterRectus/discordia) to receive and respond to [Discord Interactions](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-response-object-interaction-callback-type).
This is done over the gateway websocket connection [Discordia](https://github.com/SinisterRectus/discordia) offers, and aims to be very extensible so other extensions and libraries can build upon without having to worry about compatibility with each other.

## Documentation
This only implements the interactions portion, for App commands such as Message Components or Slash Commands you need to use a second extension library alongside this one, for example [discordia-components](https://github.com/Bilal2453/discordia-components/) adds support for Message Components such as buttons and select menus.

## Supported responses

The library is documented at the [Wiki](https://github.com/Bilal2453/discordia-interactions/wiki), it is mostly complete but small details are still missing.
- [x] Text Replies.
- [x] Deferred Text Replies.
- [x] Update Replies.
- [x] Autocomplete Replies.
- [x] Modal Replies.
- [ ] Premium Required Replies. (deprecated by Discord)
- [ ] Launch Activity Replies.

## Installing

Due to a bug in Lit, the Lit upstream version of this library won't install, you have to manually clone it.

1. Run `cd PATH` replace `PATH` with your bot's directory path.
1. Run `cd PATH` replace `PATH` with your bot's directory path.
2. Run `git clone https://github.com/Bilal2453/discordia-interactions.git ./deps/discordia-interactions`.
3. Make sure you now have a folder `discordia-interactions` in your `deps` directory.
3. Make sure you now have a folder `discordia-interactions` in your `deps` directory after running the last command.

## Documentation

The library is fully documented over at the [Wiki](https://github.com/Bilal2453/discordia-interactions/wiki), for any questions and support feel free to contact me on our [Discordia server](https://discord.gg/sinisterware), especially if you are writing a library using this!

# Examples
## Building other extensions

This library offers multiple wrapping stages for other libraries to integrate with, allowing you to control the data flow and hook at different stages. For example I built a little Slash command library for the [Discordia Wiki bot](https://github.com/Bilal2453/discordia-wiki-bot), and [this is](https://github.com/Bilal2453/discordia-wiki-bot/blob/da42b124646bc718e17db89fac0c15456da4520f/libs/slash.lua#L140-L144) how it hooks into discordia-interactions to implement a new `slashCommand` event.
You can hook an `interactionCreate` event pre-listener, resolve user inputs to Interaction methods with your own resolver, or wrap resolved values.

## Examples

Here is a simple example that gets executed when you click a button or invoke the interaction in other means.

```lua
local discordia = require("discordia")
require("discordia-interactions") -- Modifies Discordia and adds interactionCreate event
require("discordia-interactions") -- Adds the interactionCreate event and applies other patches

local client = discordia.Client()
local intrType = discordia.enums.interactionType
Expand Down Expand Up @@ -53,3 +73,7 @@ end)

client:run("Bot TOKEN")
```

## Developers Notes

While it is not hard to merge this into Discordia, you are discouraged from doing that. From what I have noticed multiple people are merging this into the Discordia code instead of just using the extensions, and while they might have some reasons to do that, it will make life a lot harder for maintainability, for example when a new discordia-interactions release comes out it will become pretty much a manual job to hand pick the patches to apply, and it creates *more* incompatibility instead of solving any, this is *EXPLICITLY* an extension for good reasons, otherwise I could've simply PRed this into Discordia and called it a day, which is a lot easier to me than doing this.
24 changes: 12 additions & 12 deletions init.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
--[[
Apache License 2.0
Apache License 2.0
Copyright (c) 2022 Bilal2453
Copyright (c) 2022-2024 Bilal2453
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]

local discordia = require("discordia")
Expand Down Expand Up @@ -57,7 +57,7 @@ do
}
local events = client._events
for k, v in pairs(EventHandler) do
if rawget(events, k) then -- compatiblity with other libraries
if rawget(events, k) then -- compatibility with other libraries
local old_event = events[k]
events[k] = function(...)
v(...)
Expand Down
2 changes: 1 addition & 1 deletion libs/client/API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local endpoints = {

function API:createInteractionResponse(id, token, payload, files)
local endpoint = f(endpoints.INTERACTION_CALLBACK, id, token)
return self:request("POST", endpoint, payload, nil, files)
return self:request("POST", endpoint, payload, {with_response = true}, files)
end

function API:createWebhookMessage(id, token, payload, files) -- same as executeWebhook but allows files
Expand Down
12 changes: 6 additions & 6 deletions libs/client/resolver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
The code in this file is not injected into Discordia.
It is only used internally in this library.
Code below is taken from original Discordia source code and modified to work
for our usecase. This file is licensed under the Apache License 2.0.
Some of the code below was taken from original Discordia source code and modified to work
for our use-case. This file is licensed under the Apache License 2.0.
--]]

--[[
Apache License 2.0
Copyright (c) 2016-2022 SinisterRectus (Original author of Discordia)
Copyright (c) 2021-2022 Bilal2453 (Modified message resolver to support raw fields)
Copyright (c) 2016-2024 SinisterRectus
Copyright (c) 2021-2024 Bilal2453
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -192,15 +192,15 @@ resolver.autocomplete_resolvers = {}
resolver.autocomplete_wrappers = {}

function resolver.autocomplete(choices)
for _, v in pairs(resolver.modal_resolvers) do
for _, v in pairs(resolver.autocomplete_resolvers) do
local c = v(choices)
if c then
choices = c
break
end
end
if type(choices) ~= "table" then return end
for _, v in pairs(resolver.modal_wrappers) do
for _, v in pairs(resolver.autocomplete_wrappers) do
v(choices)
end
return choices
Expand Down
Loading

0 comments on commit 2b559fa

Please sign in to comment.