Skip to content

Commit 6e6aa05

Browse files
committed
add and remove editors
1 parent 3950c85 commit 6e6aa05

5 files changed

Lines changed: 181 additions & 9 deletions

File tree

command/cmd_editor.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package command
2+
3+
import (
4+
"fmt"
5+
"monkebot/database"
6+
"monkebot/twitchapi"
7+
"monkebot/types"
8+
)
9+
10+
var editor = types.Command{
11+
Name: "editor",
12+
Aliases: []string{},
13+
Usage: "editor [add|remove] user",
14+
Description: "Opt out of one or all commands",
15+
ChannelCooldown: 5,
16+
UserCooldown: 5,
17+
NoPrefix: false,
18+
NoPrefixShouldRun: nil,
19+
CanDisable: false,
20+
Execute: func(message *types.Message, sender types.MessageSender, args []string) error {
21+
if len(args) != 3 || args[1] != "add" && args[1] != "remove" {
22+
sender.Say(message.Channel, "Usage: editor [add|remove] user")
23+
return nil
24+
}
25+
26+
if !message.Chatter.IsBroadcaster {
27+
sender.Say(message.Channel, "❌Only the broadcaster can set editors")
28+
return nil
29+
}
30+
31+
tx, err := message.DB.Begin()
32+
if err != nil {
33+
return err
34+
}
35+
defer tx.Rollback()
36+
37+
editor, err := twitchapi.GetUserByName(message.Cfg, args[2])
38+
if err != nil {
39+
return err
40+
}
41+
if editor == nil || len(*editor) == 0 {
42+
sender.Say(message.Channel, "User not found", struct {
43+
Param types.SenderParam
44+
Value string
45+
}{Param: types.ReplyMessageID, Value: message.ID})
46+
return nil
47+
}
48+
49+
editorID, editorName := (*editor)[0].ID, (*editor)[0].Login
50+
var successMsg string
51+
switch args[1] {
52+
case "add":
53+
err = database.InsertEditor(tx, message.Chatter.ID, editorID, editorName)
54+
if err != nil {
55+
return err
56+
}
57+
successMsg = fmt.Sprintf("✅Added editor %q", args[2])
58+
case "remove":
59+
err = database.RemoveEditor(tx, message.Chatter.ID, editorID)
60+
if err != nil {
61+
return err
62+
}
63+
successMsg = fmt.Sprintf("✅Removed editor %q", args[2])
64+
}
65+
66+
err = tx.Commit()
67+
if err != nil {
68+
return err
69+
}
70+
71+
sender.Say(message.Channel, successMsg, struct {
72+
Param types.SenderParam
73+
Value string
74+
}{Param: types.ReplyMessageID, Value: message.ID})
75+
return nil
76+
},
77+
}

