如何计算使用 Python 接收的消息



我一直在使用这里的代码教程为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

是类对象 "bot" 的成员,如果您打算使用在类体内声明的 count,则将其作为 bot.count 访问。我没有看到您尝试使用的任何全局计数。

因为我没有ch。房间管理器(和一个帐户),我无法测试它。全局变量也应该有效:

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)

我修复了一些在我之前使用代码时碰巧崩溃的东西。不过我不再使用它了。根据命令,这是所有房间!不只是一个房间,好吗?

相关内容

  • 没有找到相关文章

最新更新