如何统计 Python 收到的消息

How to count messages received with Python

我一直在使用代码教程 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 的新手,但我似乎无法弄清楚为什么每次有人发送消息时这不会打印出正确的计数。我怎样才能使这个计数器工作?

count 是 class 对象 "bot" 的成员,如果您打算使用在 class 主体内声明的计数,则以 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)

像这样?创建一个名为 "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)

我修复了之前使用代码时发生崩溃的一些问题。不过我不再使用它了。而且一吩咐,全是房间!不只是一个房间,好吗?