Skip to content

Commit

Permalink
Add invite thing for exezin
Browse files Browse the repository at this point in the history
  • Loading branch information
godwhoa committed Nov 4, 2018
1 parent f6b8d1a commit 70cd4c3
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require (
github.com/cenkalti/backoff v1.1.0
github.com/davecgh/go-spew v1.1.1
github.com/fsnotify/fsnotify v1.4.7
github.com/go-sql-driver/mysql v1.4.0
github.com/gofrs/uuid v3.1.0+incompatible
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce
github.com/jmoiron/sqlx v0.0.0-20180614180643-0dae4fefe7c0
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-sql-driver/mysql v1.4.0 h1:7LxgVwFb2hIQtMm87NdgAVfXjnt4OePseqT1tKx+opk=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/gofrs/uuid v3.1.0+incompatible h1:q2rtkjaKT4YEr6E1kamy0Ha4RtepWlQBedyHx0uzKwA=
github.com/gofrs/uuid v3.1.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead h1:Y9WOGZY2nw5ksbEf5AIpk+vK52Tdg/VN/rHFRfEeeGQ=
github.com/hako/durafmt v0.0.0-20180520121703-7b7ae1e72ead/go.mod h1:5Scbynm8dF1XAPwIwkGPqzkM/shndPm79Jd1003hTjE=
github.com/hashicorp/hcl v0.0.0-20180404174102-ef8a98b0bbce h1:xdsDDbiBDQTKASoGEZ+pEmF1OnWuu8AQ9I8iNbHNeno=
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/godwhoa/oodle/plugins/urban"
"github.com/godwhoa/oodle/plugins/webhook"
"github.com/godwhoa/oodle/plugins/wiki"
"github.com/godwhoa/oodle/plugins/invite"
_ "github.com/mattn/go-sqlite3"
flag "github.com/ogier/pflag"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -74,6 +75,7 @@ func main() {
urban.Register(deps),
wiki.Register(deps),
sed.Register(deps),
invite.Register(deps),
webhook.Register(deps),
); err != nil {
logger.Fatal(err)
Expand Down
62 changes: 62 additions & 0 deletions plugins/invite/invite.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package invite

import (
"time"

_ "github.com/go-sql-driver/mysql"
"github.com/godwhoa/oodle/oodle"
"github.com/gofrs/uuid"
"github.com/jmoiron/sqlx"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

func Register(deps *oodle.Deps) error {
sender, bot, log := deps.IRC, deps.Bot, deps.Logger
schema := `
CREATE TABLE IF NOT EXISTS tokens (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
token VARCHAR(36) NOT NULL,
nick VARCHAR(30) NOT NULL,
created TIMESTAMP NOT NULL
)
`
dsn := viper.GetString("token_mysql")
if dsn == "" {
return nil
}
db, err := sqlx.Connect("mysql", dsn)
if err != nil {
return err
}
err = db.Ping()
if err != nil {
return err
}
_, err = db.Exec(schema)
bot.RegisterCommands(Invite(sender, log, db))
return nil
}

func Invite(sender oodle.Sender, log *logrus.Logger, db *sqlx.DB) oodle.Command {
return oodle.Command{
Prefix: ".",
Name: "invite",
Description: "Generates an random invite code.",
Usage: ".invite",
Fn: func(nick string, args []string) (string, error) {
token, err := uuid.NewV4()
if err != nil {
return "Could not generate token", nil
}
_, err = db.Exec(`INSERT INTO tokens(token, nick, created) VALUES(?,?,?);`,
token.String(), nick, time.Now().UTC())
if err != nil {
log.Debug(err)
return "Database error.", nil
}
sender.SendTo(nick, token.String())
return "Invite sent.", nil
},
}
}

0 comments on commit 70cd4c3

Please sign in to comment.