Thread Tools Display Modes
10-16-12, 08:32 PM   #1
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
Check before running [addon/help]

Hello friends wowinterface,

I have an addon and would like to make some changes in his operation.

I would like it to check if I'm in the guild "x" and am in rank "3", if so it executes the code, if not, it does nothing.

You can?

Follow the code
Code:
local nocanal = 0
local strings = {}

 strings[1] = "123"
 strings[2] = "321" 

local function onUpdate(self,elapsed)
    nocanal = nocanal + elapsed
    if nocanal >= 300 then
	local sendToChannel = 2
	if GetChannelName(sendToChannel) == 0 then
            sendToChannel = 1
	end
	SendChatMessage(strings[random(1,2)], "CHANNEL", GetDefaultLanguage("player"), sendToChannel);
	nocanal = 0
    end
end
local f = CreateFrame("frame")
f:SetScript("OnUpdate", onUpdate)
f:SetScript("OnEvent",onEvent)
  Reply With Quote
10-16-12, 11:19 PM   #2
Ekaterina
A Theradrim Guardian
AddOn Author - Click to view addons
Join Date: Oct 2010
Posts: 65
Caetan,
I believe that you will just need to add some lines to the top.

Lua Code:
  1. if not IsInGuild() then return end -- Checks to see if you're in a guild, if not does nothing further.
  2.  
  3. local myGuildName = "My Guild Name" -- Replace "My Guild Name" with the name you are looking for. Capitalisation matters!
  4. local guildName, guildRankName, guildRankIndex = GetGuildInfo("player")
  5.  
  6. if not (guildName == myGuildName and guildRankIndex == 3) then return end -- Check if your guild name and rank is what you are looking for, if not does nothing further.

This has only been drycoded, but I think this is all you should need.
Ekat
  Reply With Quote
10-17-12, 12:51 AM   #3
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,918
From memory you might have to do guild checks after a check for at least PLAYER_LOGIN perhaps even PLAYER_ENTERING_WORLD as this is when information about your character is first available.

In fact, looking at my GuildChat plugin for nUI I use the events : PLAYER_GUILD_UPDATE and GUILD_ROSTER_UPDATE to trigger before trying to use any guild related information.

The GUILD_ROSTER_UPDATE event is triggered when you call GuildRoster() which I do when the events GUILD_INVITE_REQUEST and GUILD_INVITE_CANCEL are triggered to signify a change in your guild status.
__________________
  Reply With Quote
10-17-12, 09:40 PM   #4
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
I made some changes in the addon and it is working perfectly.

I would like to add a funçãso that would work as follows ...

she notes that only /1 or /2 a message containing the text "123" was launched by another player, if so, the addon sets the variable "nocanal" to "300", avoiding spam. is possible?
  Reply With Quote
10-17-12, 10:03 PM   #5
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
You would need to register for the CHAT_MSG_CHANNEL event. When it fires, check its arguments to find out if the received message is in a channel you care about (the channelNumber argument), and then loop over your table of messages and check the received message (the message argument) against each one. If you find a match, set your variable.
__________________
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
10-18-12, 03:50 AM   #6
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
Got it.

The possibility exists that you write the command line for me?
Unfortunately I do not tenhoum advanced knowledge to do so.
  Reply With Quote
10-18-12, 02:32 PM   #7
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
According to the (partial) code you posted earlier in this thread, your script already has an event handler. If you want help modifying it, post your entire code. I don't know why I have to keep telling you this over and over in every thread you post. If you want help modifying or fixing your code, post your code.
__________________
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
10-18-12, 03:18 PM   #8
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
The current code is this...

Code:
local nocanal = 0
local strings = {}
 strings[1] = "<WOW> 123"
 strings[2] = "<WOW> 321"
local function onUpdate(self,elapsed)
    nocanal = nocanal + elapsed
    if nocanal >= 300 then
	if not IsInGuild() then return end
	local myGuildName = "Guild"
	local guildName, guildRankName, guildRankIndex = GetGuildInfo("player")
	if not (guildName == myGuildName and guildRankIndex == 1) then return end
	local sendToChannel = 2
	if GetChannelName(sendToChannel) == 0 then
            sendToChannel = 1
	end
	SendChatMessage(strings[random(1,10)], "CHANNEL", GetDefaultLanguage("player"), sendToChannel);
	nocanal = 0
    end
end
local f = CreateFrame("frame")
f:SetScript("OnUpdate", onUpdate)
GuildRoster()
He is working perfectly.
I would like to add a function that checks before sending the message, the channel / 2 and / 1 in text search WOW and if found, it resets the "nocanal = 0" so it is no Spamming. understand?

is it possible?
  Reply With Quote
