我一直在使用代码教程found here为Chatango聊天室制作机器人。我的基本代码如下:
import ch
import random
import sys
class bot(ch.RoomManager):
def onInit(self):
self.setNameColor("000000")
self.setFontColor("000000")
self.setFontFace("Ariel")
self.setFontSize(11)
def onMessage(self, room, user, message):
print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
try:
cmd, args = message.body.split(" ", 1)
except:
cmd, args = message.body, ""
if cmd[0] == "!":
prfx = True
cmd = cmd[1:]
else:
prfx = False
rooms = ["room0", "room1"]
username = "username goes here"
password = "password goes here"
bot.easy_start(rooms,username,password)
它基本上就是我在上面链接的网站上看到的内容。在过去的几天里使用它,我知道代码有效。我想要实现的是一种在消息进入时对消息进行计数的方法。我已经尝试过以下方面:
import ch
import random
import sys
class bot(ch.RoomManager):
count = 0
def onInit(self):
self.setNameColor("000000")
self.setFontColor("000000")
self.setFontFace("Ariel")
self.setFontSize(11)
def onMessage(self, room, user, message):
print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
global count
count = count + 1
print (count)
try:
cmd, args = message.body.split(" ", 1)
except:
cmd, args = message.body, ""
if cmd[0] == "!":
prfx = True
cmd = cmd[1:]
else:
prfx = False
rooms = ["room0", "room1"]
username = "username goes here"
password = "password goes here"
bot.easy_start(rooms,username,password)
我是Python的新手,但我似乎无法弄清楚为什么每次有人发送消息时都不会打印出正确的计数。我怎样才能使这个计数器工作?
答案 0 :(得分:0)
count是类对象“bot”的成员,如果你打算使用在类体内声明的count,那么将其作为bot.count访问。 我没有看到您尝试使用的任何全局计数。
答案 1 :(得分:0)
由于我没有ch.RoomManager(和一个帐户),我无法测试它。全局变量也应该起作用:
import ch
import random
import sys
# Initiate count
count = 0
class bot(ch.RoomManager):
def onInit(self):
self.setNameColor("000000")
self.setFontColor("000000")
self.setFontFace("Ariel")
self.setFontSize(11)
def onMessage(self, room, user, message):
print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
global count
count = count + 1
print (count)
try:
cmd, args = message.body.split(" ", 1)
except:
cmd, args = message.body, ""
if cmd[0] == "!":
prfx = True
cmd = cmd[1:]
else:
prfx = False
rooms = ["room0", "room1"]
username = "username goes here"
password = "password goes here"
bot.easy_start(rooms,username,password)
答案 2 :(得分:0)
喜欢这个?创建一个名为" log.txt"的文件。并复制以下内容:
import ch
import random
import sys
class bot(ch.RoomManager):
def onInit(self):
self.setNameColor("000000")
self.setFontColor("000000")
self.setFontFace("Arial")
self.setFontSize(11)
def onMessage(self, room, user, message):
f = open("log.txt", "a") # "a" means append/add
f.write("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body)+"\n")
f.close()
print("[{0}] {1}: {2}".format(room.name, user.name.title(), message.body))
try:
cmd, args = message.body.split(" ", 1)
except:
cmd, args = message.body, ""
if len(cmd) > 0:
if cmd[0] == "!":
prfx = True
cmd = cmd[1:]
else:
prfx = False
else:
return
if cmd == "say" and prfx:
if args: room.message(args)
else: room.message("Nothing to say!")
elif cmd == "count" and prfx:
room.message("Numbers of messages sent in all rooms I am in: "+str(len(open("log.txt").readlines())))
rooms = ["room0", "room1"]
username = "username goes here"
password = "password goes here"
bot.easy_start(rooms,username,password)
我修复了以前使用代码时发生崩溃的一些事情。我不再使用它了。在命令上,它是所有房间!不只是一个房间,好吗?