handlename's blog

コード片など

scalaでirc botを書く場合

Javaのライブラリを使うのがらくちんみたいだ。 PircBotを使う場合はこんな感じ。

package com.example.IRCBot

import org.jibble.pircbot._

class Bot() extends PircBot {
  override def onMessage(
    channel: String,
    sender: String,
    login: String,
    host: String,
    message: String) {
    message match {
      case m if m.startsWith(this.getName) => this.sendMessage("hi")
      case _ =>
    }
  }
}

object SimpleBotApp extends App {
  val bot = new Bot()
  bot.setName("echo-bot")
  bot.setEncoding("utf-8")
  bot.connect("irc.example.com", 6667)
  bot.joinChannel("#hoge")
}