add variable value in table in lua for ptokax script - lua

Recently i updated my ptokax to 0.5.3 and since then my votekick script has stopped working as in my script takes input from other online users as 1 or 2 as accept or deny the user to be kicked or not but now whenever the user enters 1 or 2 the script has stopped taking input and inserting it in the table i suspects its maybe due to some syntax change . please have a look at my script and suggest .
data = " <Nick> 2" -- this is the way script takes input frm dc chat
s,e,vote= string.find(data,"%b<>%s(.+)")
if vote == "1" then
table.insert(votesPlus,user.sNick)
Core.SendToAll("*--"..user.sNick.." accepts--")
if #votesPlus == votes or # votesMinus == votes then
stop(nTimerId)
end
return true
elseif vote == "2" then
table.insert(votesMinus,user.sNick)
Core.SendToAll("*--"..user.sNick.." denies--")
if #votesPlus == votes or # votesMinus == votes then
stop(nTimerId)
end
return true
else
-- the user is not voting even when poll active
end

Please mention whether you're using the PtokaX released to be used with Lua 5.3.0 or 5.1.5.
The NMDC hub protocols define that the chat messages are sent in the following format:
<Nick> the message|
where | acts as the delimiter.
Apart from that, I don't see any issues with your script. You can, although, optimise performance:
local vote = data:match "%b<>%s(%d)"
-- since you only want a single digit to be matched
-- also, use local variable whenever possible
if vote == "1" then
table.insert(votesPlus, user.sNick)
Core.SendToAll( "*--"..user.sNick.." accepts--" )
if #votesPlus == votes or #votesMinus == votes then
stop( nTimerId )
end
return true
elseif vote == "2" then
table.insert(votesMinus, user.sNick)
Core.SendToAll( "*--"..user.sNick.." denies--" )
if #votesPlus == votes or #votesMinus == votes then
stop( nTimerId )
end
return true
else
-- the user is not voting even when poll active
-- return false so that further scripts might be able to process it
return false
end
PS: I think you should also check if the same user is voting twice! Also, you can put the following code:
if #votesPlus == votes or #votesMinus == votes then
stop( nTimerId )
end
in the call to OnTimer function.

Related

Every time i try running this code it just returns [string “<eval>“]:7: “then expected near ‘=’ ” assistance please

