package discordbot

import (
	"fmt"

	"github.com/bwmarrin/discordgo"
)

type DiscordBot struct {
	Session *discordgo.Session
}

func NewDiscordBot(token string) (*DiscordBot, error) {
	discord, err := discordgo.New("Bot " + token)
	if err != nil {
		return nil, err
	}
	return &DiscordBot{
		Session: discord,
	}, nil
}

func (dbot *DiscordBot) AddHandler(handlerFunc func(*discordgo.Session, *discordgo.MessageCreate)) *DiscordBot {
	dbot.Session.AddHandler(handlerFunc)
	return dbot
}

func (dbot *DiscordBot) Open() error {

	if err := dbot.Session.Open(); err != nil {
		return fmt.Errorf("cannot open the session: %v", err)
	}
	return nil
}

func (dbot *DiscordBot) Close() {
	dbot.Session.Close()
}
