Skip to content

Commit 3664558

Browse files
committed
Mines update
and more scripts examples
1 parent 86d2d9e commit 3664558

7 files changed

Lines changed: 96 additions & 13 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ run-development.bat
44
*.sqlite
55
*.exe
66
/.vscode/*
7-
test.go
7+
test.go
8+
/gitignored

cmd/jarvbot/bunker.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,14 @@ func getTimeoutRole(ds *discordgo.Session, guildID string) (*discordgo.Role, err
124124
return guildRoleByName(ds, guildID, customRoleName)
125125
}
126126

127+
func getTimeoutRoleName(ds *discordgo.Session, guildID string) string {
128+
timeoutRole, err := getTimeoutRole(ds, guildID)
129+
if err != nil {
130+
return defaultTimeoutRoleName
131+
}
132+
return timeoutRole.Name
133+
}
134+
127135
func setCustomTimeoutRole(ds *discordgo.Session, guildID string, roleName string) error {
128136
return serverDS.setServerProperty(guildID, serverPropCustomTimeoutRoleName, roleName)
129137
}

cmd/jarvbot/commands.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ var commands = map[string]command{
138138
"!announce": adminOnly(answerAnnounce),
139139
"!dbbackup": adminOnly(answerDbBackup),
140140
"!runtimestats": adminOnly(answerRuntimeStats),
141+
"!sudoplacemines": adminOnly(answerPlaceMines),
142+
"!sudocheckmines": adminOnly(answerCheckMines),
141143
"!abort": adminOnly(answerAbort),
142144
"!reboot": adminOnly(answerReboot),
143145
"!shutdown": adminOnly(answerShutdown),

cmd/jarvbot/mines.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ var mineTriggerMessages = []string{
2121
"<user> triggered a mine.\nPress F to pay respects.",
2222
"<user> stepped on a mine and didn't have enough Explosion Resistance.",
2323
"<user> found a hidden mine! Sadly, it blew up when they picked it up.",
24-
"<user> just won the Big Mine Lottery! Enjoy your (role) prize.",
25-
"This channel was NOT safe. Goodbye <user>.",
24+
"<user> just won the Big Mine Lottery! Enjoy your <role> prize.",
25+
"This channel was NOT safe. <user> blew up, goodbye.",
2626
"R.I.P. <user>\n\nSpoke at the wrong time and place.\n\n<joinyear> - <curryear>",
27-
"Mine detected. Oh wait - too late for <user>.",
27+
"Mine detected. Oh wait, too late for <user>.",
2828
"!mineexplode <user>",
2929
// Game references
3030
"Rocketboo missed the Ethereal and hit <user> instead!",
@@ -35,7 +35,7 @@ var mineTriggerMessages = []string{
3535
type PlaceMinesQueryInput struct {
3636
Where string `short:"w" long:"where" default:""` // Channel id, or empty for whole guild
3737
Guild string `short:"g" long:"guild" default:""` // Guild id, for admin user
38-
ChancePercentage float64 `short:"c" long:"chance" default:"5.0"` // 5.0 = 5%
38+
ChancePercentage float64 `short:"c" long:"chance" default:"1.0"` // 5.0 = 5%
3939
Amount int `short:"a" long:"amount" default:"10"` // Amount of mines
4040
Duration string `short:"d" long:"duration" default:"2m"` // 99h99m99s
4141
CustomMessage string `short:"m" long:"custom_message" default:""` // Custom message that replaces the default ones
@@ -48,7 +48,7 @@ func answerPlaceMines(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx co
4848
return false
4949
}
5050

51-
if len(existing) >= minesMaxSetsPerGuild {
51+
if len(existing) >= minesMaxSetsPerGuild && mc.Author.ID != adminID {
5252
ds.ChannelMessageSend(mc.ChannelID, "You have too many mine sets in this server!")
5353
return false
5454
}
@@ -65,7 +65,7 @@ func answerPlaceMines(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx co
6565
return false
6666
}
6767

68-
if len(input.CustomMessage) > minesMaxCustomMessageLength {
68+
if len(input.CustomMessage) > minesMaxCustomMessageLength && mc.Author.ID != adminID {
6969
ds.ChannelMessageSend(mc.ChannelID, "That custom message is too long.")
7070
return false
7171
}
@@ -90,8 +90,34 @@ func answerPlaceMines(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx co
9090
return true
9191
}
9292

93+
type CheckMinesQueryInput struct {
94+
Guild string `short:"g" long:"guild" default:""` // Guild id, for admin user
95+
}
96+
9397
func answerCheckMines(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx context.Context) bool {
94-
mines, err := serverDS.getMinesByGuild(mc.GuildID)
98+
var input CheckMinesQueryInput
99+
err := parseCommandArgs(&input, mc.Content)
100+
if err != nil {
101+
ds.ChannelMessageSend(mc.ChannelID, "Wrong format error: "+err.Error())
102+
return false
103+
}
104+
var guildID string
105+
var guildName string
106+
107+
if input.Guild != "" && mc.Author.ID == adminID {
108+
guildID = input.Guild
109+
guild, err := ds.Guild(guildID)
110+
if err != nil {
111+
ds.ChannelMessageSend(mc.ChannelID, "Could not find Guild: "+err.Error())
112+
return false
113+
}
114+
guildName = guild.Name
115+
} else {
116+
guildID = mc.GuildID
117+
guildName = "this server"
118+
}
119+
120+
mines, err := serverDS.getMinesByGuild(guildID)
95121
if err != nil {
96122
adminNotifyIfErr("answerCheckMines", err, ds)
97123
return false
@@ -108,8 +134,8 @@ func answerCheckMines(ds *discordgo.Session, mc *discordgo.MessageCreate, ctx co
108134

109135
if m.ChannelID == "" {
110136
fmt.Fprintf(&b,
111-
"ID: %d, %d mines in this server with %.4f%% chance of exploding.",
112-
m.ID, m.Amount, percent)
137+
"ID: %d, %d mines in %s with %.4f%% chance of exploding.",
138+
m.ID, m.Amount, guildName, percent)
113139
} else {
114140
fmt.Fprintf(&b,
115141
"ID: %d, %d mines in channel <#%s> with %.4f%% chance of exploding.",
@@ -191,7 +217,7 @@ func processMinesetTrigger(ds *discordgo.Session, mc *discordgo.MessageCreate, m
191217
}
192218

193219
// Normal mine logic
194-
message := buildMineMessage(mc, mineset)
220+
message := buildMineMessage(ds, mc, mineset)
195221
_, err = ds.ChannelMessageSend(mc.ChannelID, message)
196222
serverNotifyIfErr("Mine message could not be sent", err, mc.GuildID, ds)
197223
if err := ds.GuildMemberRoleAdd(mc.GuildID, mc.Author.ID, timeoutRole.ID); err == nil {
@@ -200,15 +226,21 @@ func processMinesetTrigger(ds *discordgo.Session, mc *discordgo.MessageCreate, m
200226
}
201227
}
202228

203-
func buildMineMessage(mc *discordgo.MessageCreate, mineset *MineSet) string {
229+
func buildMineMessage(ds *discordgo.Session, mc *discordgo.MessageCreate, mineset *MineSet) string {
204230
var message string
205231
if strings.TrimSpace(mineset.CustomMessage) != "" {
206232
message = mineset.CustomMessage
207233
} else {
208234
message = rngx.Pick(mineTriggerMessages)
209235
}
236+
// cheap replacements
210237
message = strings.Replace(message, "<user>", mc.Author.Mention(), 1)
211238
message = strings.Replace(message, "<joinyear>", mc.Member.JoinedAt.Format("2006"), 1)
212239
message = strings.Replace(message, "<curryear>", time.Now().Format("2006"), 1)
240+
// more expensive replacements
241+
if strings.Contains(message, "<role>") {
242+
rolename := getTimeoutRoleName(ds, mc.GuildID)
243+
message = strings.Replace(message, "<role>", rolename, 1)
244+
}
213245
return message
214246
}

scripts/discordbot.service

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[Unit]
2+
Description=Discord bot
3+
After=network.target
4+
StartLimitIntervalSec=0
5+
6+
[Service]
7+
User=pi
8+
Type=simple
9+
Restart=always
10+
RestartSec=1
11+
ExecStart=/home/pi/discord-bot/run.sh
12+
13+
[Install]
14+
WantedBy=multi-user.target
15+

scripts/run.sh.exampleForRaspberry

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,4 @@ export GOARCH=arm
88
export GOARM=7
99
export PATH=$PATH:/usr/local/go/bin
1010

11-
cd /home/pi/discord-bot
1211
/home/pi/go/bin/jarvbot -token *********************************************************** -adminID ******************

scripts/updateDiscordBot.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
set -eu
3+
4+
log() {
5+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*"
6+
}
7+
8+
cd "$HOME/discord-bot"
9+
10+
GIT_OUTPUT=$(git pull --ff-only)
11+
12+
if [ "$GIT_OUTPUT" = "Already up to date." ]; then
13+
log "No changes; nothing to do"
14+
exit 0
15+
fi
16+
17+
log "Updating bot"
18+
19+
go mod download
20+
/usr/local/go/bin/go build ./cmd/jarvbot/
21+
22+
sudo cp /usr/local/jarvbot /usr/local/jarvbot.bak || true
23+
sudo mv ./jarvbot /usr/local/jarvbot
24+
sudo systemctl restart discordbot
25+
26+
log "Update complete"

0 commit comments

Comments
 (0)