-1

I have a hunch that the answer to this is embarassingly easy, but nonetheless I can't figure it out (the fact that I don't know any of these languages at all might be the case). What I need is a script which would work this way:

  1. First, you type command like !random and a number from the range 1-100 (the number would mean the probability of success in %) [like this: !random 78]
  2. Then, it would - basing on given probability - choose whether you succeeded or not [for example, with !random 78, there is 78% probability that the outcome would be "Success"]
  3. Then, it would show on a channel a public message what the outcome is ("Success" or "Failure")

I need this one for online text RPG sessions. Also sorry for my bad English.

How the code looks now:

    __module_name__ = "HexChat Randomiser"
    __module_version__ = "0.1"
    __module_description__ = "A randomiser for HexChat."
      
    import random
    import xchat

def callback(word, world_eol, userdata):
    number = word[1]
    if random_chance(number):
        print ("Success")
    else:
        print ("Failure")


    def random_chance(percent_chance):
        return random.randrange(1, 101) > (100 - percent_chance)


    xchat.hook_command("random", callback, help="/random <number>")

The error:

Traceback (most recent call last):
File "<string>", line 10, in callback
File "<string>", line 17, in random_chance
TypeError: unsupported operand type(s) for -: 'int' and 'str'

1 Answers1

1

First you may want to have a look at the Python or Perl documentation for hexchat.

If you want to proceed in python I've wrote a small script to get you started:

import random
import xchat

def callback(word, world_eol, userdata):
    number = word[1]
    if random_chance(number):
        print "Success"
    else:
        print "Failure"


def random_chance(percent_chance):
    return random.randrange(1, 101) > (100 - int(percent_chance))
       

xchat.hook_command("random", callback, help="/random <number>")

You will have to get it to work in hexchat yourself. To load the script you will have to first save it some where then call the load command:

load

Load a script with given filename. /load will also work.

Community
  • 1
  • 1
Noelkd
  • 7,686
  • 2
  • 29
  • 43
  • Thank you very much! I changed a few things in the code so it now loads properly in HexChat. Unfortunately, when I type /random command, an error occurs. I pasted it into my original post. – MiddayHobo Feb 24 '15 at 14:54
  • You are getting a not getting a number but a string, you need to turn it into an int for it to work. change `100 - percent_chance` to `100 - int(percent_chance)`. I'm not even sure `word[1]` is the number, that's why i said you'd have to do the leg work yourself. – Noelkd Feb 24 '15 at 14:59
  • Last thing: how to display the command I just used (for example "/random 65") in a line before "Success" and "Failure", so that other users would know that I actually used it and not just manually typed the outcome? – MiddayHobo Feb 24 '15 at 15:37
  • [rtfm](http://hexchat.readthedocs.org/en/latest/script_python.html#word-and-word-eol) – Noelkd Feb 24 '15 at 15:41