import xchat
hooker = None

__module_name__ = "Away Script"
__module_version__ = '1.0'
__module_description__ = "States that your away"


def away(word, word_eol, userdata):
	chan = xchat.get_info('channel')
	xchat.command("msg " + chan + " I'm Away")
def toggle(word, word_eol, userdata):
	global a
	if word[1] == 'on':
		xchat.hook_print("Channel Msg Hilight", away)
		xchat.command("echo Loaded")
	elif word[1] == 'off':		
		xchat.unhook(a)
		hooker = None
		xchat.command("echo Unloaded")
	
	else:
		xchat.command("echo Use /a on or /a off")
	return xchat.EAT_ALL
xchat.hook_command("a", toggle)
a = xchat.hook_print("Channel Msg Hilight", away)
xchat.command("a off")

Dani AI

Generated

— the symptom you described ("/a on" works but "/a off" does not) is almost always a hook-handle and scope problem.

xchat.hook_print returns a hook handle that must be saved and later passed to xchat.unhook. In the posted flow a handle created when you enable the hook is not being stored back into the module-level variable that toggle uses when unhooking. Also assigning to a name inside a function without declaring it global creates a local variable, so the global handle never changes. Finally, calling your toggle command at import time (the trailing xchat.command("a off")) adds confusion during testing.

Fix checklist:

  • Initialize a single module-level hook variable to None.
  • Inside the toggle function declare that name global before assigning to it.
  • When enabling, save the value returned by xchat.hook_print into that global variable.
  • When disabling, only call xchat.unhook if the stored handle is not None, then set it back to None.
  • Validate command arguments (check len(word) before using word[1]) so /a with no args does not raise IndexError.

Extra tips: use a clearer name than a (for example hook_id) and echo or print the handle while debugging so you can confirm it changes. Make /a on idempotent (do nothing if the hook is already set) to avoid creating multiple hooks. Remove the automatic "/a off" at import; it hides whether your enabling/disabling works interactively.

Recommended Answers

All 2 Replies

What are you asking for help with?

I forgot to ask my question, oops. Why is it that once I tyurn it on using /a on, I cant turn it off using /a off?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.