parent
38a9d2eda2
commit
614186a4a7
@ -0,0 +1,20 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
var db *sql.DB
|
||||||
|
|
||||||
|
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)")
|
||||||
|
db.Exec("CREATE TABLE shard(id STRING NOT NULL UNIQUE, shard INTEGER NOT NULL)")
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
viewAuditLogRole = "645856347258093568"
|
||||||
|
manageWebhookRole = "645856081750523914"
|
||||||
|
changeNicknameRole = "645977231281356800"
|
||||||
|
embedLinkRole = "645977361254318080"
|
||||||
|
voiceActivityRole = "645977361254318080"
|
||||||
|
)
|
||||||
|
|
||||||
|
func checkForGachaEligibility(dg *discordgo.Session, discordID, intendedLevel string) {
|
||||||
|
member, err := dg.GuildMember(guildID, discordID)
|
||||||
|
if err != nil {
|
||||||
|
logError(dg, "checkForGachaEligibility", "getGuildMember", discordID)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
found := false
|
||||||
|
for _, v := range member.Roles {
|
||||||
|
if v == intendedLevel {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if found {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
rollGacha(dg, member)
|
||||||
|
|
||||||
|
err = dg.GuildMemberRoleAdd(guildID, discordID, intendedLevel)
|
||||||
|
if err != nil {
|
||||||
|
logError(dg, "checkForGachaEligibility", "guildMemberRoleAdd", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func rollGacha(dg *discordgo.Session, member *discordgo.Member) {
|
||||||
|
lookUpAndGive := func(friendlyName, role string) {
|
||||||
|
found := false
|
||||||
|
for _, v := range member.Roles {
|
||||||
|
if v == role {
|
||||||
|
found = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
channel, err := dg.UserChannelCreate(member.User.ID)
|
||||||
|
if found {
|
||||||
|
rewardShard(dg, member.User.ID)
|
||||||
|
if err == nil {
|
||||||
|
dg.ChannelMessageSend(
|
||||||
|
channel.ID,
|
||||||
|
fmt.Sprintf(
|
||||||
|
"**GACHA > ** You got __%s__! However, it was a duplicate so you got a shard instead.", friendlyName,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
dg.GuildMemberRoleAdd(guildID, member.User.ID, role)
|
||||||
|
dg.ChannelMessageSend(
|
||||||
|
channel.ID,
|
||||||
|
fmt.Sprintf("**GACHA > ** You got __%s__!", friendlyName),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
gachaRandomness := make([]byte, 1)
|
||||||
|
rand.Read(gachaRandomness)
|
||||||
|
switch gachaRandomness[0] % 5 {
|
||||||
|
case 0:
|
||||||
|
lookUpAndGive("View Audit Log", viewAuditLogRole)
|
||||||
|
case 1:
|
||||||
|
lookUpAndGive("Manage Webhook", manageWebhookRole)
|
||||||
|
case 2:
|
||||||
|
lookUpAndGive("Change Nickname", changeNicknameRole)
|
||||||
|
case 3:
|
||||||
|
lookUpAndGive("Embed Link", embedLinkRole)
|
||||||
|
case 4:
|
||||||
|
lookUpAndGive("Use Voice Activity", voiceActivityRole)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
func rewardShard(dg *discordgo.Session, discordID string) {
|
||||||
|
tx, err := db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
logError(dg, "rewardShard", "begin", discordID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, err = tx.Exec("INSERT INTO shard(id, shard) VALUES(?, 1) ON CONFLICT(id) DO UPDATE SET shard = shard+1;", discordID)
|
||||||
|
if err != nil {
|
||||||
|
logError(dg, "rewardShard", "insert", discordID, err)
|
||||||
|
tx.Rollback()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = tx.Commit()
|
||||||
|
if err != nil {
|
||||||
|
logError(dg, "rewardShard", "commit", discordID, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getShard(dg *discordgo.Session, discordID string) int {
|
||||||
|
rows, err := db.Query("SELECT shard FROM shard WHERE id=?", discordID)
|
||||||
|
if err != nil {
|
||||||
|
logError(dg, "getShard", "select", discordID, err)
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
if rows.Next() {
|
||||||
|
var shard int
|
||||||
|
rows.Scan(&shard)
|
||||||
|
return shard
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
Reference in New Issue