[Hexchat] Scripting in Hexchat... reddit / irc ---------------------------------------------------------------------- Drachenwulf · 2022-01-16 01:36 I am trying to figure out how to script some commands. in particular an on join command to set a particular mode to the one channel I own.... someone in my other thread suggested scripting it would work better. so can someone please help me figure this out? ---------------------------------------------------------------------- Gods_of_Sourdough · 2022-01-16 03:25 [https://hexchat.readthedocs.io/en/latest/script\_python.html](https:/ /hexchat.readthedocs.io/en/latest/script_python.html) ---------------------------------------------------------------------- MatanzaCueto · 2022-01-16 05:43 Is hexchat based on mirc scripting language? Then I can help you :) ---------------------------------------------------------------------- ProgVal · 2022-01-16 12:13 > on join command [create a hook](https://hexchat.readthedocs.io/en/latest/script_python .html#hexchat.hook_server) on the "JOIN" command > to set a particular mode [Send a command](https://hexchat.readthedocs.io/en/latest/script_pytho n.html#hexchat.command) with the given mode. For example, `hexchat.command("mode #channel +o foobar")` if you want to op `foobar`. ---------------------------------------------------------------------- skizzerz1 · 2022-01-16 17:31 Scripting in HexChat is done via real programming languages. The easiest two to pick up if you aren't already familiar with programming are Lua and Python, so I would recommend one of those. I personally view Lua as a bit more approachable between the two so that's what I'll use in my reply (additionally, if you're running HexChat on Windows, Lua is guaranteed to be available whereas Python may not be available depending on how HexChat was installed). For developing Lua scripts, there are two main points of documentation: - [HexChat's Lua Interface documentation](https://hexchat.readthedocs. io/en/latest/script_lua.html) - [The Lua 5.1 reference manual](https://www.lua.org/manual/5.1/) The first thing you'll need to do when creating a new script is have a code editor installed. I suggest [VS Code](https://code.visualstudio.com/) for this, although plenty of alternative options exist. Create a new empty file in your editor and save it into your HexChat's addons folder as `joincommands.lua` (or whatever other name you want, but you will need the `.lua` file extension). By saving the empty file with the `.lua` file extension, VS Code will recognize that the file is meant to be Lua code and offer features such as syntax highlighting and code autocompletion. You may be asking where is your HexChat's addons folder? Here's how to find it: - On Windows: `C:\Users\yourusername\AppData\Roaming\HexChat\addons` - On Linux: `/home/yourusername/.config/hexchat/addons` (If you are using the Store version on Windows, or the Flatpak/Snap version on Linux, the paths will be different. [See here for the full listing of paths](https://hexchat.readthedocs.io/en/latest/settings.html#config- files); in each case you will want to go into the addons sub-folder of what is listed in that link) Having the file saved in HexChat's addons folder means it will be automatically loaded each time HexChat starts up, which is why we want to save it there as opposed to elsewhere! Now that you have the file, it's time to put the base structure of the code. Put the following in there: hexchat.register("joincommands.lua", "1.0", "Run commands automatically after joining channels") hexchat.hook_server("JOIN", function (word, word_eol) -- PLACEHOLDER FOR CODE end) The top line registers your plugin with HexChat by giving it the plugin's name, the plugin's version, and the plugin's description. You can customize all three of these to your liking, however make sure to keep them as *strings*. In Lua, a *string* is text that is enclosed by *double quotes*. If you forget to put quotes around your string, the code will be invalid. The second line tells HexChat to run a function (a block of code) whenever it receives a `JOIN` from the server. In the IRC protocol, each time you `/join` a channel, the server will confirm that your join was successful by sending a `JOIN` back to the client. So, we listen in on that successful join message to run stuff. This guarantees that we are joined to the channel before we attempt to run any commands on it. The placeholder bit starts with two hyphens (`--`). This indicates a *comment* in Lua. Comments are not run as code, and can be put anywhere in the file to help explain things or provide any other annotations you may desire. In this tutorial, I'm just using them as placeholders for things we'll fill in later. Next, we need to actually fill in that placeholder with what we want to do. Our code will need to do the following: 1) verify that we are only running the command when joined to the correct channel, and then 2) actually run the command. Let's do that now. To get our current channel (aka the channel that we just joined), we can call `hexchat.get_info("channel")`. We'll save that into a *local variable* so that we can reference it multiple times as needed: local channel channel = hexchat.get_info("channel") The top line declares `channel` as a *local variable*. The second line *assigns* the current channel name into the `channel` variable. Assignment is done with a *single equals sign*. Next we'll test if it's the correct channel: if channel == "#mycoolchannel" then -- PLACEHOLDER FOR THING TO DO ON CORRECT CHANNEL end This is doing a *comparison* between the `channel` variable and the *string* with the value #mycoolchannel. If the two are equivalent, we run the placeholder code after the `then`. Equals comparisons are done with *two equals signs*. Finally, we need to actually do what we set out to do in the first place: set a mode on the channel after we join it. Let's assume that we want to set the `-s` mode to make our channel less secret, since you never specified what you were actually trying to set. You can of course replace the command with whatever you actually need or want to do: hexchat.command("mode -s") You **do not** include the initial / when calling `hexchat.command()`. These commands can be anything that HexChat supports as a slash command (so mode, msg, etc.) and are interpreted exactly as you typed them into the input box on the channel in question. Since HexChat knows that the current channel is the one we just joined, we don't need to explicitly specify a channel name in the /mode command. Finally, we'll tell HexChat to continue processing other plugins as well as do whatever it internally needs to do whenever it receives a `JOIN` event from the server. To do this, at the very end of the function (after the `end` of the if block) we tell it to "eat nothing" (if everything is eaten, nothing else is run. If nothing is eaten, everything else is run): return hexchat.EAT_NONE So, to put it all together: hexchat.register("joincommands.lua", "1.0", "Run commands automatically after joining channels") hexchat.hook_server("JOIN", function (word, word_eol) local channel channel = hexchat.get_info("channel") if channel == "#mycoolchannel" then hexchat.command("mode -s") end return hexchat.EAT_NONE end) Now your script is done and you can load it in! It will be automatically loaded each time HexChat starts, or you can manually load it by going to Window -> Plugins and Scripts in the menu bar. ---------------------------------------------------------------------- Drachenwulf · 2022-01-17 05:21 thank you this works, but if I just have a server interruption, when I re connect it doesn't... which confuses me because if I disconnect, I leave the channel that I own and then I Join when I reconnect... edit for elaboration, for some reason this doesn't work every time and I don't know why ---------------------------------------------------------------------- PastinOBravO187 · 2022-08-20 05:57 how do I change the time stamp in Hexchat?