Initial Commit

master 0.0
Luther Wen Xu 2019-11-17 22:47:34 +07:00
commit c9c68d7e88
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
7 changed files with 142 additions and 0 deletions

2
.gitignore vendored

@ -0,0 +1,2 @@
token.txt
bot.db

@ -0,0 +1,8 @@
module PermissionGacha
go 1.13
require (
github.com/bwmarrin/discordgo v0.20.1
github.com/mattn/go-sqlite3 v1.12.0
)

@ -0,0 +1,10 @@
github.com/bwmarrin/discordgo v0.20.1 h1:Ihh3/mVoRwy3otmaoPDUioILBJq4fdWkpsi83oj2Lmk=
github.com/bwmarrin/discordgo v0.20.1/go.mod h1:O9S4p+ofTFwB02em7jkpkV8M3R0/PUVOwN61zSZ0r4Q=
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/mattn/go-sqlite3 v1.12.0 h1:u/x3mp++qUxvYfulZ4HKOvVO0JWhk7HtE8lWhbGz/Do=
github.com/mattn/go-sqlite3 v1.12.0/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/mattn/go-sqlite3 v2.0.0+incompatible h1:+afSeuaczjy4ZUN55wuDe1bCaAFBu0hg5Cf2Zw2ar0s=
github.com/mattn/go-sqlite3 v2.0.0+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA=
golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=

@ -0,0 +1,34 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"os/signal"
"strings"
"syscall"
"github.com/bwmarrin/discordgo"
)
func main() {
token, _ := ioutil.ReadFile("token.txt")
dg, err := discordgo.New(strings.TrimSpace(string(token)))
if err != nil {
fmt.Println("Error creating Discord session:", err)
return
}
dg.AddHandler(messageCreate)
err = dg.Open()
if err != nil {
fmt.Println("Error opening Discord session:", err)
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
dg.Close()
db.Close()
}

@ -0,0 +1,12 @@
package main
import "github.com/bwmarrin/discordgo"
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.Bot {
return
}
go incrementXP(s, m.Author.ID)
//TODO Handle commands
}

@ -0,0 +1,13 @@
package main
import (
"log"
"github.com/bwmarrin/discordgo"
)
func logError(dg *discordgo.Session, a ...interface{}) {
dg.UserUpdateStatus(discordgo.StatusDoNotDisturb)
dg.UpdateStatus(0, "with error")
log.Println(a...)
}

63
xp.go

@ -0,0 +1,63 @@
package main
import (
"database/sql"
"math"
"sync"
"time"
"github.com/bwmarrin/discordgo"
_ "github.com/mattn/go-sqlite3"
)
var db *sql.DB
var lastMessage = make(map[string]time.Time)
var incrementMutex = sync.Mutex{}
func init() {
var err error
db, err = sql.Open("sqlite3", "./bot.db")
if err != nil {
panic(err)
}
db.SetMaxOpenConns(1)
db.Exec("CREATE TABLE xp(id STRING NOT NULL UNIQUE, xp INTEGER NOT NULL)")
}
func incrementXP(dg *discordgo.Session, discordID string) {
amountToIncrement := calculateIncrement(discordID)
tx, err := db.Begin()
if err != nil {
logError(dg, "incrementXP", "begin", discordID, err)
return
}
_, err = tx.Exec("INSERT INTO xp(id, xp) VALUES(?, ?) ON CONFLICT(id) DO UPDATE SET xp = xp+?;", discordID, amountToIncrement)
if err != nil {
logError(dg, "incrementXP", "insert", discordID, err)
tx.Rollback()
return
}
err = tx.Commit()
if err != nil {
logError(dg, "incrementXP", "commit", discordID, err)
return
}
}
func calculateIncrement(discordID string) int {
incrementMutex.Lock()
defer incrementMutex.Unlock()
now := time.Now()
delta := now.Sub(lastMessage[discordID])
lastMessage[discordID] = now
if delta.Seconds() < 0 {
return 0
}
//Constructed on Desmos:
//y=\frac{100}{\left(1+e^{-\left(\frac{x-15}{3}\right)}\right)}
return int(100 / (1 + math.Pow(math.E, -((delta.Seconds()-15)/3))))
}