package main

import (
	"fmt"
	"github.com/bwmarrin/discordgo"
	"os"
)

const botToken = "MTAxMzU4NDA1ODAxMTM2OTYwMg.GQRu8W.hH1uPAuqIq1ST7tBoMI_o2mYRZdOrp1P9qPjnU"

var BotId string

func main() {
	fmt.Println("Bot is starting...")

	discord, err := discordgo.New("Bot " + botToken)

	checkErr(err)

	user, err := discord.User("@me")

	checkErr(err)

	BotId = user.ID

	discord.AddHandler(messageHandler)

	err = discord.Open()

	checkErr(err)

	fmt.Println("Bot is running!")

	<-make(chan struct{})
	return
}

func messageHandler(session *discordgo.Session, message *discordgo.MessageCreate) {
	if message.Author.ID == BotId {
		return
	}

	switch message.Content {
	case "go$ping":
		_, _ = session.ChannelMessageSendReply(message.ChannelID, "pong!", message.Reference())
	case "go$stop", "go$exit":
		{
			_, _ = session.ChannelMessageSend(message.ChannelID, "Shutting down...")
			os.Exit(0)
		}
	}

	fmt.Println(message.Content)
}

func checkErr(err error) {
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	}
}
