Skip to content
Snippets Groups Projects
Select Git revision
  • 50e904c5e01fcf92e841201d15b911a82f0ac68d
  • master default protected
2 results

discord.go

Blame
  • discord.go 7.29 KiB
    package main
    
    import (
    	"fmt"
    	"github.com/bwmarrin/discordgo"
    	tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
    	"log"
    	"strconv"
    	"strings"
    	"sync"
    )
    
    var (
    	session      *discordgo.Session
    	application  *discordgo.Application
    	discordMutex = make(map[string]*sync.Mutex)
    )
    
    func init() {
    	discordgo.Logger = func(msgL, _ int, format string, a ...interface{}) {
    		prefix := "discord: "
    		switch msgL {
    		case discordgo.LogError:
    			prefix += "[error] "
    		case discordgo.LogWarning:
    			prefix += "[warning] "
    		case discordgo.LogInformational:
    			prefix += "[informational] "
    		case discordgo.LogDebug:
    			if !config.System.Verbose {
    				return
    			}
    			prefix += "[debug] "
    		}
    		log.Printf(prefix+format, a...)
    	}
    }
    
    func openDiscord() {
    	if s, err := discordgo.New(); err != nil {
    		// this should never happen
    		log.Fatalf("error creating discord session: %s", err)
    	} else {
    		session = s
    	}
    
    	if strings.HasPrefix(config.Discord.Token, "Bot ") {
    		session.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
    	} else {
    		session.Identify.Intents = 0
    		session.Identify.Properties.Browser = ""
    		session.Identify.Properties.OS = "Linux"
    		session.Identify.Properties.Referer = ""
    		session.Identify.Properties.ReferringDomain = ""
    		session.Identify.Properties.Device = ""
    		session.Identify.Capabilities = 61
    	}
    	session.Token = config.Discord.Token
    
    	if err := session.Open(); err != nil {
    		log.Fatalf("error connecting to discord: %s", err)
    	} else {
    		log.Printf("connected to discord as %s#%s (%s)",
    			session.State.User.Username, session.State.User.Discriminator, session.State.User.ID)
    	}
    
    	if strings.HasPrefix("Bot ", config.Discord.Token) {
    		if app, err := session.Application("@me"); err != nil {
    			log.Fatalf("error getting application: %s", err)
    		} else {