-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: commands to applications and type masking * feat: Built a reaction handler through the rules system, also added an example through autopin
- Loading branch information
Showing
12 changed files
with
368 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package applicationrules | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aussiebroadwan/tony/database" | ||
"github.com/aussiebroadwan/tony/framework" | ||
) | ||
|
||
const ( | ||
autoPinThreshold = 5 | ||
) | ||
|
||
// AutoPinRule is a rule that automatically pins messages that are reacted to | ||
// with a pin emoji 📌 at least 5 times. | ||
type AutoPinRule struct { | ||
framework.ApplicationRule | ||
} | ||
|
||
func (r *AutoPinRule) Name() string { | ||
return "auto-pin" | ||
} | ||
|
||
func (r *AutoPinRule) GetType() framework.ApplicationRuleType { | ||
return framework.ApplicationRuleTypeReactions | ||
} | ||
|
||
// Test tests the rule against the content | ||
func (r *AutoPinRule) Test(content string) error { | ||
if content != "📌" { | ||
return nil | ||
} | ||
return fmt.Errorf("pin emoji found in message") | ||
} | ||
|
||
// Action takes action if the rule is violated | ||
func (r *AutoPinRule) Action(ctx *framework.Context, violation error) { | ||
db := ctx.Database() | ||
reaction, add := ctx.Reaction() | ||
|
||
// Increment or decrement the count | ||
if add { | ||
database.IncrementAutoPin(db, reaction.ChannelID, reaction.MessageID) | ||
} else { | ||
database.DecrementAutoPin(db, reaction.ChannelID, reaction.MessageID) | ||
} | ||
|
||
// Get the count | ||
count, pinned, err := database.GetAutoPin(db, reaction.ChannelID, reaction.MessageID) | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Check if the message should be pinned | ||
if count >= autoPinThreshold && !pinned.Valid { | ||
// Pin the message | ||
if err := ctx.Session().ChannelMessagePin(reaction.ChannelID, reaction.MessageID); err == nil { | ||
database.SetAutoPinPinned(db, reaction.ChannelID, reaction.MessageID, true) | ||
} | ||
} else if count < autoPinThreshold && pinned.Valid { | ||
// Unpin the message | ||
if err := ctx.Session().ChannelMessageUnpin(reaction.ChannelID, reaction.MessageID); err != nil { | ||
database.SetAutoPinPinned(db, reaction.ChannelID, reaction.MessageID, false) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package database | ||
|
||
import ( | ||
"database/sql" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func SetupAutoPinDB(db *sql.DB) { | ||
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS autopin ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
channel_id TEXT NOT NULL, | ||
message_id TEXT NOT NULL, | ||
reacts INTEGER NOT NULL, | ||
pinned TEXT | ||
)`) | ||
if err != nil { | ||
log.WithField("src", "database.SetupAutoPinDB").WithError(err).Fatal("Failed to create autopin table") | ||
} | ||
} | ||
|
||
func GetAutoPin(db *sql.DB, channelId string, messageId string) (int, sql.NullString, error) { | ||
var reacts int | ||
var pinned sql.NullString | ||
err := db.QueryRow(`SELECT reacts, pinned FROM autopin WHERE channel_id = ? AND message_id = ?`, channelId, messageId).Scan(&reacts, &pinned) | ||
if err != nil { | ||
return 0, sql.NullString{Valid: false}, err | ||
} | ||
return reacts, pinned, nil | ||
} | ||
|
||
func IncrementAutoPin(db *sql.DB, channelId string, messageId string) error { | ||
|
||
// Check if autopin already exists | ||
reacts, _, err := GetAutoPin(db, channelId, messageId) | ||
if err == nil { | ||
// Update autopin | ||
_, err := db.Exec(`UPDATE autopin SET reacts = ? WHERE channel_id = ? AND message_id = ?`, reacts+1, channelId, messageId) | ||
if err != nil { | ||
log.WithField("src", "database.IncrementAutoPin").WithError(err).Error("Failed to update autopin") | ||
return err | ||
} | ||
|
||
log.WithField("src", "database.IncrementAutoPin").Info("Updated autopin") | ||
return nil | ||
} | ||
|
||
_, err = db.Exec(`INSERT INTO autopin (channel_id, message_id, reacts) VALUES (?, ?, ?)`, channelId, messageId, 1) | ||
if err != nil { | ||
log.WithField("src", "database.IncrementAutoPin").WithError(err).Error("Failed to add autopin") | ||
return err | ||
} | ||
|
||
log.WithField("src", "database.IncrementAutoPin").Info("Added autopin") | ||
return nil | ||
} | ||
|
||
func DecrementAutoPin(db *sql.DB, channelId string, messageId string) error { | ||
// Check if autopin already exists | ||
reacts, _, err := GetAutoPin(db, channelId, messageId) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
newReacts := reacts - 1 | ||
|
||
// If the new reacts is 0, delete the autopin | ||
if newReacts == 0 { | ||
_, err = db.Exec(`DELETE FROM autopin WHERE channel_id = ? AND message_id = ?`, channelId, messageId) | ||
if err != nil { | ||
log.WithField("src", "database.DecrementAutoPin").WithError(err).Error("Failed to delete autopin") | ||
return err | ||
} | ||
|
||
log.WithField("src", "database.DecrementAutoPin").Info("Deleted autopin") | ||
return nil | ||
} | ||
|
||
// Update autopin | ||
_, err = db.Exec(`UPDATE autopin SET reacts = ? WHERE channel_id = ? AND message_id = ?`, newReacts, channelId, messageId) | ||
if err != nil { | ||
log.WithField("src", "database.DecrementAutoPin").WithError(err).Error("Failed to update autopin") | ||
return err | ||
} | ||
|
||
log.WithField("src", "database.DecrementAutoPin").Info("Updated autopin") | ||
return nil | ||
} | ||
|
||
func SetAutoPinPinned(db *sql.DB, channelId string, messageId string, pinned bool) error { | ||
|
||
if pinned { | ||
_, err := db.Exec(`UPDATE autopin SET pinned = ? WHERE channel_id = ? AND message_id = ?`, time.Now().Format(time.DateTime), channelId, messageId) | ||
if err != nil { | ||
log.WithField("src", "database.SetAutoPinPinned").WithError(err).Error("Failed to update autopin") | ||
return err | ||
} | ||
} else { | ||
_, err := db.Exec(`UPDATE autopin SET pinned = NULL WHERE channel_id = ? AND message_id = ?`, channelId, messageId) | ||
if err != nil { | ||
log.WithField("src", "database.SetAutoPinPinned").WithError(err).Error("Failed to update autopin") | ||
return err | ||
} | ||
|
||
} | ||
|
||
log.WithField("src", "database.SetAutoPinPinned").Info("Updated autopin") | ||
return nil | ||
} |
Oops, something went wrong.