1. You ask a lot of questions, but often they are very similar to questions you already asked. This tells me that you are just copying the code we give you, but not spending any time reading our answers and making sure you understand how the code works. Please make some effort to help yourself before asking others to spend time helping you. If you do not understand an answer we give you, please tell us, and ask about the parts you don't understand.
2. It looks like you are trying to write an addon that automatically sends messages to numbered chat channels at regular intervals. Be very careful with this kind of functionality, as many players will consider such messages to be spam, especially if they are sent too often.
3. You should also be aware that channel 2 is
not always Trade, and channel 1 is
not always General. The code you wrote does not check, so if your addon is sending messages like "WTS [Cool Item] 15k gold PST", you could end up sending them to the LookingForGroup channel, where they will not be welcome.
4. Here is your code adjusted to check if channel 2 exists and, if it doesn't, send to channel 1 instead, and with a slash command so you can start or stop sending the messages. Please read through it and make sure you understand how everything works, and ask about anything you don't understand.
Lua Code:
-- Create an extra variable to hold this, so you can change it easily.
local INTERVAL = 900
-- It's more efficient to define the table with the strings in it to
-- start with, instead of defining an empty table and then adding the
-- strings to it afterward:
local strings = {
"a",
"b",
}
local nocanal = INTERVAL
local f = CreateFrame("Frame")
f:Hide()
-- Hide the frame by default, so you can control when it sends messages.
-- Since you are only using the function once, there is no need to
-- assign it to a variable. Just declare it in the SetScript function:
f:SetScript("OnUpdate", function(self, elapsed)
-- Subtract (count down) instead of add (count up).
nocanal = nocanal - elapsed
if nocanal <= 0 then
local sendToChannel = 2
if GetChannelName(sendToChannel) == 0 then
-- This channel does not exist.
-- Send to channel 1 instead:
sendToChannel = 1
end
-- A few notes here:
-- 1. Use #strings instead of 1,2. This way, you can just add
-- more strings without having to update this part of the code.
-- 2. Use "nil" instead of looking up the player's default language.
-- Sending a nil value has the same effect (the message is sent
-- in the defualt language) without the expense of a function call.
-- 3. Use the "sendToChannel" variable instead of a hardcoded number.
SendChatMessage(strings[random(#strings)], "CHANNEL", nil, sendToChannel)
nocanal = INTERVAL
end
end)
-- Add a slash command to start or stop sending messages, and to adjust
-- the time between messages:
SLASH_RANDOMSPAM1 = "/togglespam"
SlashCmdList.RANDOMSPAM = function(interval)
interval = tonumber(interval)
if interval then
-- You entered a number, like "/togglepsam 800"
if f:IsShown() then
-- The timer is running. Adjust it:
local difference = INTERVAL - interval
nocanal = nocanal - different
end
-- Set the new interval value:
INTERVAL = interval
else
-- You didn't enter a number. Start or stop sending messages.
if f:IsShown() then
-- Messages are sending. Stop it:
f:Hide()
nocanal = INTERVAL
else
-- Messages are not sending. Start it:
nocanal = 0
f:Show()
end
end
end