Actions on Idle

Posted by Traz on Tue 27 Mar 2018 02:15 AM — 6 posts, 23,027 views.

#0
I was looking to see if it was possible to do some actions after idling for a set period of time. I looked around and found the plugin that was made for marking yourself as AFK, but I would like to do something different.

An example is after about 2 minutes of idle time, I'd like to execute my sleep alias.

Also, the plugin used OnPluginCommand which could tell when something was typed. I'd like something that can also check if triggers are going off so it doesn't decide to idle while I'm doing something automated.

Any help would be appreciated.
Australia Forum Administrator #1

First check out plugin callbacks. In particular OnPluginSent. That lets you know that something has been sent to the MUD (by you or by some script).

What you could do is have a 2-minute timer. When that timer fires you can do your “idle” stuff like sleeping. However to prevent it firing while you are doing stuff, in the plugin callback OnPluginSent you call ResetTimer which resets the timer so it goes back to timing 2 minutes again.

#2
Ok, so I tried this, but the timer kept counting down, not caring that something was going on.


<timers>
  <timer name="IdleSense" minute="5" second="0.00" offset_second="0.00"    send_to="12"
group="NecroM" >
  <send>
function OnPluginSent (sText)
ResetTimer ("IdleSense")
end
Execute ("sleep")
</send>

  </timer>
</timers>


Edit: Just as a side note, the timer is off currently because I couldn't get it to work, so that is not the reason it doesn't work.

Edit2: I think you might have misunderstood and thought I was making a plugin. I was just referring to the AFK plugin. I can see why what I did won't work but I don't know how to make it work.
Amended on Tue 27 Mar 2018 03:49 AM by Traz
Australia Forum Administrator #3
You can't shove the OnPluginSent function into something that a timer calls and hope for it to work. (Well, you can hope, but it won't!).

Writing a plugin is quite easy, there is a plugin wizard for that.

Here is a simple plugin that does what you want:

Template:saveplugin=Do_Something_When_Idle
To save and install the Do_Something_When_Idle plugin do this:
  1. Copy the code below (in the code box) to the Clipboard
  2. Open a text editor (such as Notepad) and paste the plugin code into it
  3. Save to disk on your PC, preferably in your plugins directory, as Do_Something_When_Idle.xml
    • The "plugins" directory is usually under the "worlds" directory inside where you installed MUSHclient.
  4. Go to the MUSHclient File menu -> Plugins
  5. Click "Add"
  6. Choose the file Do_Something_When_Idle.xml (which you just saved in step 3) as a plugin
  7. Click "Close"
  8. Save your world file, so that the plugin loads next time you open it.




<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>

<muclient>
<plugin
   name="Do_Something_When_Idle"
   author="Nick Gammon"
   id="3dd3eb0ecf9eb5b183c98fec"
   language="Lua"
   purpose="Send a sleep command if you are idle"
   date_written="2018-03-27 15:34:55"
   requires="4.90"
   version="1.0"
   >
<description trim="y">
<![CDATA[
Sends "sleep" if you are idle for 5 minutes.
]]>
</description>

</plugin>


<!--  Timers  -->

<timers>
  <timer name="IdleSense"
          enabled="y"
          minute="5"
          second="0.00"
          send_to="12"
          group="NecroM" >
  <send>
  if not timerFired then
    Execute ("sleep")
    timerFired = true
  end -- if not already done it
  </send>

  </timer>
</timers>

<!--  Script  -->

<script>
<![CDATA[
function OnPluginSent (sText)
  ResetTimer ("IdleSense")
  timerFired = false
end
]]>
</script>

</muclient>


Testing showed an interesting problem. The timer would keep firing every 5 minutes, attempting to put you to sleep again. So what this does is remember when you send the "sleep" command so you don't do it again. This "remembering" is cleared when you send something new.

This has to be done after sending "sleep" otherwise that counts as doing something new.
Amended on Tue 27 Mar 2018 07:46 AM by Nick Gammon
#4
The problem is, I want to be able to turn it off and potentially use this same concept in different scenarios. Can you turn off/on a plugin with an alias?
Australia Forum Administrator #5
You can certainly disable plugins with an alias.

Template:function=EnablePlugin
EnablePlugin

The documentation for the EnablePlugin script function is available online. It is also in the MUSHclient help file.



What you could do is make a "sleep" alias which catches your attempt to sleep and do it conditionally. Or, if you don't want to lose the sleep command, make the plugin send Execute something random like "ebf9035ff8421255fc1c68d8" and then make an alias to match that. You can then do something in that alias like:


if I_want_to_sleep then
  Send "sleep"
end -- if