如何计算用Python收到的消息
我一直在使用代码教程found here来为Chatango聊天室创建一个bot。我的基本代码如下:如何计算用Python收到的消息
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,但我似乎无法找出原因这在每次有人发送消息时都不会打印出正确的计数。我怎样才能使这个计数器工作?
count是类对象“bot”的成员,如果你打算使用在类体内声明的count,那么以bot.count的形式访问它。 我没有看到您尝试使用的任何全局计数。
由于我没有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)
谢谢你的回应,但是当我尝试这个时,我的程序崩溃了。不幸的是,它让我崩溃太快,看看它会抛出什么错误。 – Patr3xion
self.count也是可以的,但你需要做一个'def __init __(self):self.count = 0'。 [本教程](http://learnpythonthehardway.org/book/ex40.html)可以帮助您使用此类变量版本。 –
这样子?创建一个名为“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)
我固定的一些东西所发生崩溃时,我之前使用的代码。但我不再使用它了。而在命令上,这是所有的房间!不只是一个房间,好吗?
我应该在类外声明''count'吗?这会使它成为全球性的吗? – Patr3xion
如果你想使用全局计数,那么是,在类之外声明它。 如果你想使用count作为类的成员,然后使用bot.count。 但我会建议正确地设计它,决定如果你需要一个全局计数,类对象的数量或实例对象的数量 – AlokThakur
我实现了这个想法,它已经完成了我的目标。感谢您的帮助。 – Patr3xion