-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (91 loc) · 3.58 KB
/
main.go
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2022-2023 Kesuaheli
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
logger "log"
"os/signal"
"syscall"
"cake4everybot/config"
"cake4everybot/database"
"cake4everybot/event"
"cake4everybot/webserver"
"github.com/bwmarrin/discordgo"
"github.com/kesuaheli/twitchgo"
"github.com/spf13/viper"
)
var log = logger.New(logger.Writer(), "[MAIN] ", logger.LstdFlags|logger.Lmsgprefix)
const banner string = "\n" +
" ______ __ __ __ ______ \n" +
" / ____/___ _/ /_____ / // / / ____/ _____ _______ ______ ____ ___ \n" +
" / / / __ `/ //_/ _ \\/ // /_/ __/ | | / / _ \\/ ___/ / / / __ \\/ __ \\/ _ \\\n" +
"/ /___/ /_/ / ,< / __/__ __/ /___ | |/ / __/ / / /_/ / /_/ / / / / __/\n" +
"\\____/\\__,_/_/|_|\\___/ /_/ /_____/ |___/\\___/_/ \\__, /\\____/_/ /_/\\___/ \n" +
" /____/ \n" +
" ____ _ __ ____ __ \n" +
" / __ \\(_)_____________ _________/ / / __ )____ / /_ \n" +
" / / / / / ___/ ___/ __ \\/ ___/ __ / ______ / __ / __ \\/ __/ \n" +
" / /_/ / (__ ) /__/ /_/ / / / /_/ / /_____/ / /_/ / /_/ / /_ \n" +
" /_____/_/____/\\___/\\____/_/ \\__,_/ /_____/\\____/\\__/ \n" +
"\n" +
"Version: v%s\n" +
"%s\n" +
"Copyright 2022-2023 Kesuaheli\n\n"
func init() {
config.Load("config.yaml")
}
func main() {
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
defer stop()
webChan := make(chan struct{})
log.Printf(banner, viper.GetString("version"), viper.GetString("discord.credits"))
database.Connect()
defer database.Close()
// initializing Discord and Twitch bots
discordBot, err := discordgo.New("Bot " + viper.GetString("discord.token"))
if err != nil {
log.Fatalf("invalid discord bot parameters: %v", err)
}
discordBot.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in to Discord as %s#%s\n", s.State.User.Username, s.State.User.Discriminator)
})
twitchBot := twitchgo.New(viper.GetString("twitch.name"), viper.GetString("twitch.token"))
// adding listeners for events
event.AddListeners(discordBot, twitchBot, webChan)
// open connection and login to Discord and Twitch
log.Println("Logging in to Discord")
err = discordBot.Open()
if err != nil {
log.Fatalf("could not open the discord session: %v", err)
}
defer discordBot.Close()
log.Println("Logging in to Twitch")
err = twitchBot.Connect()
if err != nil {
log.Fatalf("could not open the twitch connection: %v", err)
}
defer twitchBot.Close()
// register all events.
err = event.PostRegister(discordBot, twitchBot, viper.GetString("discord.guildID"))
if err != nil {
log.Printf("Error registering events: %v\n", err)
}
log.Println("Starting webserver...")
addr := ":8080"
webserver.Run(addr, webChan)
// Wait to end the bot
log.Println("Press Ctrl+C to exit")
<-ctx.Done()
log.Println("\nGracefully shutting down. Byee")
}