Thread Tools Display Modes
06-08-12, 06:04 AM   #1
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
channel check [help]

friends, I have this code ...

Code:
local nocanal = 0
local strings = {}

 strings[1] = "a"
 strings[2] = "b"

local function onUpdate(self,elapsed)
    nocanal = nocanal + elapsed
    if nocanal >= 900 then
        SendChatMessage(strings[random(1,2)], "CHANNEL", GetDefaultLanguage("player"), "2");
        nocanal = 0
    end
end

local f = CreateFrame("frame")
f:SetScript("OnUpdate", onUpdate)
Only when I'm somewhere where there is no channel /2 nothing happens, is there any way I can make a verification and if there is no channel /2, it sends the message to the channel /1?
  Reply With Quote
06-08-12, 11:19 AM   #2
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
Code:
-- Check if the specific channel exists
myChannel = 1;
id, name = GetChannelName(myChannel);
if (id > 0 and name ~= nil) then
  SendChatMessage("This is just a test.", "CHANNEL", nil, id);
end
Examples section of the GetChannelName() API page.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
06-08-12, 04:30 PM   #3
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
I understand friend The problem is that I have a good knowledge of codes .lua , It would be possible to modify my code to make it work this way?
  Reply With Quote
06-09-12, 02:34 AM   #4
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
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:
  1. -- Create an extra variable to hold this, so you can change it easily.
  2. local INTERVAL = 900
  3.  
  4. -- It's more efficient to define the table with the strings in it to
  5. -- start with, instead of defining an empty table and then adding the
  6. -- strings to it afterward:
  7. local strings = {
  8.     "a",
  9.     "b",
  10. }
  11.  
  12. local nocanal = INTERVAL
  13.  
  14. local f = CreateFrame("Frame")
  15. f:Hide()
  16. -- Hide the frame by default, so you can control when it sends messages.
  17.  
  18. -- Since you are only using the function once, there is no need to
  19. -- assign it to a variable. Just declare it in the SetScript function:
  20. f:SetScript("OnUpdate", function(self, elapsed)
  21.     -- Subtract (count down) instead of add (count up).
  22.     nocanal = nocanal - elapsed
  23.     if nocanal <= 0 then
  24.         local sendToChannel = 2
  25.         if GetChannelName(sendToChannel) == 0 then
  26.             -- This channel does not exist.
  27.             -- Send to channel 1 instead:
  28.             sendToChannel = 1
  29.         end
  30.         -- A few notes here:
  31.         -- 1. Use #strings instead of 1,2. This way, you can just add
  32.         --    more strings without having to update this part of the code.
  33.         -- 2. Use "nil" instead of looking up the player's default language.
  34.         --    Sending a nil value has the same effect (the message is sent
  35.         --    in the defualt language) without the expense of a function call.
  36.         -- 3. Use the "sendToChannel" variable instead of a hardcoded number.
  37.         SendChatMessage(strings[random(#strings)], "CHANNEL", nil, sendToChannel)
  38.         nocanal = INTERVAL
  39.     end
  40. end)
  41.  
  42. -- Add a slash command to start or stop sending messages, and to adjust
  43. -- the time between messages:
  44. SLASH_RANDOMSPAM1 = "/togglespam"
  45. SlashCmdList.RANDOMSPAM = function(interval)
  46.     interval = tonumber(interval)
  47.     if interval then
  48.         -- You entered a number, like "/togglepsam 800"
  49.         if f:IsShown() then
  50.             -- The timer is running. Adjust it:
  51.             local difference = INTERVAL - interval
  52.             nocanal = nocanal - different
  53.         end
  54.         -- Set the new interval value:
  55.         INTERVAL = interval
  56.     else
  57.         -- You didn't enter a number. Start or stop sending messages.
  58.         if f:IsShown() then
  59.             -- Messages are sending. Stop it:
  60.             f:Hide()
  61.             nocanal = INTERVAL
  62.         else
  63.             -- Messages are not sending. Start it:
  64.             nocanal = 0
  65.             f:Show()
  66.         end
  67.     end
  68. end
__________________
Retired author of too many addons.
Message me if you're interested in taking over one of my addons.
Don’t message me about addon bugs or programming questions.
  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Search/Requests » channel check [help]


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off