This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Mantas's profile picture

Chat Box

Started by Mantas, 08 August 2016 - 07:52 AM
Mantas #1
Posted 08 August 2016 - 09:52 AM
So I have been playing around with Chat Boxes recently and I have been seeing their capabilities. I played around a bit and made this.

(prepare to cringe at the layout)

cbox = peripheral.wrap("right")
local T = os.time()
local D = os.day()
  while true do
  event, player, message,command = os.pullEvent("chat")
  if message == "Time?" then
	cbox.say(string.char(167).."6"..string.char(167).."l".."The time is "..textutils.formatTime( T, false ).." on Day "..D)
  end
end

So once I did that I figured I could do something like this, using surferpup's Real World Time API.



os.loadAPI("timeAPI")

cbox = peripheral.wrap("right")
local T = os.time()
local D = os.day()
  while true do
	event, player, message,command = os.pullEvent("chat")
	  if message == "Time in-game?" then
	  cbox.say(string.char(167).."6"..string.char(167).."l".."The time is "..textutils.formatTime( T, false ).." on Day "..D)
	end

local uTime = timeAPI.getTime("UTC",0,dateTimeFormat,0)

  if message == "UTC Time?" then
  cbox.say("The time is: "..uTime)
end
end


Now, whenever a lazy person wants to know the time in UTC, all they have to do is type it in chat mid-conversation, as opposed to having to tab out to google it like a normal person. However, it will get extremely annoying if every time someone asks the chatbox for the time, it will flood the chat with information that is only relevant to two people at most at any given moment. I am aware that chatboxes can tell(), but I just can not figure out how to get the Chat Box to detect who asked for the time and tell() them instead of perpetually annoying the rest of the server by riddling say()'ing to everyone what one person asked for.

I would appreciate if someone could point me in the right direction.
Bomb Bloke #2
Posted 08 August 2016 - 12:35 PM
I am aware that chatboxes can tell(), but I just can not figure out how to get the Chat Box to detect who asked for the time and tell() them instead of perpetually annoying the rest of the server by riddling say()'ing to everyone what one person asked for.

Can you not simply use the name you assigned to the "player" variable?
Mantas #3
Posted 08 August 2016 - 12:40 PM
I'm very new to CC, Lua and the lot. That's exactly what I'm asking how to do. If player x sends message x, how do you get the Chat Box to reply to player x. I want it to be available to anyone in the server, so specifing a player = "player_name" isn't what I'm looking for. At least that's what I think you mean. If not, I'm a bit lost.

I originally thought if message == "message" and player ==… ? But I didn't know if that would work, and how it would work.

EDIT: I guess, what I'm looking for is something like (in actual code) if message == "message" and player == getPlayerName. or if message == "message" then cbox.tell(getPlayerName, "message")
Edited on 08 August 2016 - 10:57 AM
Bomb Bloke #4
Posted 08 August 2016 - 12:51 PM
You don't need to compare the player name to anything; if a given event indicated that any player said "Time in-game?", then call the "tell" method of whichever chat box it is you're using (there's a few!) with the name gathered when you pulled that event.

That is to say, all you need to do is change this:

cbox.say(string.char(167).."6"..string.char(167).."l".."The time is "..textutils.formatTime( T, false ).." on Day "..D)

… to something like this:

cbox.tell(player, string.char(167).."6"..string.char(167).."l".."The time is "..textutils.formatTime( T, false ).." on Day "..D)  -- Or whatever argument order your chat box uses

Though it'd pay to forget about your T / D variables. For one thing, setting them up outside the loop like that means they'll be assigned a time / day once, and will always report that same time / day whenever a user asks. You could move them into the loop but you only ever make use of them on one line - it'd be easier to just stick the os.time() / os.day() calls directly into that line.

A relatively minor nit-pick, instead of this sort of thing:

string.char(167).."6"..string.char(167).."l".."The time is "

… you can use the \ escape character to indicate symbols directly within a single string. Putting a number after it generates the related character:

"\1676\167l The time is "
Mantas #5
Posted 08 August 2016 - 01:09 PM
Well that was more obvious than I thought, I will definitely actually pay more attention to what I'm actually copying and what it means.

Also, this whole time I've indeed been typing

string.char(167).."6"..string.char(167).."l".."The time is "
Your minor-nit pick means a very big deal to me :D/>

Thank you, it all works, and I completely understand what I'm doing now. I'll pay more attention to what variables are within the program next time.
Edited on 08 August 2016 - 11:11 AM
DannySMc #6
Posted 08 August 2016 - 02:11 PM
Also a note on this you get the time before the while true loop (on the first example) so you don't get it each time, therefore the time will never update :)/>
Edited on 08 August 2016 - 12:12 PM
Mantas #7
Posted 12 August 2016 - 01:22 AM
Thank you, I have changed everything in concurrence to all the input I've received from you two. Now to figure out how that god-damned API formats time :P/>
TheRockettek #8
Posted 12 August 2016 - 08:33 AM
What mod is your chatbox from

How i would do it:


TimeZone = CET
Format = "%r %f"
chatBox = peripheral.find("chatBot")
function magic(text) -- So  i can easly just do &6Hello and it would format it to §6Hello.
  r,_ = string.gsub(text,"&","\194\167")
  return r
end
getTime = function(timezone,format)
	time = http.get("http://www.timeapi.org/"..string.upper(timezone).."/now?format=" ..  string.gsub(textutils.urlEncode(format),"+","%20")
	rtime = time.readAll()
	if not rtime then
	  return "Could not contact.")
   else
	 return rtime
   end
end
while true do
  local _,_,u,m = os.pullEvent("chat_message")
  if m == "Time ingame?" then
	igt = textutils.formatTime(os.time(),true)
	chatBox.say(magic("&6The time ingame is: &e" .. igt))
   elseif m == "UTC Time?" then
	 chatBox.say(nagic("&6The UTC time is: &e" .. getTime("UTC","&f %r"))
  end
end

I didnt test it (i should of) but that should work

Now to figure out how that god-damned API formats time :P/>/>

http://pastebin.com/7ZPjf1U8
Edited on 12 August 2016 - 12:13 PM
HDeffo #9
Posted 12 August 2016 - 05:02 PM
I actually know the server he is on cx far as I can tell the mod with chatboxes is a completely undocumented mod that isn't even listed on the forums so I would assume its a mod private to that server
TheRockettek #10
Posted 12 August 2016 - 06:38 PM
wuts the mods name d:
if you dont know, atleast like provide a screen shot of it or something ;)/>
justync7 #11
Posted 13 August 2016 - 02:05 AM
I actually know the server he is on cx far as I can tell the mod with chatboxes is a completely undocumented mod that isn't even listed on the forums so I would assume its a mod private to that server
wuts the mods name d:
if you dont know, atleast like provide a screen shot of it or something ;)/>
I would assume he is playing on SwitchCraft? MinimalPeripherals is my mod. (Yet to be released on the forums)

Using a chatbox is as simple as:

local chatbox = peripheral.wrap("side")
chatbox.say("message","label","prefix")
chatbox.tell("player","message","label","prefix")

Catching events from a chatbox is as simple as:

local chatbox = peripheral.wrap("side")
while true do
  local event, user, message = os.pullEvent("chat")
  print(user..": "..message)
  sleep(0)
end

Hope I helped :)/>

EDIT: Oh I forgot, the mod can be found here if you wish to see any of its code.
Edited on 13 August 2016 - 12:08 AM
TheRockettek #12
Posted 13 August 2016 - 08:16 AM
I actually know the server he is on cx far as I can tell the mod with chatboxes is a completely undocumented mod that isn't even listed on the forums so I would assume its a mod private to that server
wuts the mods name d:
if you dont know, atleast like provide a screen shot of it or something ;)/>/>
I would assume he is playing on SwitchCraft? MinimalPeripherals is my mod. (Yet to be released on the forums)

Using a chatbox is as simple as:

local chatbox = peripheral.wrap("side")
chatbox.say("message","label","prefix")
chatbox.tell("player","message","label","prefix")

Catching events from a chatbox is as simple as:

local chatbox = peripheral.wrap("side")
while true do
  local event, user, message = os.pullEvent("chat")
  print(user..": "..message)
  sleep(0)
end

Hope I helped :)/>/>

EDIT: Oh I forgot, the mod can be found here if you wish to see any of its code.

Does your chatbox support unicode?

If so, my magic function would work as it can use the section symbol which gives it colour :)/>/>