diff --git a/data/BotProfile.lua b/data/BotProfile.lua new file mode 100644 index 0000000..fe2f621 --- /dev/null +++ b/data/BotProfile.lua @@ -0,0 +1,12 @@ +BotProfile = { + playing = "=help" +} + +BotProfile.status = string.format([[ +%s is a general-purpose bot written in Lua. +My default prefix is %s, see %shelp for more information about me! + +My source code can be viewed here: %s +]], info.name, info.prefix, info.prefix ,info.repository) + +return BotProfile diff --git a/data/Changelog.lua b/data/Changelog.lua index 454d03a..8694d8a 100644 --- a/data/Changelog.lua +++ b/data/Changelog.lua @@ -24,4 +24,16 @@ Added command: - convert (converts units of measurements) ]] +Changelog['v0.4'] = [[ +Added commands: +- pickrandom (picks a random word from supplied arguments, split by spaces) +- ynm (ask a question and get a reply ranging from yes, no or maybe) +- checklove (ping two users and get their love value calculated based on their discord IDs) +- checktruth (provide a string of text after the command name and get the string's objective, inrefutable and correct truth value) + +Changes: +- Help command is now more compact +- added a "Playing" status message +]] + return Changelog diff --git a/data/CommandList.lua b/data/CommandList.lua index 7d6b052..070352c 100755 --- a/data/CommandList.lua +++ b/data/CommandList.lua @@ -27,7 +27,7 @@ local commandType = { add("info", "Displays info about the bot.", commandType.technical, function(Message, Caller, ...) local output = "Hi, I'm " .. info.name .. "\nI am a general-purpose bot, written in Lua!\n" local prefix = "My prefix is: " .. info.prefix - local source = "Check out my source code: " .. info.repository + local source = "Check out my source code: `" .. info.repository .. "`" output = output .. string.format("%s\n%s\n", prefix, source) Message.channel:send(output) @@ -48,13 +48,13 @@ add("help", "Displays a help message about all commands.", commandType.technical -- Add one by one by types to output: for i, v in pairs(sorted) do - output = output .. "\n\n**__" .. tostring(i) .. "__**" + output = output .. "\n**__" .. tostring(i) .. "__**:" -- Add each command to output: for _, l in pairs(v) do local cmd_name = "**" .. tostring(l.name) .. "**" - local desc = "" .. tostring(l.desc) .. "" - output = output .. "\n " .. cmd_name .. "\n " .. desc .. "" + local desc = "*" .. tostring(l.desc) .. "*" + output = output .. "\n> " .. cmd_name .. " " .. desc end end @@ -74,7 +74,6 @@ add("changelog", "Shows the changelog for the current or a specific version of t else showVersion = arg[1] end - if Changelog[showVersion] == nil then local tab = {} @@ -99,7 +98,7 @@ add("echo", "I will echo back whatever you said to me.", commandType.chat, funct return end local str = table.concat(tab, " ") - + Message.channel:send(Message.author.mentionString .. " said: '" .. str .. "'") end) @@ -120,6 +119,100 @@ add("flip", "Flip a coin.", commandType.math, function(Message, Caller, ...) Message.channel:send(Message.author.mentionString .. " flipped a coin:\n**" .. flip .. "**") end) +add("ynm", "Yes? No? Maybe? Ask me a question!", commandType.math, function(Message, Caller, ...) + local args = ... + local temp = string.format(YesNoMaybe.askMeSomethingPrompt, Message.author.mentionString) + if args == nil then + Message.channel:send(temp) + return + end + if #args < 1 then + Message.channel:send(temp) + return + end + local vals = {} + + for i,v in pairs(YesNoMaybe.weight) do + for _=0, v do + table.insert(vals, i) + end + end + + local out = easy.string.firstUppercase(easy.table.randomIn(vals)) + Message.channel:send(string.format(YesNoMaybe.responsePrompt, out, Message.author.mentionString)) +end) + +add("pickrandom", "Pick a random word from a string input (seperated by spaces).", commandType.math, function(Message, Caller, ...) + local args = ... + local usage = "\nUsage: pickrandom option1 option2 option3 long_option_four option.5 ( -> randomly chosen)" + + local function handleErrorMessage(txt, ...) + Message.channel:send(string.format(txt, ...) .. usage) + end + if args == nil then + handleErrorMessage("Please provide a string from which words can be randomly picked.") + return + end + if #args < 1 then + handleErrorMessage("Please provide a string from which words can be randomly picked.") + return + end + + local pick = easy.table.randomIn(args) + Message.channel:send("Your randomly chosen word is:\n**" .. pick .. "**") +end) + +add("checklove", "Check the love value between two members.", commandType.math, function(Message, Caller, ...) + local mentioned = Message.mentionedUsers + local usage = "\nUsage: checklove @FirstMember @SecondMember" + if #mentioned < 2 then + Message.channel:send("You need to provide two users as input!" .. usage) + end + + -- Member IDs: + mentioned = {mentioned[1][1], mentioned[1][2]} + + local percentage = (mentioned[1]%1000 + mentioned[2]%1000) % 101 + Message.channel:send(string.format("%s x %s = %d%s", "<@"..mentioned[1]..">", "<@"..mentioned[2]..">", percentage, "%")) +end) + +add("checktruth", "Check the truth-value of a given statement.", commandType.math, function(Message, Caller, ...) + local args = ... + local usage = "\nUsage: checktruth This String Will Be Evaluated For The Truth Value" + local function handleErrorMessage(txt, ...) + Message.channel:send(string.format(txt, ...) .. usage) + end + + -- Check if string provided: + if args == nil then + handleErrorMessage("Faulty Syntax. You need to provide a string to evaluate after the command name!") + return + end + if #args < 1 then + handleErrorMessage("Faulty Syntax. You need to provide a string to evaluate after the command name!") + return + end + + local fullString = table.concat(args, " ") + local fullEncoded = base64.encode(tostring(fullString)) + local percentage = 0 + + -- Emergency Break-Out: + if fullEncoded == nil then + Message.channel:send("There was an error calculating the truth value...") + return + end + + -- Calculate Percentage: + for i=1, #fullEncoded do + local char = string.sub(fullEncoded, i, i) + percentage = percentage + string.byte(char) + end + percentage = percentage % 101 + + Message.channel:send(string.format("The truth value for... `%s`... is %d%s!", fullString, percentage, "%")) +end) + add("roll", "Roll a die. (Supports `roll int int` and `roll string`(-> String: '2d6', '4d20') )", commandType.math, function(Message, Caller, ...) local arg = ... local default = "1d6" @@ -192,7 +285,7 @@ add("convert", "Convert units of measurement.", commandType.math, function(Messa for j,k in pairs(v) do table.insert(concat, j) end - local cat_name = (string.sub(i, 1, 1)):upper()..string.sub(i, 2, -1):lower() + local cat_name = easy.string.firstUppercase(i) usage = usage .. "\n" .. cat_name .. ":\n> " .. table.concat(concat, ", ") end local function handleErrorMessage(txt, ...) @@ -273,7 +366,7 @@ local function handleSocialEvent(Message, Caller, type, textAction, emojis) local text = textAction .. " you" local emoji = emojis[1] local gifs = Gifs[type] - local gif = gifs[math.random(1, #gifs)] + local gif = easy.table.randomIn(gifs) -- User @'ed themselves: if active == passive then diff --git a/data/ReactionList.lua b/data/ReactionList.lua index 53462d5..08a99c1 100644 --- a/data/ReactionList.lua +++ b/data/ReactionList.lua @@ -5,14 +5,4 @@ local function add(id, reactionType, reaction, trigger) ReactionList[id] = MessageSubstring:init(id, reactionType, reaction, trigger) end --- Add Reactions here: -add("general_kenobi", "reply", - {"General Kenobi!"}, - {"hello there", "hello there!"} -) -add("hi_dad", "reply", - {"Hi, I'm dad!"}, - {"i am", "i'm", "im"} -) - return ReactionList diff --git a/data/Units.lua b/data/Units.lua index a19b3d9..4eebc23 100644 --- a/data/Units.lua +++ b/data/Units.lua @@ -54,7 +54,7 @@ Units.list = { ['pint'] = 0.4731765, ['drop'] = 0.00005 }, - ['weigth'] = { + ['mass'] = { ['mg'] = 0.001, ['g'] = 1, ['kg'] = 1000, diff --git a/data/YesNoMaybe.lua b/data/YesNoMaybe.lua new file mode 100644 index 0000000..3800394 --- /dev/null +++ b/data/YesNoMaybe.lua @@ -0,0 +1,17 @@ +-- This file contains the weight values for yes, no and maybe for the ynm command: +-- Notice: Numbers are not percentages! + +YesNoMaybe = {} +YesNoMaybe.weight = { + ['yes'] = 5, + ['no'] = 5, + ['maybe'] = 2 +} + +-- This string will be fed through string.format(), %s is Message.author.mentionString (Ping) +YesNoMaybe.askMeSomethingPrompt = "I don't know, how about you ask me something, %s?" + +-- This is the answer string, it will be fed through string.format() as well: (first is out, second is Message.author.mentionString) +YesNoMaybe.responsePrompt = "%s, %s." + +return YesNoMaybe diff --git a/globalInfo.lua b/globalInfo.lua index 79608f3..43986c8 100644 --- a/globalInfo.lua +++ b/globalInfo.lua @@ -1,5 +1,6 @@ +-- Information considered to be global: (can be modified, but has nothing secret or config-related in it) globalInfo = { - version = 'v0.3' + version = 'v0.4' } return globalInfo diff --git a/import.lua b/import.lua index 1c7e76c..8417373 100755 --- a/import.lua +++ b/import.lua @@ -3,8 +3,9 @@ bot = require "src/bot" easy = require "src/easy" -- Libraries: -Class = require "lib/class" -- https://github.com/vrld/hump/blob/master/class.lua +Class = require "lib/class" -- https://github.com/vrld/hump/blob/master/class.lua Switch = require "lib/Switch" -- https://github.com/NiroUwU/Lua-Utils/blob/main/Switch.lua +base64 = require "lib/base64" -- https://github.com/iskolbin/lbase64/blob/master/base64.lua -- Import General Data About the Bot globalInfo = require "globalInfo" @@ -22,7 +23,9 @@ require "data/ReactionList" require "data/BannedIDs" -- Data required for Commands: +require "data/BotProfile" require "data/Units" require "data/HelloResponse" require "data/Gifs" require "data/Changelog" +require "data/YesNoMaybe" diff --git a/lib/base64.lua b/lib/base64.lua new file mode 100644 index 0000000..32de332 --- /dev/null +++ b/lib/base64.lua @@ -0,0 +1,201 @@ +--[[ + + base64 -- v1.5.3 public domain Lua base64 encoder/decoder + no warranty implied; use at your own risk + + Needs bit32.extract function. If not present it's implemented using BitOp + or Lua 5.3 native bit operators. For Lua 5.1 fallbacks to pure Lua + implementation inspired by Rici Lake's post: + http://ricilake.blogspot.co.uk/2007/10/iterating-bits-in-lua.html + + author: Ilya Kolbin (iskolbin@gmail.com) + url: github.com/iskolbin/lbase64 + + COMPATIBILITY + + Lua 5.1+, LuaJIT + + LICENSE + + See end of file for license information. + +--]] + + +local base64 = {} + +local extract = _G.bit32 and _G.bit32.extract -- Lua 5.2/Lua 5.3 in compatibility mode +if not extract then + if _G.bit then -- LuaJIT + local shl, shr, band = _G.bit.lshift, _G.bit.rshift, _G.bit.band + extract = function( v, from, width ) + return band( shr( v, from ), shl( 1, width ) - 1 ) + end + elseif _G._VERSION == "Lua 5.1" then + extract = function( v, from, width ) + local w = 0 + local flag = 2^from + for i = 0, width-1 do + local flag2 = flag + flag + if v % flag2 >= flag then + w = w + 2^i + end + flag = flag2 + end + return w + end + else -- Lua 5.3+ + extract = load[[return function( v, from, width ) + return ( v >> from ) & ((1 << width) - 1) + end]]() + end +end + + +function base64.makeencoder( s62, s63, spad ) + local encoder = {} + for b64code, char in pairs{[0]='A','B','C','D','E','F','G','H','I','J', + 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y', + 'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n', + 'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2', + '3','4','5','6','7','8','9',s62 or '+',s63 or'/',spad or'='} do + encoder[b64code] = char:byte() + end + return encoder +end + +function base64.makedecoder( s62, s63, spad ) + local decoder = {} + for b64code, charcode in pairs( base64.makeencoder( s62, s63, spad )) do + decoder[charcode] = b64code + end + return decoder +end + +local DEFAULT_ENCODER = base64.makeencoder() +local DEFAULT_DECODER = base64.makedecoder() + +local char, concat = string.char, table.concat + +function base64.encode( str, encoder, usecaching ) + encoder = encoder or DEFAULT_ENCODER + local t, k, n = {}, 1, #str + local lastn = n % 3 + local cache = {} + for i = 1, n-lastn, 3 do + local a, b, c = str:byte( i, i+2 ) + local v = a*0x10000 + b*0x100 + c + local s + if usecaching then + s = cache[v] + if not s then + s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)]) + cache[v] = s + end + else + s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)]) + end + t[k] = s + k = k + 1 + end + if lastn == 2 then + local a, b = str:byte( n-1, n ) + local v = a*0x10000 + b*0x100 + t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[64]) + elseif lastn == 1 then + local v = str:byte( n )*0x10000 + t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[64], encoder[64]) + end + return concat( t ) +end + +function base64.decode( b64, decoder, usecaching ) + decoder = decoder or DEFAULT_DECODER + local pattern = '[^%w%+%/%=]' + if decoder then + local s62, s63 + for charcode, b64code in pairs( decoder ) do + if b64code == 62 then s62 = charcode + elseif b64code == 63 then s63 = charcode + end + end + pattern = ('[^%%w%%%s%%%s%%=]'):format( char(s62), char(s63) ) + end + b64 = b64:gsub( pattern, '' ) + local cache = usecaching and {} + local t, k = {}, 1 + local n = #b64 + local padding = b64:sub(-2) == '==' and 2 or b64:sub(-1) == '=' and 1 or 0 + for i = 1, padding > 0 and n-4 or n, 4 do + local a, b, c, d = b64:byte( i, i+3 ) + local s + if usecaching then + local v0 = a*0x1000000 + b*0x10000 + c*0x100 + d + s = cache[v0] + if not s then + local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d] + s = char( extract(v,16,8), extract(v,8,8), extract(v,0,8)) + cache[v0] = s + end + else + local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d] + s = char( extract(v,16,8), extract(v,8,8), extract(v,0,8)) + end + t[k] = s + k = k + 1 + end + if padding == 1 then + local a, b, c = b64:byte( n-3, n-1 ) + local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + t[k] = char( extract(v,16,8), extract(v,8,8)) + elseif padding == 2 then + local a, b = b64:byte( n-3, n-2 ) + local v = decoder[a]*0x40000 + decoder[b]*0x1000 + t[k] = char( extract(v,16,8)) + end + return concat( t ) +end + +return base64 + +--[[ +------------------------------------------------------------------------------ +This software is available under 2 licenses -- choose whichever you prefer. +------------------------------------------------------------------------------ +ALTERNATIVE A - MIT License +Copyright (c) 2018 Ilya Kolbin +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +------------------------------------------------------------------------------ +ALTERNATIVE B - Public Domain (www.unlicense.org) +This is free and unencumbered software released into the public domain. +Anyone is free to copy, modify, publish, use, compile, sell, or distribute this +software, either in source code form or as a compiled binary, for any purpose, +commercial or non-commercial, and by any means. +In jurisdictions that recognize copyright laws, the author or authors of this +software dedicate any and all copyright interest in the software to the public +domain. We make this dedication for the benefit of the public at large and to +the detriment of our heirs and successors. We intend this dedication to be an +overt act of relinquishment in perpetuity of all present and future rights to +this software under copyright law. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN +ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +------------------------------------------------------------------------------ +--]] diff --git a/main.lua b/main.lua index 25de7a8..2b2ea68 100755 --- a/main.lua +++ b/main.lua @@ -49,9 +49,16 @@ local function findMessageSubstring(Message, Caller, messageData, ...) end end +local function updateProfile() + Client:setUsername(info.name) + Client:setGame(BotProfile.playing) + -- Client:setStatus(BotProfile.status) +end + -- MAIN: Client:on("ready", function() + updateProfile() bot.debug("Bot started: %s", os.date()) end) @@ -102,7 +109,7 @@ Client:on("messageCreate", function(Message) ) end - -- Find Substring, react to it: (still broken) + -- Find Substring, react to it: (still broken... ;w;) -- findMessageSubstring(Message, messageData) end) diff --git a/src/easy.lua b/src/easy.lua index 765d23b..0a443c8 100644 --- a/src/easy.lua +++ b/src/easy.lua @@ -1,12 +1,19 @@ -easy = {} +easy = { + table = {}, + string = {} +} -function easy.randomIn(tab) +function easy.table.randomIn(tab) if type(tab) ~= "table" then - bot.debug("easy.randomIn needs a table of contents!") + bot.debug("randomIn() needs a table of contents!") return end return tab[math.random(1, #tab)] end +function easy.string.firstUppercase(str) + return (string.sub(str, 1, 1)):upper()..string.sub(str, 2, -1):lower() +end + return easy