Skip to content

Instantly share code, notes, and snippets.

@Siapran
Last active November 22, 2020 22:37
Show Gist options
  • Save Siapran/42a14232bbf56f2f956d9978b4c3507e to your computer and use it in GitHub Desktop.
Save Siapran/42a14232bbf56f2f956d9978b4c3507e to your computer and use it in GitHub Desktop.
local http = require("coro-http")
local discordia = require("discordia")
local client = discordia.Client()
local function starts_with( str, start )
return str:sub(1, #start) == start
end
local prefix = "!"
client:on("ready", function()
client:info("Logged in as ".. client.user.username)
end)
local commands = setmetatable({}, { __index = function( t, k )
if k == "squeak" then
return { call = function( message, arg )
message:reply("🐁 squeak!")
end }
end
return {
brief = "no such command",
call = function( message, arg )
-- ignore unrecognized command
end
}
end})
local function fetch( attachments )
local res = {}
for idx, v in ipairs(attachments) do
local _, body = http.request("GET", v.url)
res[idx] = {"SPOILER_" .. v.filename, body}
end
return res
end
commands.spoiler = {
brief = "replace this message with all attachments marked as spoilers",
example = "[MESSAGE_WITH_ATTACHMENTS] " .. prefix
.. "spoiler REST_OF_YOUR_MESSAGE",
call = function( message, arg )
if not message.attachments then return end
local data = fetch(message.attachments)
message:reply({
mention = message.author,
content = "posted: " .. arg,
-- allowed_mentions = {
-- parse = {}
-- },
files = data
})
message:delete()
end
}
commands.remove = {
brief = "remove a spoiler post created from your own message",
example = prefix .. "remove BOT_MESSAGE_ID",
call = function( message, arg )
local found = message.channel:getMessage(arg)
if not found then
message:reply(
"could not find message " .. arg .. " in this channel")
return
end
if found.author ~= client.user then
message:reply("target message is not a bot message")
return
end
local firstMention = found.mentionedUsers.first
if message.author ~= firstMention then
message:reply("you are not the originator of the target message")
return
end
found:delete()
message:delete()
end
}
commands.info = {
brief = "display bot information",
call = function( message, arg )
local answer = { embed = {
author = {
name = "Spoiler Bot",
icon_url = client.user.avatarURL,
},
description =
"A helper bot for marking images as spoiler on mobile.\n"
.. "Source code available [here](https://gist.github.com"
.. "/Siapran/42a14232bbf56f2f956d9978b4c3507e)."
}}
message:reply(answer)
end
}
commands.help = {
brief = "display a list of available commands, "
.. "or help for a particular command",
example = prefix .. "help spoiler",
call = function( message, arg )
if arg == "" then
local fields = {}
for key,value in pairs(commands) do
table.insert(fields, {
name = key,
value = value.brief or ""
})
end
local answer = { embed = {
title = "available commands",
fields = fields
}}
message:reply(answer)
else
local command = commands[arg]
local fields = {}
if command.brief then
table.insert(fields, {
name = "brief",
value = command.brief
})
end
if command.example then
table.insert(fields, {
name = "example",
value = "`" .. command.example .. "`"
})
end
local answer = { embed = {
title = arg,
fields = fields
}}
message:reply(answer)
end
end
}
client:on("messageCreate", function( message )
if message.author.bot then return end
if not starts_with(message.content, prefix) then return end
local cmd, arg =
message.content
:sub(prefix:len() + 1)
:match("(%S+)%s*(.*)")
if cmd then
client:info(tostring(message.author) .. ": "
.. tostring(message.content))
commands[cmd].call(message, arg)
end
end)
if not args[2] then
client:error("Please provide a bot token via commandline arguments.")
return
end
client:run("Bot " .. args[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment