Skip to content

Commit

Permalink
Merge branch 'release/3.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
j4rv committed Apr 5, 2024
2 parents 45ce5e1 + 4fa1acc commit bdf09e2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 4 deletions.
4 changes: 2 additions & 2 deletions cmd/jarvbot/bunker.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ func answerShoot(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context
}

func getTimeoutRole(ds *discordgo.Session, guildID string) (*discordgo.Role, error) {
customRoleName, err := serverDS.getServerProperty(guildID, customTimeoutRoleNameKey)
customRoleName, err := serverDS.getServerProperty(guildID, serverPropCustomTimeoutRoleName)
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)
return serverDS.setServerProperty(guildID, serverPropCustomTimeoutRoleName, roleName)
}

// Internal functions
Expand Down
40 changes: 40 additions & 0 deletions cmd/jarvbot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ var commands = map[string]command{
"!allowspamming": modOnly(answerAllowSpamming),
"!preventspamming": modOnly(answerPreventSpamming),
"!setcustomtimeoutrole": modOnly(answerSetCustomTimeoutRole),
"!announcehere": modOnly(answerAnnounceHere),
// only available for the bot owner
"!addglobalcommand": adminOnly(answerAddGlobalCommand),
"!removeglobalcommand": adminOnly(answerRemoveGlobalCommand),
"!announce": adminOnly(answerAnnounce),
"!listcommands": adminOnly(answerListCommands),
"!reboot": adminOnly(answerReboot),
"!shutdown": adminOnly(answerShutdown),
Expand Down Expand Up @@ -201,6 +203,15 @@ func answerSetCustomTimeoutRole(ds *discordgo.Session, mc *discordgo.MessageCrea
return err == nil
}

func answerAnnounceHere(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context.Context) bool {
err := serverDS.setServerProperty(mc.GuildID, serverPropAnnounceHere, mc.ChannelID)
notifyIfErr("answerAnnounceHere", err, ds)
if err == nil {
ds.ChannelMessageSend(mc.ChannelID, "Okay! Will send announcements in this channel")
}
return err == nil
}

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

func answerAddCommand(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context.Context) bool {
Expand Down Expand Up @@ -255,6 +266,35 @@ func answerRemoveGlobalCommand(ds *discordgo.Session, mc *discordgo.MessageCreat
return err == nil
}

func answerAnnounce(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context.Context) bool {
commandBody := strings.TrimSpace(commandPrefixRegex.ReplaceAllString(mc.Content, ""))
properties, err := serverDS.getServerProperties(serverPropAnnounceHere)
if err != nil {
ds.ChannelMessageSend(mc.ChannelID, "Could not get server properties: "+err.Error())
return false
}
log.Println("Server properties", properties)

errors := ""
for _, prop := range properties {
_, err = ds.ChannelMessageSend(prop.PropertyValue, commandBody)
log.Println("Sending message to channel", prop.PropertyValue, "in server", prop.ServerID, "with content", commandBody)
if err != nil {
errors += fmt.Sprintf("Could not send message to channel %s in server %s: %s\n", prop.PropertyValue, prop.ServerID, err)
}
}

if errors != "" {
ds.ChannelMessageSendEmbed(mc.ChannelID, &discordgo.MessageEmbed{
Title: "Errors while announcing",
Description: errors,
})
} else {
ds.ChannelMessageSend(mc.ChannelID, commandSuccessMessage)
}
return errors == ""
}

func answerListCommands(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context.Context) bool {
keys, err := commandDS.allSimpleCommandKeys(mc.GuildID)
notifyIfErr("answerListCommands::allSimpleCommandKeys", err, ds)
Expand Down
4 changes: 3 additions & 1 deletion cmd/jarvbot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ var warnMessageMaxLength = 320

const avatarTargetSize = "1024"

const customTimeoutRoleNameKey = "custom_timeout_role_name"
const serverPropCustomTimeoutRoleName = "custom_timeout_role_name"
const serverPropAnnounceHere = "announce_here"

const defaultTimeoutRoleName = "Shadow Realm"
const shootMisfireChance = 0.2
const nuclearCatastropheChance = 0.006
Expand Down
15 changes: 14 additions & 1 deletion cmd/jarvbot/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func createTableReact4RoleMessage(db *sqlx.DB) {

func createTableServerProperties(db *sqlx.DB) {
createTable("ServerProperties", []string{
"ServerID VARCHAR(20) UNIQUE NOT NULL",
"ServerID VARCHAR(20) NOT NULL",
"PropertyName VARCHAR(32) NOT NULL",
"PropertyValue TEXT NOT NULL",
"CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP",
Expand Down Expand Up @@ -285,6 +285,12 @@ type serverDataStore struct {
db *sqlx.DB
}

type ServerProperty struct {
ServerID string `db:"ServerID"`
PropertyName string `db:"PropertyName"`
PropertyValue string `db:"PropertyValue"`
}

func (s *serverDataStore) setServerProperty(serverID, propertyName, propertyValue string) error {
_, err := s.db.Exec(`
INSERT INTO ServerProperties (ServerID, PropertyName, PropertyValue)
Expand All @@ -302,6 +308,13 @@ func (s *serverDataStore) getServerProperty(serverID, propertyName string) (stri
return propertyValue, err
}

func (s *serverDataStore) getServerProperties(propertyName string) ([]ServerProperty, error) {
var properties []ServerProperty
err := s.db.Select(&properties, `SELECT ServerID, PropertyName, PropertyValue FROM ServerProperties WHERE PropertyName = ?`,
propertyName)
return properties, err
}

// methods for repetitive stuff

func createTable(table string, columns []string, db *sqlx.DB) {
Expand Down
12 changes: 12 additions & 0 deletions cmd/jarvbot/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,16 @@ func TestServerProperties(t *testing.T) {
if val != "value2" {
t.Errorf("Expected 'value2', got '%s'", val)
}

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

0 comments on commit bdf09e2

Please sign in to comment.