Colors can be either 1 or 2 integers, so if you intend ^4123 to show the number 123 in red, it needs to be zero-padded to 2 digits.

The only time doublequotes are not included in the string the way you're doing it is that var %var = "" sets the var to the empty string. A variable cannot end with a single space unless using the -p switch.

Instead of checking if it's one of the colors not allowed, I just have a list of zero-padded numbers that are allowed, and I hardcoded my $rand call to use the number of items in the list. (Your 'rand' alias is never seen because a built-in identifier is always seen before a scripted alias)

Instead of making 3 checks against the 3 previous colors, I just toss back the random color if it's in the list. And I can't have a false match because I'm comparing 2 digits against 2 digits that are separated by spaces.

Note that if you comment out the line having the special handling for spaces, this will show your message as if it has consecutive spaces because they all have colors between them.

I added a check to see if your input began with a '/' because otherwise this would be showing your editbox /command to channel. The $ctrlenter is if you want to be able to selectively exclude messages from being colorized by pressing Ctrl+Enter together. The $inpaste doesn't trigger if you have editbox lines set to auto.

Things to consider if you use this for sending actual messages to actual channels:

* This acts in all channels, and some of them hate colors and will ban you for it. Some channels have a setting that strips colors from messages before displaying them, while other channels have a different flag that completely blocks a message containing any colors. You can restrict it by changing from :#: to :#chan1,#chan2: without spaces.

* By now, the 99 colors are supported almost universally except for those still using old versions. Other users may not see colors 00-15 the same way because they can define them to any 24-bit RGB they wish. But on the other hand, colors 16-98 are standardized as specific color shades that cannot be changed, so you can use color 52 and be guaranteed that they see red except those using versions older than v7.52 from 2018.

* You're assuming that everyone has the same background color as you do, or that they have modified the RGB for colors 0-15 so they work against whatever is their chosen background color. For example, the default color 12 contrasts well against white, but not against blackground. So the only way to get it to contrast against black is to change the RGB into a light-blue shade that contrasts against black, but doesn't look good against white. Plus, there's the problem of wanting to see a 'good blue' for windows where I set the background color, and the problem trying to find blue-ish shades for 02 12 10 11 that all contrast against black while still still allowing you to tell the 4 apart.

One way to solve the contrast problem is to begin your message by beginning your message with the color code that sets it to a specific background color. But then, people can be annoyed to see your messages showing up as a different background stripe than all the other.

* If people find this annoying, you might want to instead have the behavior be normal most of the time, but only changes to be colorized if you DO press ctrl+enter, such as changing the 1st line to be:

if (!$ctrlenter) return

Or, you can have an alias to only do this when you have an editbox command, where you'd put this code into the '/colormytext' alias instead of intercepting ON INPUT

Code
on *:INPUT:#:{
  if ((/* iswm $1) || ($ctrlenter) || ($inpaste) ) return
  var %i = 0, %str, %colors 04 06 07 08 09 10 11 12 13 , %recentcolors
  while (%i < $len($parms)) {
    inc %i
    var -p %char $mid($parms,%i,1)
    if (%char == $chr(32)) { var -p %str $+(%str,$v2) | continue }
    while ($gettok(%colors,$rand(1,9),32) isin %recentcolors) noop
    var %recentcolors $gettok($v1 %recentcolors,1-3,32)
    var -p %str $+(%str,$chr(3),$v1,%char)
  }
  !msg $chan %str
  halt
}