So I’m trying to do some kind of help system where if the player types ‘help’ it will return “alright what do you need help with?” And if the player types anything else it returns something.
This is the script:
print("Hi, welcome to Liminality.This is a horror game so be prepared. If you need anyhelp, just type'help'.Thanks for trying out our game!")
local help = io.read()
if help == "help" then do
print("alright, what do you need help with?")
end
elseif help =~ "help" then do
print("no")
end
In if statements you should not use "do" at the end, You will probably use it mostly for "for" loops.
If you use "elseif" then you should continue the if statement and add just one "end" at the end of the condition; also you can use how many elseif you like. Lastly if you use "else" then you should not write any condition for it because it's supposed to be the last one.
if ( ) then
*action you want to perform*
elseif ( ) then
*another action you want to perform*
else
*last action you want to perform*
end
I corrected the code and you can see i removed the "do" and the "end".
I also corrected the "not equal" sign to "~="
print("Hi, welcome to Liminality. This is a horror game so be prepared. If you
need anyhelp, just type'help'.Thanks for trying out our game!")
local help = io.read()
if (help == "help") then
print("alright, what do you need help with?")
elseif (help ~= "help") then
print("no")
end
Because you have only 1 if statement and than 1 opposite i would have done it like this:
print("Hi, welcome to Liminality. This is a horror game so be prepared. If you
need anyhelp, just type'help'.Thanks for trying out our game!")
local help = io.read()
if (help == "help") then
print("alright, what do you need help with?")
else
print("no")
end

Why does my permission check (Discordia bot) always return false?

The problem is on line 10. No matter what role or permission I have I always get my "You don't have admin or the role required (test message)" message from my bot. I thought it was because I was using "and" instead of "or" but that only fixed a previous problem (which was I would always be approved to use the command no matter what permission or if I had admin). Could someone explain what I'm doing wrong?
Edit: I needed to use "and" instead of "or", and I didn't make sure to take admin off of my alternate account I was using to test this.
client:on("messageCreate", function(message)
if message.author.bot == true then
return end
local author = message.guild:getMember(message.author.id)
local authorRole = message.guild:getMember(message.member)
if message.content == prefix.."Permissions" then
for i, v in ipairs(roleswithpermission) do
if not author:hasPermission("administrator") or not authorRole:hasRole(v) then
message.channel:send("You don't have admin or the role required (test message)")
elseif author:hasPermission("administrator") or authorRole:hasRole(v) then
message.channel:send("This is a test message (means the permission check worked and you have permission)")
end
end
end
end)

How do i make a warn command for my bot using DISCORDIA(LUA)

How can i make a warn command in discordia?
i have tried this but the .json returns as null(no error) in cmd, ive been trying for ages i just cant figure out the problem:
elseif args[1]:lower():sub(3, #".warn") == ".warn" then
local wopen = io.open("warns.json", "r")
local wparse = json.parse(wopen:read("*a"))
wopen:close()
if args[2] then
local mentioned_user = message.mentionedUsers.first
local mentioned_member = message.guild:getMember(mentioned_user)
local mentioned = message.guild:getMember(mentioned_member)
if mentioned ~= nil then
if args[3] then
table.remove(args, 1) --// removes command and mention arguments
local reason = table.concat(args) --// turns remaining contents of the table into one long string value
if wparse[mentioned.id] then --// checks if the mentioned user exists in the database
wparse[mentioned.id] = wparse[mentioned.id] + 1 --// big brain math (if the user already exists, add 1 to their warnings)
message:reply(mentioned.username.." has been warned because: "..reason..". They now have "..wparse[mentioned.id].." warnings.")
else --// if they mentioned user doesn't exist, we should add them to it
wparse[mentioned.id] = 1 --// if they don't exist, add them to the database and set their warnings to 1
message:reply(mentioned.username.." has been warned because: "..reason..". They now have 1 warning.")
end
end
end
else
message:reply("Provide a member to warn.")
end
wopen = io.open("warns.json", "w")
wopen:write(json.stringify(wparse))
wopen:close()
guessing your :sub() command is wrong.
it begins at position three, but then ends at length of ".warn" which is 5
so :sub(3, 5) will never return anything that will never == ".warn"
at best, it might give you ".wa"
you need to add three to the second arg in your sub()
:sub(3, #".warn" +3) == ".warn"

Is there a way to add time to a Wait class inside an If statement?

I started learning LUA a few days ago, started my own project inside Tabletop Simulator game, but I've hit a brick wall. I can't add time to a Wait class.
This is an example of what I tried:
function state_check()
--This function checks if the state of obj_1 and obj_2 is 0 or 1
end
function press_button()
--This function activates other functions based on the state of obj_1 and obj_2
i = 1 --Function starts with a wait of 1 second
if obj_1_state == 1 then --If state is 1, then the function is triggered and 1 second is added to i
Wait.time(func_1, i)
i = i + 1
end
if obj_2_state == 1 then
Wait.time(func_2, i)
end
end
I need for the function to check the first part and if true, do the second part 1 second later. If not, do the second part normally and skip the "i = i + 1".
My problem is that the function does everything at the same time. I know I'm doing something wrong, but I can't figure out what. Is there a way to create some for of gate to do everything in order or anything similar?
Your code seems correct.
I don't know what is the problem.
But I know that one of possible solutions is to follow the "callback hell" style of programming:
local function second_part(obj_2_state)
if obj_2_state == 1 then
Wait.time(func_2, 1)
end
end
local function first_part(obj_1_state, obj_2_state)
if obj_1_state == 1 then
Wait.time(
function()
func_1()
second_part(obj_2_state)
end, 1)
else
second_part(obj_2_state)
end
end
function press_button()
local obj_1_state, obj_2_state = --calculate states of obj_1 and obj_2 here
first_part(obj_1_state, obj_2_state)
end

Awesome WM (v3.5.5) keygrabber alternative

I've never liked the default window switching possibilities in Awesome, so I thought I'd implement Alt-Tab behavior that takes history into account (and does fancy opacity effects).
When Alt-Tab is pressed, the entire history is recorded in a table, and appended to that history are the minimized windows (within the same tag). When this table is generated, I instantiate a keygrabber that captures Tab-press events (to switch to the next client in the table) and Alt-release events (to abort entirely).
A flag keeps track of whether the user is in the process of Alt-tabbing, to prevent the table from being generated over and over again.
The code (it's a lot and you probably don't need to see it, but my experience tells me that when I don't post all the code, people will ask for it eventually):
altTabbing = false
altTabIndex = 1
altTabHistory = {}
clientOpacities = {}
function altTabSetOpacities(restore)
for i,c in pairs(altTabHistory) do
if not restore and i ~= altTabIndex then
c.opacity = 0.5
else
c.opacity = clientOpacities[i]
end
end
end
function myAltTab()
-- First check if the user is already alttabbing, in which case the history
-- should NOT be updated. If the user has just pressed alt-tab, generate a new
-- history-table
if not altTabbing then -- generate history-table
-- Clear Tables
for i in pairs(altTabHistory) do altTabHistory[i] = nil end
for i in pairs(clientOpacities) do clientOpacities[i] = nil end
-- Get focus history for current tag
local s = mouse.screen;
local idx = 0
local c = awful.client.focus.history.get(s, idx)
while c do
table.insert(altTabHistory, c)
table.insert(clientOpacities, c.opacity)
idx = idx + 1
c = awful.client.focus.history.get(s, idx)
end
-- Minimized clients will not appear in the focus history
-- Find them by cycling through all clients, and adding them to the list
-- if not already there.
-- This will preserve the history AND enable you to focus on minimized clients
local t = awful.tag.selected(s)
local all = client.get(s)
for i = 1, #all do
local c = all[i]
local ctags = c:tags();
-- check if the client is on the current tag
local isCurrentTag = false
for j = 1, #ctags do
if t == ctags[j] then
isCurrentTag = true
break
end
end
if isCurrentTag then
-- check if client is already in the history
-- if not, add it
local addToHistory = true
for k = 1, #altTabHistory do
if altTabHistory[k] == c then
addToHistory = false
break
end
end
if addToHistory then
table.insert(altTabHistory, c)
table.insert(clientOpacities, c.opacity)
end
end
end
-- reset current index and flag
altTabIndex = 1
altTabbing = true
-- Now that we have collected all windows, we should run a keygrabber
-- as long as the user is alt-tabbing:
keygrabber.run(
function (mod, key, event)
-- Stop alt-tabbing when the alt-key is released
if key == "Alt_L" and event == "release" then
altTabbing = false
altTabSetOpacities(true)
c = altTabHistory[altTabIndex]
client.focus = c
c:raise()
return false -- stop keygrabber
end
-- Move to next client on each Tab-press
if key == "Tab" and event == "press" then
myAltTab()
return true -- keep going
end
return true -- keep going
end
)
end -- if not altTabbing
-- at this point, the user is alt-tabbing, so we should raise
-- the next client in the history-table
if #altTabHistory < 2 then return end
-- Switch to next client
altTabIndex = altTabIndex + 1
if altTabIndex > #altTabHistory then
altTabIndex = 1 -- wrap around
end
-- focus on current client
local c = altTabHistory[altTabIndex]
c.minimized = false
c:raise()
-- make current client stand out
altTabSetOpacities(false)
end
I realize there's a lot of code, but the main thing is the keygrabber. For still unknown reasons, Awesome sometimes crashes while I'm Alt-Tabbing using this approach. I want to replace the keygrabber by connecting signals to the Alt and Tab keys, and disconnecting them as soon as the user is done. However, I'm not able to do this for some reason.
I instantiate a new key-object like this:
local altkey = awful.key({}, "Alt_L")[1]
I found out by trial and error that awful.key() actually returns a table of which I could query the first element for key, keysym etc, hence the [1]. However, when I try to connect a signal to this object, the LUA interpreter complains and tells me it's a nil object. So my question is: am I doing the right thing here? Is it even possible to replace the keygrabber in the way I intend to?
To use the Alt_L key in Awesome you should refer to "Mod1" in your rc.lua file, to make it mor readable I added the following line to the beginning of my configuration so Alt_L can be used.
Alt_L = "Mod1"

Resources