Skip to content

Instantly share code, notes, and snippets.

@veeponym
Created February 15, 2023 13:08
Show Gist options
  • Save veeponym/f8cc3251f3141cdec6da81b5d5f85792 to your computer and use it in GitHub Desktop.
Save veeponym/f8cc3251f3141cdec6da81b5d5f85792 to your computer and use it in GitHub Desktop.
-- Require the http library
local http = require('http')
-- Require the json library
local json = require('json')
-- Define a joke object
local joke = {}
-- Define a function to register the commands
function joke:registerCommands(client, slash)
-- Define a slash command for joke
slash:registerCommand('joke', 'Tell a random joke', function(interaction)
-- Make a http request to the joke API
http.request('GET', 'https://official-joke-api.appspot.com/random_joke', {}, function(res)
-- Check if the status code is 200
if res.statusCode == 200 then
-- Read the response data
res:on('data', function(data)
-- Parse the data as JSON
local jokeData = json.parse(data)
-- Get the setup and punchline from the joke data
local setup = jokeData.setup
local punchline = jokeData.punchline
-- Reply with the joke
interaction:reply(setup .. '\n' .. punchline)
end)
else
-- Reply with an error message
interaction:reply('Sorry, I could not get a joke for you.')
end
end)
end)
end
-- Define a function to unregister the commands
function joke:unregisterCommands(client, slash)
-- Unregister the slash command for joke
slash:unregisterCommand('joke')
end
-- Return the joke object
return joke
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment