advanced user parsing with string patterns

This commit is contained in:
tommy aka doge 2022-08-22 02:55:02 -04:00
parent 9b4c586659
commit 5864eb7c48
3 changed files with 75 additions and 46 deletions

View File

@ -1,42 +1,42 @@
local discordia=require('discordia') -- local discordia=require('discordia')
local API=require('./../strafes_net.lua') -- local API=require('./../strafes_net.lua')
discordia.extensions() -- discordia.extensions()
API.MAPS={} -- API.MAPS={}
for _,game in next,API.GAMES do -- for _,game in next,API.GAMES do
if type(tonumber(game)) == 'number' then -- if type(tonumber(game)) == 'number' then
local maps = {count=0} -- local maps = {count=0}
local res,headers = API:GetMaps(game) -- local res,headers = API:GetMaps(game)
local pages = tonumber(headers['Pagination-Count']) -- local pages = tonumber(headers['Pagination-Count'])
maps.count=maps.count+#res -- maps.count=maps.count+#res
for _,v in next,res do -- for _,v in next,res do
maps[v.ID]=v -- maps[v.ID]=v
end -- end
if pages>1 then -- if pages>1 then
for i=2,pages do -- for i=2,pages do
res,headers = API:GetMaps(game,i) -- res,headers = API:GetMaps(game,i)
maps.count=maps.count+#res -- maps.count=maps.count+#res
for _,j in next,res do -- for _,j in next,res do
maps[j.ID]=j -- maps[j.ID]=j
end -- end
end -- end
end -- end
setmetatable(maps,{__index=function(self,i) -- setmetatable(maps,{__index=function(self,i)
if i=='count' then return self.count end -- if i=='count' then return self.count end
if not tonumber(i) then -- if not tonumber(i) then
for ix,v in next,self do -- for ix,v in next,self do
if type(v)=='table' and v.DisplayName:lower():find(i:lower()) then -- if type(v)=='table' and v.DisplayName:lower():find(i:lower()) then
return v -- return v
end -- end
end -- end
elseif tonumber(i) then -- elseif tonumber(i) then
for ix,v in next,self do -- for ix,v in next,self do
if type(v)=='table' and v.ID==i then -- if type(v)=='table' and v.ID==i then
return v -- return v
end -- end
end -- end
end -- end
end}) -- end})
API.MAPS[game]=maps -- API.MAPS[game]=maps
print('map init done for game:',API.GAMES[game],'count:',API.MAPS[game].count) -- print('map init done for game:',API.GAMES[game],'count:',API.MAPS[game].count)
end -- end
end -- end

View File

@ -36,7 +36,7 @@ commands:Add('user',{},'user <username|mention|"me">', function(t)
local user_info=API:GetUserFromAny(user,message) local user_info=API:GetUserFromAny(user,message)
if type(user_info)=='string' then return message:reply('```'..user_info..'```') end if type(user_info)=='string' then return message:reply('```'..user_info..'```') end
-- for a,b in next,user_info do user_info[a]=tostring(b)end -- for a,b in next,user_info do user_info[a]=tostring(b)end
local description = user_info.description local description = user_info.description=='' and 'null' or user_info.description
local created = tostring(date.fromISO(user_info.created):toSeconds()) local created = tostring(date.fromISO(user_info.created):toSeconds())
local current = date():toSeconds() local current = date():toSeconds()
local accountAge = round((current-created)/86400) local accountAge = round((current-created)/86400)
@ -66,8 +66,7 @@ commands:Add('user',{},'user <username|mention|"me">', function(t)
{name='Last Online',value='<t:'..round(LastOnline)..':R>',inline=true}, {name='Last Online',value='<t:'..round(LastOnline)..':R>',inline=true},
{name='Last Location',value=LastLocation,inline=true}, {name='Last Location',value=LastLocation,inline=true},
{name='Banned',value=isBanned,inline=true}, {name='Banned',value=isBanned,inline=true},
{name='Description',value=description,inline=false},
{name='Description',value=description or 'None',inline=false},
} }
} }
message:reply({embed=embed}) message:reply({embed=embed})

View File

@ -194,7 +194,17 @@ function API:CalculatePoint(rank,count)
end end
function API:GetUserFromAny(user,message) function API:GetUserFromAny(user,message)
if user=='me' then local str = user:match('^["\'](.+)[\'"]$')
local num = user:match('^(%d+)$')
if str then
local roblox_user=API:GetRobloxInfoFromUsername(str)
if not roblox_user.id then return 'User not found' end
return roblox_user
elseif num then
local roblox_user = API:GetRobloxInfoFromUserId(user)
if not roblox_user.id then return 'Invalid user id' end
return roblox_user
elseif user=='me' then
local me=message.author local me=message.author
local roblox_user=API:GetRobloxInfoFromDiscordId(me.id) local roblox_user=API:GetRobloxInfoFromDiscordId(me.id)
if not roblox_user.id then return 'You are not registered with the RoverAPI' end if not roblox_user.id then return 'You are not registered with the RoverAPI' end
@ -210,6 +220,26 @@ function API:GetUserFromAny(user,message)
if not roblox_user.id then return 'User not found' end if not roblox_user.id then return 'User not found' end
return roblox_user return roblox_user
end end
-- if user=='me' then
-- local me=message.author
-- local roblox_user=API:GetRobloxInfoFromDiscordId(me.id)
-- if not roblox_user.id then return 'You are not registered with the RoverAPI' end
-- return roblox_user
-- elseif user:match('<@%d+>') then
-- local user_id=user:match('<@(%d+)>')
-- local member=message.guild:getMember(user_id)
-- local roblox_user=API:GetRobloxInfoFromDiscordId(member.id)
-- if not roblox_user.id then return 'User is not registered with the RoverAPI' end
-- return roblox_user
-- elseif user:match('%d+')==user then
-- local roblox_user = API:GetRobloxInfoFromUserId(user)
-- if not roblox_user.id then return 'Invalid user id' end
-- return roblox_user
-- else
-- local roblox_user=API:GetRobloxInfoFromUsername(user)
-- if not roblox_user.id then return 'User not found' end
-- return roblox_user
-- end
return 'Something went wrong (this should generally not happen)' return 'Something went wrong (this should generally not happen)'
end end