Closed BarelyMiSSeD closed 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:
othertext
is not recommended. In this case the garbage collector will take care of it, but in other situations it could mess up stuff. I recommend using the with
statement when you deal with files.global
keyword does nothing here.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.
Thanks Man.. That worked.. .You are AWESOME!!!!
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)
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!!