forked from chanbakjsd/TerraOceanPlugin
go: Implement invite code creation
It is now possible to create invite code using '!invite <validation code>'master
parent
eb20b6aa9a
commit
6b3761d98e
@ -1 +1,2 @@
|
||||
token.txt
|
||||
terraocean.db
|
||||
|
@ -0,0 +1,68 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
//Import this driver to be used by the SQL package.
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
var db *sql.DB
|
||||
|
||||
var errNotFound = errors.New("db: the requested entry was not found")
|
||||
var errAlreadyExists = errors.New("db: attempting to write to an entry that already exists")
|
||||
var errInviteUsed = errors.New("db: the requested invite has already been used")
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
db, err = sql.Open("sqlite3", "terraocean.db")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = db.Exec("CREATE TABLE IF NOT EXISTS invite (code TEXT, owner TEXT, used BOOLEAN)")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func getInviteOwner(inviteCode string) (string, error) {
|
||||
rows, err := db.Query("SELECT owner, used FROM invite WHERE code=?", inviteCode)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if rows.Next() {
|
||||
var owner string
|
||||
var used bool
|
||||
err := rows.Scan(&owner, &used)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if used {
|
||||
return "", errInviteUsed
|
||||
}
|
||||
return owner, nil
|
||||
}
|
||||
return "", errNotFound
|
||||
}
|
||||
|
||||
func setInviteOwner(inviteCode, owner string) error {
|
||||
_, err := getInviteOwner(inviteCode)
|
||||
if err == nil {
|
||||
return errAlreadyExists
|
||||
}
|
||||
if err != errNotFound {
|
||||
return err
|
||||
}
|
||||
_, err = db.Exec("INSERT INTO invite(code, owner, used) VALUES(?,?,?)", inviteCode, owner, false)
|
||||
return err
|
||||
}
|
||||
|
||||
func useInvite(inviteCode string) error {
|
||||
owner, err := getInviteOwner(inviteCode)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = db.Exec("REPLACE INTO invite(code, owner, used) VALUES(?,?,?)", inviteCode, owner, true)
|
||||
return err
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package main
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
func createInvite(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
|
||||
if !enforceDM(s, m) {
|
||||
return
|
||||
}
|
||||
if !membersOnly(s, m) {
|
||||
return
|
||||
}
|
||||
if len(command) < 2 {
|
||||
s.ChannelMessageSend(m.ChannelID, "指令的使用方法是`!invite <你所要设置的验证码>`.\nUsage: `!invite <validation code>`")
|
||||
return
|
||||
}
|
||||
if len(command) > 2 {
|
||||
s.ChannelMessageSend(m.ChannelID, "所提供的验证码不得有任何空格。\nThe provided validation code cannot contain any spaces.")
|
||||
return
|
||||
}
|
||||
|
||||
err := setInviteOwner(command[1], m.Author.ID)
|
||||
switch err {
|
||||
case errAlreadyExists:
|
||||
owner, _ := getInviteOwner(command[1])
|
||||
if owner == m.Author.ID {
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
"这个验证码归你所有,它仍然未被使用。请把它告诉你要邀请的人。\nThis validation code belongs to you. Please give it to the person you want to invite.",
|
||||
)
|
||||
return
|
||||
}
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
"这个验证码已被其他会员注册。请使用别的验证码。\nThis validation code is already registered. Please try another one.",
|
||||
)
|
||||
case errInviteUsed:
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
"这个验证码已被使用。请使用别的验证码。\nThis validation code has already been used. Please try another one.",
|
||||
)
|
||||
case nil:
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
"这个验证码现在属于你了。请好好保管并把它交给你要邀请的人。\nThis validation code now belongs to you! Pass it on to the person you want to invite.",
|
||||
)
|
||||
default:
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
"不明错误已发生,请把这个错误信息发在群里。\nAn unknown error has occurred. Please pass this error message on.\n"+err.Error(),
|
||||
)
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
|
||||
const guildID = "626424729234046987"
|
||||
const memberRoleID = "626434632614805526"
|
||||
|
||||
func enforceDM(s *discordgo.Session, m *discordgo.MessageCreate) bool {
|
||||
if m.GuildID != "" {
|
||||
//This command can only be used in DM to protect the invite creator.
|
||||
s.ChannelMessageDelete(m.ChannelID, m.ID)
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
m.Author.Mention()+",这个指令只能在私信中使用。\n"+m.Author.Mention()+", this command can only be used in DM.",
|
||||
)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func membersOnly(s *discordgo.Session, m *discordgo.MessageCreate) bool {
|
||||
member, err := s.GuildMember(guildID, m.Author.ID)
|
||||
if err != nil {
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
m.Author.Mention()+",请在成为会员后才使用这个指令。\n"+m.Author.Mention()+", please use this command after you become a member.",
|
||||
)
|
||||
return false
|
||||
}
|
||||
for _, v := range member.Roles {
|
||||
if v == memberRoleID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
s.ChannelMessageSend(
|
||||
m.ChannelID,
|
||||
m.Author.Mention()+",请在成为会员后才使用这个指令。\n"+m.Author.Mention()+", please use this command after you become a member.",
|
||||
)
|
||||
return false
|
||||
}
|
Loading…
Reference in New Issue