Esempio n. 1
0
// NewBot creates a Bot for a given connection and set of handlers.
func NewBot(config *config.Config, connector Connector) *Bot {
	sqlDB, err := sql.Open("sqlite3", config.DB.File)
	if err != nil {
		log.Fatal(err)
	}

	logIn := make(chan Message)
	logOut := make(chan Messages)

	RunNewLogger(logIn, logOut)

	users := []User{
		User{
			Name: config.Nick,
		},
	}

	bot := &Bot{
		Config:         config,
		Plugins:        make(map[string]Handler),
		PluginOrdering: make([]string, 0),
		Conn:           connector,
		Users:          users,
		Me:             users[0],
		DB:             sqlDB,
		logIn:          logIn,
		logOut:         logOut,
		Version:        config.Version,
		httpEndPoints:  make(map[string]string),
	}

	bot.migrateDB()

	http.HandleFunc("/", bot.serveRoot)
	if config.HttpAddr == "" {
		config.HttpAddr = "127.0.0.1:1337"
	}
	go http.ListenAndServe(config.HttpAddr, nil)

	connector.RegisterMessageReceived(bot.MsgReceived)
	connector.RegisterEventReceived(bot.EventReceived)

	return bot
}
Esempio n. 2
0
// Newbot creates a bot for a given connection and set of handlers.
func New(config *config.Config, connector Connector) Bot {
	sqlDB, err := sqlx.Open("sqlite3_custom", config.DB.File)
	if err != nil {
		log.Fatal(err)
	}

	logIn := make(chan msg.Message)
	logOut := make(chan msg.Messages)

	msglog.RunNew(logIn, logOut)

	users := []user.User{
		user.User{
			Name: config.Nick,
		},
	}

	bot := &bot{
		config:         config,
		plugins:        make(map[string]Handler),
		pluginOrdering: make([]string, 0),
		conn:           connector,
		users:          users,
		me:             users[0],
		db:             sqlDB,
		logIn:          logIn,
		logOut:         logOut,
		version:        config.Version,
		httpEndPoints:  make(map[string]string),
	}

	bot.migrateDB()

	http.HandleFunc("/", bot.serveRoot)
	if config.HttpAddr == "" {
		config.HttpAddr = "127.0.0.1:1337"
	}
	go http.ListenAndServe(config.HttpAddr, nil)

	connector.RegisterMessageReceived(bot.MsgReceived)
	connector.RegisterEventReceived(bot.EventReceived)

	return bot
}