~kris/9p

glenda

ref: fed0daaacea34ffac16836b16496badafa92c67a glenda/main.go -rw-r--r-- 2.1 KiB
fed0daaa — arwn add invite 4 years ago
                                                                                
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
package main

import (
	"flag"
	"fmt"
	"github.com/bwmarrin/discordgo"
	"github.com/henesy/glenda/x/mux"
	"log"
	"os"
	"os/signal"
	sc "strconv"
	"syscall"
	"time"
)

const Version = "v0.1.1"

// Session is declared in the global space so it can be easily used
// throughout this program.
// In this use case, there is no error that would be returned.
var Session, _ = discordgo.New()

// Read in all configuration options from both environment variables and
// command line arguments.
func init() {

	// Discord Authentication Token
	// Have to prefix "Bot [Token Here]" or 401 Forbidden
	Session.Token = os.Getenv("DG_TOKEN")
	if Session.Token == "" {
		flag.StringVar(&Session.Token, "t", "", "Discord Authentication Token")
	}
}

func main() {

	// Declare any variables needed later.
	var err error

	// Print out a fancy logo!
	fmt.Printf(` 
            __
           (  \
      __   \  '\
     (  "-_ \ .-'----._
     '-_  "v"         "-
	"Y'             ".
	 |                |
	 |        o     o |
	 |          .<>.  |
	  \         "Ll"  |
	   |             .'
	   |             |
	   (             /
	  /'\         . \
	  "--^--.__,\_)-'   %-16s\/`+"\n\n", Version)

	// Parse command line arguments
	flag.Parse()

	// Verify a Token was provided
	if Session.Token == "" {
		log.Println("You must provide a Discord authentication token.")
		return
	}

	// Verify the Token is valid and grab user information
	Session.State.User, err = Session.User("@me")
	if err != nil {
		log.Printf("error fetching user information, %s\n", err)
	}

	// Open a websocket connection to Discord
	err = Session.Open()
	if err != nil {
		log.Printf("error opening connection to Discord, %s\n", err)
		os.Exit(1)
	}

	// Init boot vars
	mux.StartTime = time.Now()

	// Init Mux daemons
	mux.Config.Init(Session)
	mux.RemChan = make(chan mux.Reminder, 5)
	go mux.Reminders()

	Session.UpdateGameStatus(0, "with #cat-v")

	// Wait for a CTRL-C
	log.Printf(`Now running on PID ` + sc.Itoa(os.Getpid()) + `. Press CTRL-C to exit.`)
	sc := make(chan os.Signal, 1)
	signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
	<-sc

	// Clean up
	Session.Close()

	// Exit Normally.
}