10-18-12, 06:14 PM   #9
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Code:
local MESSAGES = {
	[1] = "<WOW> 123",
	[2] = "<WOW> 321",
}

local TARGET_CHANNEL = 2

local REQUIRED_GUILD = "Guild"
local REQUIRED_RANK = 1

local timeUntilNextMessage = 300

local f = CreateFrame("Frame")

f:Hide()
f:SetScript("OnUpdate", function(self, elapsed)
	timeUntilNextMessage = timeUntilNextMessage - elapsed
	if timeUntilNextMessage < 0 then
		local sendToChannel = TARGET_CHANNEL
		if GetChannelName(sendToChannel) == 0 then
			sendToChannel = 1
		end
		SendChatMessage(MESSAGES[random(#MESSAGES)], "CHANNEL", nil, sendToChannel)
		timeUntilNextMessage = 300
	end
end)

f:RegisterEvent("CHAT_MSG_CHANNEL")
f:RegisterEvent("GUILD_ROSTER_UPDATE")
f:RegisterEvent("PLAYER_GUILD_UPDATE")
f:SetScript("OnEvent", function(self, event, ...)
	if event == "CHAT_MSG_CHANNEL" then
		local message, sender, _, _, _, _, _, channel = ...
		-- Check if the message was received in channel 1 or 2:
		if channel == 1 or channel == 2 then
			-- Loop through each string in your table:
			for _, mystring in pairs(MESSAGES) do
				-- Check if the received message contains the string:
				if strmatch(message, mystring) then
					-- Reset the timer:
					timeUntilNextMessage = 300
				end
			end
		end 
	else
		local guildName, guildRankName, guildRankIndex = GetGuildInfo("player")
		if guildName == REQUIRED_GUILD and guildRankIndex == REQUIRED_RANK then
			self:Show()
		else
			self:Hide()
		end
	end
end)

GuildRoster()
This will:

(1) Reset the timer to prevent spam if someone else sent the same message to channels 1 or 2.

(2) Only run the timer if you are in the right guild and have the right rank.

(3) Not call a bunch of extra functions in every OnUpdate (once per frame drawn to the screen).

(4) Move all configuration variables (such as the guild name and rank) to the top so it's easier to change them.

(5) Use the # operator to get the current number of table entries to pass to random, instead of a hardcoded value, so it's easier to add or remove messages to the table without having to edit this number.

(6) Not waste a function call on GetDefaultLanguage, since passing nil accomplishes the same result.
__________________
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
10-18-12, 10:43 PM   #10
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
This works perfectly, it checks the guild, the rank, the channel and triggers the message.

However it is not resetting the timer when another player sends a message of pre-programmed.
  Reply With Quote
10-19-12, 12:13 AM   #11
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
It only checks messages against the messages in the MESSAGES table. If your messages contain any "magic" characters (such as brackets, parentheses, etc), you could try changing this:

Code:
if strmatch(message, mystring) then
to this:

Code:
if message == mystring then
If it's still not working, please post your actual table of messages, and give an example of a message you think should reset the timer but doesn't.
__________________
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
10-19-12, 03:03 PM   #12
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
I made the change you ordered and are checking correctly now.

The only "problem is that it resets when I send the same message, when in fact it was only to check if anyone else besides me have"

The command line is this
Code:
		local message, sender, _, _, _, _, _, channel = ...
		if channel == 1 or channel == 2 then
			for _, mystring in pairs(MESSAGES) do
				if message == mystring then
					timeUntilNextMessage = 330
					print("resetado")
				end
			end
		end
  Reply With Quote
10-19-12, 03:25 PM   #13
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Then check the sender, too. You've been doing this long enough that you should be able to figure out simple stuff like this on your own without someone spoon-feeding it to you. >_<

Code:
		local message, sender, _, _, _, _, _, channel = ...
		if sender ~= UnitName("player") and (channel == 1 or channel == 2) then
			for _, mystring in pairs(MESSAGES) do
				if message == mystring then
					timeUntilNextMessage = 330
					print("resetado")
				end
			end
		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
10-20-12, 01:27 PM   #14
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
Now yes it is with all the checks that I desire.
Thank you for your help and sorry for the inconvenience or lack of information, I am Brazilian and I use the Google translator to communicate with you. Sorry and thanks.

Just one last curiosity, liha this command ...
Code:
	local message, sender, _, _, _, _, _, channel = ...
		if sender ~= UnitName("player") and (channel == 1 or channel == 2) then
			for _, mystring in pairs(MESSAGES) do
				if message == mystring then
					timeUntilNextMessage = 330
					print("resetado")
				end
			end
		end
There are some (not me) sent a text table and resets the timer addon for 330. As it has reset the timer randomly on a scale from 330 to 300?
  Reply With Quote
10-21-12, 01:20 AM   #15
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
Code:
				if message == mystring then
					timeUntilNextMessage = random(300, 330)
A função random aceita dois números, e retorna um número aleatório que é >= o primeiro número e <= o último número. Caso só fornecem um número, como onde você usá-lo para selecionar uma mensagem da tabela, e retorna um número aleatório que é >= 1 e <= o número.

(The random function accepts two numbers, and return a random number that is >= the first number and <= the second number. If you only provide one number, like you do when you use it to pick a message from the table, it returns a random number that is >= 1 and <= the provided number.)
__________________
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
10-21-12, 10:34 AM   #16
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
I'm very grateful "Phanx" for you have had the job of helping me with the codes and even more now to have answered me in my native language.

Thank you my friend, thank you so much.
  Reply With Quote
10-26-12, 01:57 PM   #17
Caetan0
A Warpwood Thunder Caller
Join Date: Aug 2011
Posts: 99
My friend Phanx I made some small changes in the codes for it to work in /o

Code:
local MESSAGES = {
	[1] = "WOW",
	[2] = "OWO",
}
local REQUIRED_GUILD = "\69\115\112\97\114\116\97\110\111\115"
local REQUIRED_RANK = 0
local timeUntilNextMessage = 900
local f = CreateFrame("Frame")
f:Hide()
f:SetScript("OnUpdate", function(self, elapsed)
	timeUntilNextMessage = timeUntilNextMessage - elapsed
	if timeUntilNextMessage < 0 then
		SendChatMessage("Informativo:", "OFFICER", nil, nil);
		SendChatMessage(MESSAGES[random(#MESSAGES)], "OFFICER", nil, nil)
		timeUntilNextMessage = 900
	end
end)
f:RegisterEvent("CHAT_MSG_GUILD")
f:RegisterEvent("GUILD_ROSTER_UPDATE")
f:RegisterEvent("PLAYER_GUILD_UPDATE")
f:SetScript("OnEvent", function(self, event, ...)
	if event == "CHAT_MSG_GUILD" then
		local message, sender, _, _, _, _, _, channel = ...
		if sender ~= UnitName("player") then
			for _, mystring in pairs(MESSAGES) do
				if message == mystring then
					timeUntilNextMessage = random(910, 930)
				end
			end
		end 
	else
		local guildName, guildRankName, guildRankIndex = GetGuildInfo("player")
		if guildName == REQUIRED_GUILD and guildRankIndex == REQUIRED_RANK then
			self:Show()
		else
			self:Hide()
		end
	end
	if event == "GUILD_ROSTER_UPDATE" then
		MESSAGES [10] = GetGuildRosterMOTD()
	end
end)
GuildRoster()
It was thus ta certinho you believe you or a mistake?
  Reply With Quote
10-26-12, 05:23 PM   #18
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
I would change this:

Code:
	else
		local guildName, guildRankName, guildRankIndex = GetGuildInfo("player")
		if guildName == REQUIRED_GUILD and guildRankIndex == REQUIRED_RANK then
			self:Show()
		else
			self:Hide()
		end
	end
	if event == "GUILD_ROSTER_UPDATE" then
		MESSAGES [10] = GetGuildRosterMOTD()
	end
...to this:

Code:
	elseif event == "GUILD_ROSTER_UPDATE" then
		table.insert(MESSAGES, GetGuildRosterMOTD())
	else -- event is PLAYER_GUILD_UPDATE
		local guildName, guildRankName, guildRankIndex = GetGuildInfo("player")
		if guildName == REQUIRED_GUILD and guildRankIndex == REQUIRED_RANK then
			self:Show()
		else
			self:Hide()
		end
	end
The way you wrote it, it would unnecessarily check if the player was in a guild and start/stop the update even if the player's guild state didn't change, because "else" catches any other event.

Also, you should not insert the guild MOTD into MESSAGES at position 10 unless you know there are already 9 messages in the table. If the indices are not sequential, then the # operator to get the number of messages in the table (for picking a random one) will not work correctly.

----

Como você escreveu, o código desnecessariamente verificar se o jogador está em uma guilda, e iniciar ou parar o temporizador, mesmo se o estado do jogador em sua guilda não mudou, porque "else" pega tudo que você não explicitamente verificar. Adicionar um cheque para o novo evento antes de "else" para evitá-lo.

Além disso, você não deve adicionar a MOTD da guilda para a tabela no índice 10 a menos que você sabe que há exatamente nove outras mensagens já em cima da tabela. Se os índices não são sequenciais, o operador # não irá funcionar corretamente para obter o número de mensagens na tabela, para selecionar uma mensagem aleatória.
__________________
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 » Developer Discussions » Lua/XML Help » Check before running [addon/help]

Thread Tools
Display Modes

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