MinoMino / minqlbot

An administration tool for the Quake Live client, extensible with plugins.
GNU General Public License v3.0
26 stars 10 forks source link

Fun.py #3

Closed BarelyMiSSeD closed 9 years ago

BarelyMiSSeD commented 9 years ago

I am trying to get a random response from the bot when someone types the bot's name (other). I edited the code in the fun.py but I can't get it to work. Any ideas? Here is the code from the file:

import minqlbot

import random

class fun(minqlbot.Plugin): def init(self): self.add_command("cookies", self.cmd_cookies) self.add_command("<3", self.cmd_heart, channels=("chat", "team_chat", "tell")) self.add_command("stfu", self.cmd_stfu) self.add_command("other", self.cmd_other) self.add_command("flowers", self.cmd_flowers) self.add_command("lies", self.cmd_lies) self.add_command("foc", self.cmd_foc)

def cmd_cookies(self, player, msg, channel):
    channel.reply("^7For me? Thank you, {}!".format(player))

def cmd_heart(self, player, msg, channel):
    s = ("^1\r oo   oo"
         "\no  o o  o"
         "\no   o   o"
         "\n o     o"
         "\n  o   o"
         "\n   o o"
         "\n    o")
    channel.reply(s.replace("o", "\x08"))

def cmd_stfu(self, player, msg, channel):
    channel.reply("^1Fuck You ^2{}^1! ^7You can't stop my ^4POWER^1!!".format(player))

def cmd_other(self, player, msg, channel):
    fo = open("other.txt","r")
    ln = sum(1 for line in fo)
    sl = random.randrange(1, ln + 1)
    fo.close()
    global othertext
    othertext = open("other.txt","r").readline(sl)
    channel.reply(othertext.format(player))
    #othertext.close()

def cmd_flowers(self, player, msg, channel):
    channel.reply("^7Awww, ^1Flowers ^7are so thoughtful {}^7. Still no sex for you!".format(player))

def cmd_lies(self, player, msg, channel):
    channel.reply("^1LIES?!? ^7Who is full of lies? {} ^7You should kick the liar!".format(player))

def cmd_foc(self, player, msg, channel):
    channel.reply("^1F^7.O.^4C^7 The ^1BEST^7 clan in the ^2World^1!^7!^4!".format(player))

The part that doesn't work is the cmd_other. I worked in the python IDLE command line and I was able to verify that I can read a random line from a file using the code I have in that command, but it doesn't work when I put the code in here. Does it have something to do with "channel.reply(othertext.format(player))"?

Here is a sample line from the other.txt file: ^7I am just a bot {}^7. I can’t give your life meaning!!

MinoMino commented 9 years ago

I'm not sure what you did in IDLE to make it work, but the code you gave me won't give you a random line. It'll give you a random number of characters from the first line. When you open a file, there's a cursor pointing at the first character of the file. When you do readline(), it'll start reading from the cursor until it encounters a newline. If you pass an integer x to it, it'll read x number of characters or until a newline is found.

Also, a couple of tips:

Anyway, here's how I'd do it:

[...]
def __init__(self):
[...]
    self.add_command("other", self.cmd_other)

    with open("other.txt", "r") as f:
        self.lines = f.readlines()

def cmd_other(self, player, msg, channel):
    i = random.randrange(len(self.lines))
    channel.reply(self.lines[i].format(player).rstrip())

If you would rather have it trigger when the name is said anywhere in chat rather than having to do !other, you could do the following:

[...]
def __init__(self):
[...]
    self.add_hook("chat", self.handle_chat)

    with open("other.txt", "r") as f:
        self.lines = f.readlines()

def handle_chat(self, player, msg, channel):
    split_msg = msg.lower().split()
    if minqlbot.NAME.lower() not in split_msg:
        return

    i = random.randrange(len(self.lines))
    channel.reply(self.lines[i].format(player).rstrip())

Both of these read the file only when the plugin is loaded rather than every single time the command triggers, making it a lot more efficient.

Finally, I also see you use "other.txt" when you open the file. Note that the working directory is the same folder quakelive.exe is in. This means that you'd have to make it read-only, or the launcher's gonna keep deleting it. I recommend you switch to either an absolute path to somewhere else, or simply use "python/other.txt" and put it in the python folder with the config and whatnot.

BarelyMiSSeD commented 9 years ago

Thanks Man.. That worked.. .You are AWESOME!!!!