command/cmd_remove.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
import (
44
"errors"
55
"fmt"
6+
"monkebot/database"
67
"monkebot/seventvapi"
78
"monkebot/twitchapi"
89
"monkebot/types"
@@ -21,36 +22,45 @@ var remove = types.Command{
2122
NoPrefixShouldRun: nil,
2223
CanDisable: true,
2324
Execute: func(message *types.Message, sender types.MessageSender, args []string) error {
24-
if len(args) < 3 {
25+
if len(args) < 2 {
2526
sender.Say(message.Channel, "❌Usage: remove [emote] #[channel]")
2627
return nil
2728
}
2829

29-
if !(message.Chatter.IsMod || message.Chatter.IsBroadcaster) {
30-
sender.Say(message.Channel, "❌You must be a moderator to use this command")
31-
return nil
30+
tx, err := message.DB.Begin()
31+
if err != nil {
32+
return err
3233
}
3334

3435
var targetChannelName string
3536
for i := 1; i < len(args); i++ {
3637
if channel, found := strings.CutPrefix(args[i], "#"); found {
3738
targetChannelName = channel
38-
// args left after this should be just emotes
39-
args = slices.Concat(args[1:i], args[i+1:])
39+
// remove channel from args
40+
args = slices.Delete(args, i, i+1)
4041
break
4142
}
4243
}
44+
4345
if targetChannelName == "" {
44-
sender.Say(message.Channel, "❌Usage: remove [emote] #[channel]")
45-
return nil
46+
targetChannelName = message.Channel
4647
}
48+
4749
res, err := twitchapi.GetUserByName(message.Cfg, targetChannelName)
4850
if err != nil || len(*res) == 0 {
4951
sender.Say(message.Channel, fmt.Sprintf("❌Failed to fetch channel %q", targetChannelName))
5052
return err
5153
}
5254
targetChannel := (*res)[0]
5355

56+
if !message.Chatter.IsBroadcaster && !database.SelectIsEditor(tx, targetChannel.ID, message.Chatter.ID) {
57+
sender.Say(message.Channel, "❌You must be an editor to use this command")
58+
return nil
59+
}
60+
61+
//remove command name from args
62+
args = args[1:]
63+
5464
for _, emote := range args {
5565
err = seventvapi.RemoveEmote("https://7tv.io", targetChannel.ID, emote, message.Cfg.SevenTVToken)
5666
if err != nil {

command/command.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ var Commands = []types.Command{
2727
optout,
2828
optin,
2929
remove,
30+
editor,
3031
}
3132

3233
var UnknownCommandErr = errors.New("unknown command")

database/database.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,41 @@ func UpdateUserCommandLastUsed(tx *sql.Tx, channelID string, commandName string,
507507

508508
return nil
509509
}
510+
511+
func SelectIsEditor(tx *sql.Tx, userID string, editorID string) bool {
512+
err := tx.QueryRow("SELECT user_id FROM user_editor WHERE user_id=? AND editor_id=?", userID, editorID).Scan(&userID)
513+
if err != nil {
514+
return false
515+
}
516+
return true
517+
}
518+
519+
func InsertEditor(tx *sql.Tx, userID string, editorID string, editorName string) error {
520+
err := InsertUsers(tx, false, struct {
521+
ID string
522+
Name string
523+
}{ID: editorID, Name: editorName})
524+
_, err = tx.Exec("INSERT INTO user_editor(user_id, editor_id) VALUES (?, ?)", userID, editorID)
525+
if err != nil {
526+
return err
527+
}
528+
return nil
529+
}
530+
531+
func RemoveEditor(tx *sql.Tx, userID string, editorID string) error {
532+
result, err := tx.Exec("DELETE FROM user_editor WHERE user_id=? AND editor_id=?", userID, editorID)
533+
if err != nil {
534+
return err
535+
}
536+
537+
rowsAffected, err := result.RowsAffected()
538+
if err != nil {
539+
return err
540+
}
541+
542+
if rowsAffected != 1 {
543+
return fmt.Errorf("attempt to delete editor %q for channel %q resulted in %d rows affected", editorID, userID, rowsAffected)
544+
}
545+
546+
return nil
547+
}

database/migrations.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ var CurrentSchemaStmts = []string{
3636
bot_is_joined BOOL NOT NULL DEFAULT false,
3737
FOREIGN KEY (permission_id) REFERENCES permission(id)
3838
)`,
39+
`CREATE TABLE user_editor (
40+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
41+
user_id TEXT NOT NULL,
42+
editor_id TEXT NOT NULL,
43+
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
44+
FOREIGN KEY (editor_id) REFERENCES user(id) ON DELETE CASCADE
45+
)`,
3946
`CREATE TABLE permission (
4047
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
4148
name TEXT NOT NULL,
@@ -170,12 +177,51 @@ var Migrations = DBMigrations{
170177
`INSERT INTO rpg_item (name, description) VALUES ('buttinho', 'The most widely used currency in the seven seas.')`,
171178
}},
172179
{Version: 7, Stmts: []string{
173-
`CREATE TABLE app_data (
180+
"ALTER TABLE user_command_cooldown RENAME TO user_command_data",
181+
`
182+
CREATE TABLE app_data (
174183
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
175184
migration_version INTEGER NOT NULL
176185
)`,
177186
`INSERT INTO app_data (migration_version) VALUES (7)`,
178187
}},
188+
{Version: 8, Stmts: []string{
189+
`CREATE TABLE user_editor (
190+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
191+
user_id TEXT NOT NULL,
192+
editor_id TEXT NOT NULL,
193+
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
194+
FOREIGN KEY (editor_id) REFERENCES user(id) ON DELETE CASCADE
195+
)`,
196+
197+
"INSERT INTO command (name) VALUES ('editor'), ('remove'), ('yoink')",
198+
`INSERT INTO user_command (user_id, command_id, is_enabled)
199+
SELECT id, (
200+
SELECT c.id FROM command c WHERE c.name = 'editor'
201+
), true FROM user`,
202+
`INSERT INTO user_command_data (user_id, command_id)
203+
SELECT id, (
204+
SELECT c.id FROM command c WHERE c.name = 'editor'
205+
) FROM user`,
206+
207+
`INSERT INTO user_command (user_id, command_id, is_enabled)
208+
SELECT id, (
209+
SELECT c.id FROM command c WHERE c.name = 'remove'
210+
), true FROM user`,
211+
`INSERT INTO user_command_data (user_id, command_id)
212+
SELECT id, (
213+
SELECT c.id FROM command c WHERE c.name = 'remove'
214+
) FROM user`,
215+
216+
`INSERT INTO user_command (user_id, command_id, is_enabled)
217+
SELECT id, (
218+
SELECT c.id FROM command c WHERE c.name = 'yoink'
219+
), true FROM user`,
220+
`INSERT INTO user_command_data (user_id, command_id)
221+
SELECT id, (
222+
SELECT c.id FROM command c WHERE c.name = 'yoink'
223+
) FROM user`,
224+
}},
179225
}}
180226

181227
func RunMigrations(tx *sql.Tx, migrations *DBMigrations, currentSchemaStmts []string) error {

0 commit comments

Comments
 (0)