0

API: https://github.com/satom99/litcord

How can I check my valiable if its a valid ID?

local cmd, serverID, channelID, arg = string.match(message.content, '(%S+) (%d+) (%d+) (%S+.*)')    
local server = client.servers:get('id', serverID)

serverID is the variable and I need to check if the serverID is a valid ID Otherwise I will get an error that server is a nil value.

I am trying days to finish one command and this is a part of it. If you need more contents then please tell me, I will link it to you.

Full Code:

client:on(
    'message',
    function(message)
      local userID = message.author.id
      local cmd, serverID, channelID, arg = string.match(message.content, '(%S+) (%d+) (%d+) (%S+.*)')
      local server = client.servers:get('id', serverID)
      local channel = server.channels:get('id', channelID)
      local cmd = cmd or message.content
      if (cmd == "!say") and message.parent.is_private then 
        if (userID == "187590758360940545") then

          if not server then
            return
          end

          if (server == servers) then

            if (channel == channels) then
              message.channel:sendMessage(arg)
            else
              message:reply("I don't know this channel.")
              return
            end

            message:reply("I don't know this server.")

          end

        else
          message:reply(":sob: Stop!!!!")
        end
      end
    end
)

And how I can let it write in the channel I want with functions message.channel:sendMessage(arg) this is like message:reply it replies back where the message came from.

Piglet
  • 27,501
  • 3
  • 20
  • 43
jepjep40
  • 181
  • 1
  • 2
  • 12
  • Do you need to check if `message.content` is in correct format or if `client.servers:get()` accepted your `serverID`? – mpeterv Jul 21 '16 at 18:51
  • Skype? I guess its client.servers:get() I can it explain it better if I skype you and link you everything. It should check if serverID gets accepted from my command. – jepjep40 Jul 21 '16 at 19:35
  • what is servers here: if (server == servers)? same goes for channels in the following line. you don't need the parentheses btw. – Piglet Jul 21 '16 at 21:33
  • `if not server then return end` this should be done befor you index `server`! Not 4 lines later :-) – Piglet Jul 21 '16 at 21:44
  • `local server = client.servers:get('id', serverID) -- Unter dieser linie if not server then return end` – jepjep40 Jul 21 '16 at 22:19
  • Ich weis jetzt aber nicht ob der Bot jetzt den server nicht findet oder nicht. – jepjep40 Jul 21 '16 at 22:21
  • Ich weis jetzt aber nicht ob der Bot jetzt den server nicht findet oder nicht. Ich bekomme keine Errors und auch keine replies Füge ich if not server then message:reply("This is not a server") spammt es mich und #general und randomly mentioned andere Leute also irgendwas ist passiert Und ich hab kein plan was ich machen muss damit der bot meine nachricht sendet also message:sendMessage(arg) also (arg) ist richtig sendMessage vllt auch aber der rest weis ich nicht.... – jepjep40 Jul 21 '16 at 22:35
  • local server = client.servers:get('id', serverID) isn't getting the server object when serverID is 1103739343822540800 – jepjep40 Jul 21 '16 at 22:48
  • client.servers:getALL() gives me an amount of servers the bot is. – jepjep40 Jul 22 '16 at 08:42
  • http://stackoverflow.com/questions/38530639/attempt-to-call-field-contains-a-nil-value-how-can-i-check-the-table-to-se – jepjep40 Jul 23 '16 at 16:15

1 Answers1

0

Let's forget about validating the serverID for one moment.

Of course you should always handle the case client.servers:get('id', serverID) returing nil.

Simply validating serverID somehow and hoping that you will get a valid server handle back is not an option.

So either use Luas error handling features https://www.lua.org/manual/5.3/manual.html#2.3

or simply check server with an if statement so you won't use server if it is nil.

Simplified:

local server = client.servers:get('id', serverID)
if not server then
  print("No server with id '" .. serverID .. "' found.")
  return -- or do something clever here, show a message box or whatever...
end
-- server won't be nil from here

Unless you know for sure that there is no other way for nil being returned you should handle that possibility properly.

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Und du hast skype :o? Dann füg einfach ein i hinzu nach meinem ersten jep und schon hast du mein skype name wenn du den anderen teil dann zusammen fügst. Ich schreibe es so damit man es nicht so leicht herausfindet. – jepjep40 Jul 21 '16 at 19:57
  • Der Sinn von Stackoverflow besteht darin Fragen so zu stellen, dass das Problem daraus ersichtlich wird und Antworten zu liefern die jedem weiterhelfen der das gleiche Problem hat. SO ist keine Skype-Vermittlung. Bearbeite deine Frage so, dass das was du mir auf Skype erzählen willst drin steht. – Piglet Jul 21 '16 at 20:02