grim/wasdead

ihasdatabasenow
draft
2019-03-20, Ruin
a73b1126ee04
Parents 855a25ce5948
Children 662c3c77eb5a
ihasdatabasenow
  • +2 -1
    .hgignore
  • +56 -24
    main.go
  • --- a/.hgignore Wed Mar 20 21:07:22 2019 +0000
    +++ b/.hgignore Wed Mar 20 14:46:13 2019 -0700
    @@ -28,4 +28,5 @@
    _obj
    _test
    .vscode
    -debug
    \ No newline at end of file
    +debug
    +db
    \ No newline at end of file
    --- a/main.go Wed Mar 20 21:07:22 2019 +0000
    +++ b/main.go Wed Mar 20 14:46:13 2019 -0700
    @@ -1,10 +1,9 @@
    package main
    import (
    - "encoding/json"
    + "errors"
    "flag"
    "fmt"
    - "io/ioutil"
    "log"
    "net/url"
    "os"
    @@ -15,16 +14,18 @@
    "github.com/bwmarrin/discordgo"
    "github.com/nicklaw5/helix"
    + "github.com/prologic/bitcask"
    )
    var (
    DiscordToken string
    TwitchToken string
    + DatabasePath string
    TwitchClient *helix.Client
    logger *log.Logger
    startTime time.Time
    - guildToChannel map[string]string
    + database *bitcask.Bitcask
    )
    func init() {
    @@ -32,7 +33,8 @@
    startTime = time.Now()
    flag.StringVar(&DiscordToken, "t", "", "Discord Bot Token")
    - flag.StringVar(&TwitchToken, "tcid", "", "Twitch Client ID ID")
    + flag.StringVar(&TwitchToken, "tcid", "", "Twitch Client ID")
    + flag.StringVar(&DatabasePath, "db", "./db", "Database Path")
    flag.Parse()
    }
    @@ -65,12 +67,8 @@
    logDebug("Discord Token:", DiscordToken)
    logDebug("Twitch Token:", TwitchToken)
    - if fileExist("guilds.json") {
    - plan, _ := ioutil.ReadFile("guilds.json")
    - json.Unmarshal(plan, &guildToChannel)
    - } else {
    - guildToChannel = make(map[string]string)
    - }
    + database, _ = bitcask.Open(DatabasePath)
    + defer database.Close()
    client, err := helix.NewClient(&helix.Options{
    ClientID: TwitchToken,
    @@ -101,9 +99,6 @@
    <-sc
    dg.Close()
    -
    - data, _ := json.Marshal(guildToChannel)
    - ioutil.WriteFile("guilds.json", data, 0644)
    }
    func presenceUpdate(sess *discordgo.Session, evt *discordgo.PresenceUpdate) {
    @@ -115,8 +110,13 @@
    if evt.Game.Type == discordgo.GameTypeStreaming {
    pURL, _ := url.Parse(evt.Game.URL)
    - if ch, ok := guildToChannel[evt.GuildID]; ok {
    - processPresenceUpdate(sess, ch, strings.TrimLeft(pURL.Path, "/"))
    + channel := getguild2channel(evt.GuildID)
    + if channel != "" {
    + err := processPresenceUpdate(sess, channel, strings.TrimLeft(pURL.Path, "/"))
    +
    + if err != nil {
    + logError(err)
    + }
    }
    }
    }
    @@ -131,8 +131,12 @@
    if strings.EqualFold(commandItems[0], "!uptime") {
    duration := time.Now().Sub(startTime)
    - if ch, ok := guildToChannel[m.GuildID]; ok {
    - err := sendMessage(s, ch, fmt.Sprintf(
    + channel := getguild2channel(m.GuildID)
    + if channel != "" {
    + if channel != m.ChannelID {
    + return
    + }
    + err := sendMessage(s, channel, fmt.Sprintf(
    "Uptime is: **%02d:%02d:%02d** (since **%s**)",
    int(duration.Hours()),
    int(duration.Minutes())%60,
    @@ -148,15 +152,24 @@
    return
    }
    - if ch, ok := guildToChannel[m.GuildID]; ok {
    - processPresenceUpdate(s, ch, commandItems[1])
    + channel := getguild2channel(m.GuildID)
    + if channel != "" {
    + if channel != m.ChannelID {
    + return
    + }
    + err := processPresenceUpdate(s, channel, commandItems[1])
    +
    + if err != nil {
    + logError(err)
    + }
    +
    }
    }
    if strings.EqualFold(commandItems[0], "!init") {
    sendMessage(s, m.ChannelID, "All set up to talk in this channel!")
    - guildToChannel[m.GuildID] = m.ChannelID
    + setguild2channel(m.GuildID, m.ChannelID)
    }
    }
    @@ -166,14 +179,13 @@
    return err
    }
    -func processPresenceUpdate(s *discordgo.Session, channelid, twitchName string) {
    +func processPresenceUpdate(s *discordgo.Session, channelid, twitchName string) error {
    resp, err := TwitchClient.GetUsers(&helix.UsersParams{
    Logins: []string{twitchName},
    })
    if err != nil {
    - logError(err)
    - return
    + return err
    }
    /*
    @@ -183,6 +195,10 @@
    })
    */
    + if len(resp.Data.Users) <= 0 {
    + return errors.New("user was not found")
    + }
    +
    user := resp.Data.Users[0]
    f := make([]*discordgo.MessageEmbedField, 0)
    @@ -191,7 +207,7 @@
    Value: user.Description,
    })
    - s.ChannelMessageSendEmbed(channelid, &discordgo.MessageEmbed{
    + _, err = s.ChannelMessageSendEmbed(channelid, &discordgo.MessageEmbed{
    Image: &discordgo.MessageEmbedImage{
    URL: user.OfflineImageURL,
    },
    @@ -204,6 +220,8 @@
    },
    Fields: f,
    })
    +
    + return err
    }
    func fileExist(name string) bool {
    @@ -214,3 +232,17 @@
    }
    return true
    }
    +
    +func getguild2channel(guild string) string {
    + data, err := database.Get(guild)
    +
    + if err != nil {
    + return ""
    + }
    +
    + return string(data)
    +}
    +
    +func setguild2channel(guild, channel string) {
    + database.Put(guild, []byte(channel))
    +}