diff --git a/examples/latency-example.lua b/examples/latency-example.lua new file mode 100644 index 0000000..5aaf49d --- /dev/null +++ b/examples/latency-example.lua @@ -0,0 +1,70 @@ +--[[ + Usage: send `!new-button` command to get a message with components. + + Pressing the buttons will do stuff! +]] + +local timer = require 'timer' +local discordia = require 'discordia' +require 'discordia-interactions' + +local client = discordia.Client() +client:enableIntents('messageContent') + +local Stopwatch = discordia.Stopwatch + +client:on('messageCreate', function (msg) + -- Use discordia-extensions instead of this! + -- this is just to get the example working + if msg.content == '!new-button' then + client._api:createMessage(msg.channel.id, { + content = 'Here is a button!', + components = { + { + type = 1, + components = { + { + type = 2, + label = 'API Ping', + style = 1, + custom_id = 'api_ping', + }, + { + type = 2, + label = 'API Ping Message', + style = 3, + custom_id = 'api_ping_msg', + }, + }, + }, + } + }) + end +end) + +---@param intr Interaction +client:on('interactionCreate', function (intr) + if intr.data.custom_id == 'api_ping' then + local sw = Stopwatch() + local msg, err = intr:replyDeferred(true) + sw:stop() + if not msg then + return print(err) + end + intr:reply('The API latency (one-way) is ' .. (sw:getTime() / 2):toString()) + timer.sleep(3000) + intr:deleteReply() + elseif intr.data.custom_id == 'api_ping_msg' then + local sw = Stopwatch() + local msg, err = intr:reply('Calculating latency...') + sw:stop() + if not msg then + return print(err) + end + intr:editReply('The API latency (one-way) is ' .. (sw:getTime() / 2):toString()) + timer.sleep(3000) + intr:deleteReply() + end +end) + +client:run('Bot TOKEN') diff --git a/libs/client/EventHandler.lua b/libs/client/EventHandler.lua index 9540ec3..c534ba6 100644 --- a/libs/client/EventHandler.lua +++ b/libs/client/EventHandler.lua @@ -1,16 +1,23 @@ local Interaction = require("containers/Interaction") local events = { - interaction_create_prelisteners = {} + interaction_create_prelisteners = {}, + interaction_create_postlisteners = {}, } +local function emitListeners(listeners, ...) + for _, v in pairs(listeners) do + v(...) + end +end + function events.INTERACTION_CREATE(d, client) local interaction = Interaction(d, client) - for _, v in pairs(events.interaction_create_prelisteners) do - v(interaction, client) - end - return client:emit("interactionCreate", interaction) + emitListeners(events.interaction_create_prelisteners, interaction, client) + client:emit("interactionCreate", interaction) + emitListeners(events.interaction_create_postlisteners, interaction, client) end +-- This code is part of Discordia local function checkReady(shard) for _, v in pairs(shard._loading) do if next(v) then return end