about summary refs log tree commit diff
path: root/pkg/bot/bot.go
blob: 091e1bee824d0a0455b9eed4d856b90a7ae78bb1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package bot

import (
	"context"
	"fmt"
	"jinx/pkg/discord"
)

type Bot struct {
	Client        *discord.Discord
	cancelContext context.CancelFunc
}

func Start(token string) (*Bot, error) {
	fmt.Println("hi..!")

	client := discord.NewClient(token)

	ctx, cancel := context.WithCancel(context.Background())

	fmt.Println("connecting..")
	if err := client.Connect(ctx); err != nil {
		cancel()
		return nil, err
	}

	fmt.Println("connected..")

	return &Bot{
		Client:        client,
		cancelContext: cancel,
	}, nil
}

func (b *Bot) Stop() error {
	b.cancelContext()
	return b.Client.Disconnect()
}