Skip to content

Commit

Permalink
Merge branch 'release/3.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
j4rv committed Feb 27, 2024
2 parents 31c3dda + 560bfbd commit 420e5f0
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 14 deletions.
16 changes: 14 additions & 2 deletions cmd/jarvbot/bunker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func answerLiquid(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx contex
}

func answerDon(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context.Context) bool {
timeoutRole, err := guildRoleByName(ds, mc.GuildID, timeoutRoleName)
timeoutRole, err := getTimeoutRole(ds, mc.GuildID)
notifyIfErr("answerDon, couldn't get timeoutRole", err, ds)
if err != nil {
return false
Expand Down Expand Up @@ -45,7 +45,7 @@ func answerShoot(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context
return false
}

timeoutRole, err := guildRoleByName(ds, mc.GuildID, timeoutRoleName)
timeoutRole, err := getTimeoutRole(ds, mc.GuildID)
notifyIfErr("answerShoot: get timeout role", err, ds)
if err != nil {
return false
Expand All @@ -68,6 +68,18 @@ func answerShoot(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context
return err == nil
}

func getTimeoutRole(ds *discordgo.Session, guildID string) (*discordgo.Role, error) {
customRoleName, err := serverDS.getServerProperty(guildID, customTimeoutRoleNameKey)
if err != nil {
customRoleName = defaultTimeoutRoleName
}
return guildRoleByName(ds, guildID, customRoleName)
}

func setCustomTimeoutRole(ds *discordgo.Session, guildID string, roleName string) error {
return serverDS.setServerProperty(guildID, customTimeoutRoleNameKey, roleName)
}

// Internal functions

func shoot(ds *discordgo.Session, channelID string, guildID string, shooter *discordgo.Member, target *discordgo.Member, timeoutRoleID string) error {
Expand Down
32 changes: 26 additions & 6 deletions cmd/jarvbot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func onMessageCreated(ctx context.Context) func(ds *discordgo.Session, mc *disco
// the command key must be lowercased
var commands = map[string]command{
// public
"!version": simpleTextResponse("v3.0.3"),
"!source": simpleTextResponse("Source code: https://github.com/j4rv/discord-bot"),
"!genshindailycheckin": answerGenshinDailyCheckIn,
"!genshindailycheckinstop": answerGenshinDailyCheckInStop,
Expand All @@ -63,12 +64,13 @@ var commands = map[string]command{
"!shoot": notSpammable(answerShoot),
"!pp": notSpammable(answerPP),
// only available for discord mods
"!roleids": modOnly(answerRoleIDs),
"!react4roles": modOnly(answerMakeReact4RolesMsg),
"!addcommand": modOnly(answerAddCommand),
"!removecommand": modOnly(answerRemoveCommand),
"!allowspamming": modOnly(answerAllowSpamming),
"!preventspamming": modOnly(answerPreventSpamming),
"!roleids": modOnly(answerRoleIDs),
"!react4roles": modOnly(answerMakeReact4RolesMsg),
"!addcommand": modOnly(answerAddCommand),
"!removecommand": modOnly(answerRemoveCommand),
"!allowspamming": modOnly(answerAllowSpamming),
"!preventspamming": modOnly(answerPreventSpamming),
"!setcustomtimeoutrole": modOnly(answerSetCustomTimeoutRole),
// only available for the bot owner
"!addglobalcommand": adminOnly(answerAddGlobalCommand),
"!removeglobalcommand": adminOnly(answerRemoveGlobalCommand),
Expand Down Expand Up @@ -180,6 +182,24 @@ func answerPreventSpamming(ds *discordgo.Session, mc *discordgo.MessageCreate, c
return err == nil
}

func answerSetCustomTimeoutRole(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context.Context) bool {
guildID := mc.GuildID

timeoutRoleName := strings.TrimSpace(commandPrefixRegex.ReplaceAllString(mc.Content, ""))
_, err := guildRoleByName(ds, guildID, timeoutRoleName)
if err != nil {
ds.ChannelMessageSend(mc.ChannelID, fmt.Sprintf("Could not find role '%s'", timeoutRoleName))
return false
}

err = setCustomTimeoutRole(ds, guildID, timeoutRoleName)
notifyIfErr("setCustomTimeoutRole", err, ds)
if err == nil {
ds.ChannelMessageSend(mc.ChannelID, fmt.Sprintf("Custom timeout role set to '%s'", timeoutRoleName))
}
return err == nil
}

// ---------- Simple command stuff ----------

func answerAddCommand(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context.Context) bool {
Expand Down
5 changes: 3 additions & 2 deletions cmd/jarvbot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "time"

// TODO make this a config file

const dbFilename = "db.sqlite"
var dbFilename = "db.sqlite"

var strongboxMinAmount = 1.0
var strongboxMaxAmount = 64.0
Expand All @@ -13,7 +13,8 @@ var warnMessageMaxLength = 320

const avatarTargetSize = "1024"

const timeoutRoleName = "Shadow Realm"
const customTimeoutRoleNameKey = "custom_timeout_role_name"
const defaultTimeoutRoleName = "Shadow Realm"
const shootMisfireChance = 0.2
const nuclearCatastropheChance = 0.006
const timeoutDurationWhenShot = 4 * time.Minute
Expand Down
36 changes: 36 additions & 0 deletions cmd/jarvbot/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
var moddingDS moddingDataStore
var genshinDS genshinDataStore
var commandDS commandDataStore
var serverDS serverDataStore

var errZeroRowsAffected = errors.New("zero rows were affected")

Expand All @@ -25,6 +26,7 @@ func createTables(db *sqlx.DB) {
createTableSpammableChannel(db)
createTableUserWarning(db)
createTableReact4RoleMessage(db)
createTableServerProperties(db)
}

func createTableDailyCheckInReminder(db *sqlx.DB) {
Expand Down Expand Up @@ -95,6 +97,17 @@ func createTableReact4RoleMessage(db *sqlx.DB) {
createIndex("React4RoleMessage", "MessageID", db)
}

func createTableServerProperties(db *sqlx.DB) {
createTable("ServerProperties", []string{
"ServerID VARCHAR(20) UNIQUE NOT NULL",
"PropertyName VARCHAR(32) NOT NULL",
"PropertyValue TEXT NOT NULL",
"CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP",
"UNIQUE(ServerID, PropertyName)",
}, db)
createIndex("ServerProperties", "ServerID", db)
}

// commands

type commandDataStore struct {
Expand Down Expand Up @@ -266,6 +279,29 @@ func (s moddingDataStore) deleteReact4Roles(channelID, messageID string) error {
return err
}

// server

type serverDataStore struct {
db *sqlx.DB
}

func (s *serverDataStore) setServerProperty(serverID, propertyName, propertyValue string) error {
_, err := s.db.Exec(`
INSERT INTO ServerProperties (ServerID, PropertyName, PropertyValue)
VALUES (?, ?, ?)
ON CONFLICT(ServerID, PropertyName)
DO UPDATE SET PropertyValue = excluded.PropertyValue`,
serverID, propertyName, propertyValue)
return err
}

func (s *serverDataStore) getServerProperty(serverID, propertyName string) (string, error) {
var propertyValue string
err := s.db.Get(&propertyValue, `SELECT PropertyValue FROM ServerProperties WHERE ServerID = ? AND PropertyName = ?`,
serverID, propertyName)
return propertyValue, err
}

// methods for repetitive stuff

func createTable(table string, columns []string, db *sqlx.DB) {
Expand Down
47 changes: 47 additions & 0 deletions cmd/jarvbot/db_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"os"
"testing"
)

func initTestDB() {
dbFilename = "test.sqlite"
if _, err := os.Stat(dbFilename); err == nil {
err = os.Remove(dbFilename)
if err != nil {
panic("Failed to delete the database file: " + err.Error())
}
}
initDB()
}

func TestServerProperties(t *testing.T) {
initTestDB()

_, err := serverDS.getServerProperty("0000", "key")
if err == nil {
t.Error("Expected error, got nil")
}

serverDS.setServerProperty("0000", "key", "value")
val, err := serverDS.getServerProperty("0000", "key")
if err != nil {
t.Error(err)
}
if val != "value" {
t.Errorf("Expected 'value', got '%s'", val)
}

err = serverDS.setServerProperty("0000", "key", "value2")
if err != nil {
t.Error(err)
}
val, err = serverDS.getServerProperty("0000", "key")
if err != nil {
t.Error(err)
}
if val != "value2" {
t.Errorf("Expected 'value2', got '%s'", val)
}
}
5 changes: 1 addition & 4 deletions cmd/jarvbot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"context"
"flag"
"log"
"math/rand"
"os"
"os/signal"
"syscall"
"time"

"github.com/bwmarrin/discordgo"
"github.com/jmoiron/sqlx"
Expand All @@ -23,8 +21,6 @@ var noSlashCommands bool
const discordMaxMessageLength = 2000

func main() {
rand.Seed(time.Now().UTC().UnixNano())

initFlags()
initDB()
ds := initDiscordSession()
Expand Down Expand Up @@ -66,6 +62,7 @@ func initDB() {
genshinDS = genshinDataStore{db}
commandDS = commandDataStore{db}
moddingDS = moddingDataStore{db}
serverDS = serverDataStore{db}
}

func initDiscordSession() *discordgo.Session {
Expand Down

0 comments on commit 420e5f0

Please sign in to comment.