Skip to content

Commit

Permalink
A lot of minor fixes (botlabs-gg#1123)
Browse files Browse the repository at this point in the history
* Update context.go (botlabs-gg#1111)

addition: trimpSpace

* fixed some low hanging fruits

* fixed some minor bugs

* moved hard coded numbers to constants

* changed formatting for modlog

Co-authored-by: caubert <caubert@gmail.com>
Co-authored-by: Ashish Jhanwar <ashishjh-bst@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 15, 2022
1 parent 5991545 commit 608383b
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 41 deletions.
13 changes: 11 additions & 2 deletions antiphishing/antiphishing.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var (
)

const RedisKeyPhishingDomains = "phishing_domains"
const PhishingCheckTrustRatingThreshold = 0.5

var logger = common.GetPluginLogger(&Plugin{})

Expand Down Expand Up @@ -232,10 +233,18 @@ func queryPhishingLinks(input []string) (string, error) {
if err != nil {
return "", err
}

badDomains := make([]string, len(bitflowAntifishResponse.Matches))
if bitflowAntifishResponse.Match {
return bitflowAntifishResponse.Matches[0].URL, err
for _, match := range bitflowAntifishResponse.Matches {
// only flag domains which have a low trust rating, this varies between 0 and 1, 0 means high trust, 1 means no trust.
// we use PhishingCheckTrustRatingThreshold (0.5) as cutoff to make sure false positives are ignored.
if match.TrustRating > PhishingCheckTrustRatingThreshold {
badDomains = append(badDomains, match.Domain)
}
}
return strings.Join(badDomains, ","), err
}

return "", nil
}

Expand Down
20 changes: 10 additions & 10 deletions automod/triggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (mc *MentionsTrigger) Name() string {
}

func (mc *MentionsTrigger) Description() string {
return "Triggers when a message includes more than x unique mentions."
return "Triggers when a message includes x unique mentions."
}

func (mc *MentionsTrigger) UserSettings() []*SettingDef {
Expand Down Expand Up @@ -344,7 +344,7 @@ func (vt *ViolationsTrigger) Name() string {
}

func (vt *ViolationsTrigger) Description() string {
return "Triggers when a user has more than x violations within y minutes."
return "Triggers when a user has x violations within y minutes."
}

func (vt *ViolationsTrigger) UserSettings() []*SettingDef {
Expand Down Expand Up @@ -428,7 +428,7 @@ func (caps *AllCapsTrigger) Name() string {
}

func (caps *AllCapsTrigger) Description() string {
return "Triggers when a message contains more than x% of just capitalized letters"
return "Triggers when a message contains x% of just capitalized letters"
}

func (caps *AllCapsTrigger) UserSettings() []*SettingDef {
Expand Down Expand Up @@ -647,17 +647,17 @@ func (s *SlowmodeTrigger) Name() string {
func (s *SlowmodeTrigger) Description() string {
if s.ChannelBased {
if s.Attachments {
return "Triggers when a channel has more than x attachments within y seconds"
return "Triggers when a channel has x attachments within y seconds"
}

return "Triggers when a channel has more than x messages in y seconds."
return "Triggers when a channel has x messages in y seconds."
}

if s.Attachments {
return "Triggers when a user has more than x attachments within y seconds in a single channel"
return "Triggers when a user has x attachments within y seconds in a single channel"
}

return "Triggers when a user has more than x messages in y seconds in a single channel."
return "Triggers when a user has x messages in y seconds in a single channel."
}

func (s *SlowmodeTrigger) UserSettings() []*SettingDef {
Expand Down Expand Up @@ -766,10 +766,10 @@ func (mt *MultiMsgMentionTrigger) Name() string {

func (mt *MultiMsgMentionTrigger) Description() string {
if mt.ChannelBased {
return "Triggers when a channel has more than x unique mentions in y seconds"
return "Triggers when a channel has x unique mentions in y seconds"
}

return "Triggers when a user has sent more than x unique mentions in y seconds in a single channel"
return "Triggers when a user has sent x unique mentions in y seconds in a single channel"
}

func (mt *MultiMsgMentionTrigger) UserSettings() []*SettingDef {
Expand Down Expand Up @@ -817,7 +817,7 @@ func (mt *MultiMsgMentionTrigger) CheckMessage(triggerCtx *TriggerContext, cs *d
break
}

if mt.ChannelBased || v.Author.ID == triggerCtx.MS.GuildID {
if mt.ChannelBased || v.Author.ID == triggerCtx.MS.User.ID {
// we only care about unique mentions, e.g mentioning the same user a ton wont do anythin
for _, msgMention := range v.Mentions {
if settings.CountDuplicates || !common.ContainsInt64Slice(mentions, msgMention.ID) {
Expand Down
16 changes: 8 additions & 8 deletions automod_legacy/assets/automod_legacy.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,13 @@ <h3>Basic automod</h3>

<div class="form-group">
<label for="NumMessages">Number of messages:</label>
<input type="number" class="form-control" name="Spam.NumMessages"
<input type="number" min="0" class="form-control" name="Spam.NumMessages"
value="{{.AutomodConfig.Spam.NumMessages}}"></input>
<p class="help-block">Number of messages sent within the time frame below for it to be triggered:</p>
</div>
<div class="form-group">
<label for="Within">Within (seconds):</label>
<input type="number" class="form-control" name="Spam.Within" value="{{.AutomodConfig.Spam.Within}}"></input>
<input type="number" min="0" class="form-control" name="Spam.Within" value="{{.AutomodConfig.Spam.Within}}"></input>
<p class="help-block">The timeframe to check messages from.</p>
</div>
<p>Something decent to just protect from raw spam could be five messages within two seconds. Normal users won't come
Expand All @@ -139,7 +139,7 @@ <h3>Basic automod</h3>

<div class="form-group">
<label for="Treshold">Mention Threshold</label>
<input type="number" class="form-control" name="Mention.Treshold"
<input type="number" min="0" class="form-control" name="Mention.Treshold"
value="{{.AutomodConfig.Mention.Treshold}}"></input>
<p class="help-block">The number of mentions in a message for it to trigger.<br>
If it's set to 5, then someone mentioning five people or more would trigger this.</p>
Expand Down Expand Up @@ -215,7 +215,7 @@ <h3>Basic automod</h3>
<hr />
<div class="form-group">
<label for="ViolationsExpire">Violations expire after (minutes):</label>
<input type="number" class="form-control" name="{{.Name}}.ViolationsExpire"
<input type="number" min="0" max="44640" class="form-control" name="{{.Name}}.ViolationsExpire"
value="{{.Rule.ViolationsExpire}}"></input>
<p class="help-block">When this many minutes have passed without any violations, the users violations are forgotten,
maximum is one month (44640 minutes).</p>
Expand All @@ -225,22 +225,22 @@ <h3>Basic automod</h3>
<div class="form-group row">
<div class="col-lg-2">
<label for="MuteAfter">Mute after: <small>(Set up role in moderation)</small></label>
<input type="number" class="form-control" placeholder="" value="{{.Rule.MuteAfter}}" name="{{.Name}}.MuteAfter">
<input type="number" min="0" class="form-control" placeholder="" value="{{.Rule.MuteAfter}}" name="{{.Name}}.MuteAfter">
</div>

<div class="col-lg-3">
<label for="BanAfter">Mute duration <b>(minutes)</b>:</label>
<input type="number" class="form-control" placeholder="" value="{{.Rule.MuteDuration}}"
<input type="number" min="0" class="form-control" placeholder="" value="{{.Rule.MuteDuration}}"
name="{{.Name}}.MuteDuration">
</div>

<div class="col-lg-3 col-lg-offset-1">
<label for="KickAfter">Kick after:</label>
<input type="number" class="form-control" placeholder="" value="{{.Rule.KickAfter}}" name="{{.Name}}.KickAfter">
<input type="number" min="0" class="form-control" placeholder="" value="{{.Rule.KickAfter}}" name="{{.Name}}.KickAfter">
</div>
<div class="col-lg-3">
<label for="BanAfter">Ban after <b>(Be careful with this)</b>:</label>
<input type="number" class="form-control" placeholder="" value="{{.Rule.BanAfter}}" name="{{.Name}}.BanAfter">
<input type="number" min="0" class="form-control" placeholder="" value="{{.Rule.BanAfter}}" name="{{.Name}}.BanAfter">
</div>
</div>
<p class="help-block">Punish based on number of violations >:O. (Punishment is disabled if set below 1.)</p>
Expand Down
2 changes: 1 addition & 1 deletion autorole/assets/autorole.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h2>Autorole</h2>
<div class="form-group">
<label for="autorole-duration">Minutes of membership required for role (<b>Disclaimer:</b> Will
not work if you have it set to only give roles on join)</label>
<input type="number" class="form-control" id="autorole-duration" name="RequiredDuration"
<input type="number" min="0" class="form-control" id="autorole-duration" name="RequiredDuration"
placeholder="" value="{{.Autorole.RequiredDuration}}">
</div>
<div class="form-group">
Expand Down
6 changes: 3 additions & 3 deletions commands/assets/commands.html
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ <h4>Are you sure you want to disable all commands?</h4>
{{if .Override.AutodeleteTrigger}}checked{{end}}>
</span>
</span>
<input type="number" class="form-control" placeholder="Seconds..."
<input type="number" min="0" class="form-control" placeholder="Seconds..."
value="{{if .Override}}{{.Override.AutodeleteTriggerDelay}}{{else}}10{{end}}"
name="AutodeleteTriggerDelay">
</div>
Expand All @@ -252,7 +252,7 @@ <h4>Are you sure you want to disable all commands?</h4>
{{if .Override.AutodeleteResponse}}checked{{end}}>
</span>
</span>
<input type="number" class="form-control" placeholder="Seconds..."
<input type="number" min="0" class="form-control" placeholder="Seconds..."
value="{{if .Override}}{{.Override.AutodeleteResponseDelay}}{{else}}10{{end}}"
name="AutodeleteResponseDelay">
</div>
Expand Down Expand Up @@ -371,7 +371,7 @@ <h4 class="card-title m-0">
{{if .Override.AutodeleteResponse}}checked{{end}}>
</span>
</span>
<input type="number" class="form-control" placeholder="Seconds..."
<input type="number" min="0" class="form-control" placeholder="Seconds..."
value="{{if .Override}}{{.Override.AutodeleteResponseDelay}}{{else}}10{{end}}"
name="AutodeleteResponseDelay">
</div>
Expand Down
23 changes: 23 additions & 0 deletions common/templates/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ var (
"trimSpace": strings.TrimSpace,
"upper": strings.ToUpper,
"urlescape": url.PathEscape,
"print": withOutputLimit(fmt.Sprint, MaxStringLength),
"println": withOutputLimit(fmt.Sprintln, MaxStringLength),
"printf": withOutputLimitF(fmt.Sprintf, MaxStringLength),

// math
"add": add,
Expand Down Expand Up @@ -829,3 +832,23 @@ func (s Slice) StringSlice(flag ...bool) interface{} {

return StringSlice
}

func withOutputLimit(f func(...interface{}) string, limit int) func(...interface{}) (string, error) {
return func(args ...interface{}) (string, error) {
out := f(args...)
if len(out) > limit {
return "", fmt.Errorf("string grew too long: length %d (max %d)", len(out), limit)
}
return out, nil
}
}

func withOutputLimitF(f func(string, ...interface{}) string, limit int) func(string, ...interface{}) (string, error) {
return func(format string, args ...interface{}) (string, error) {
out := f(format, args...)
if len(out) > limit {
return "", fmt.Errorf("string grew too long: length %d (max %d)", len(out), limit)
}
return out, nil
}
}
2 changes: 1 addition & 1 deletion customcommands/assets/customcommands-editcmd.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ <h2 class="card-title">
<div class="col-sm-4">
<div class="form-group">
<label>Interval</label>
<input type="number" class="form-control" name="time_trigger_interval"
<input type="number" min="0" class="form-control" name="time_trigger_interval"
placeholder="" value="{{call $dot.GetCCInterval .CC}}">
</div>
</div>
Expand Down
17 changes: 15 additions & 2 deletions customcommands/customcommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ var (
logger = common.GetPluginLogger(&Plugin{})
)

// Setting it to 1 Month approx
const (
MinIntervalTriggerDurationMinutes = 5
MinIntervalTriggerDurationHours = 1
MaxIntervalTriggerDurationHours = 744
MaxIntervalTriggerDurationMinutes = 44640
)

func KeyCommands(guildID int64) string { return "custom_commands:" + discordgo.StrID(guildID) }

type Plugin struct{}
Expand Down Expand Up @@ -160,8 +168,13 @@ func (cc *CustomCommand) Validate(tmpl web.TemplateData) (ok bool) {
return false
}

if cc.TriggerTypeForm == "interval_minutes" && cc.TimeTriggerInterval < 5 {
tmpl.AddAlerts(web.ErrorAlert("Minimum interval is now 5 minutes (was recently from 1)"))
if cc.TriggerTypeForm == "interval_minutes" && (cc.TimeTriggerInterval < MinIntervalTriggerDurationMinutes || cc.TimeTriggerInterval > MaxIntervalTriggerDurationMinutes) {
tmpl.AddAlerts(web.ErrorAlert(fmt.Sprintf("Minute interval can be between %v and %v", MinIntervalTriggerDurationMinutes, MaxIntervalTriggerDurationMinutes)))
return false
}

if cc.TriggerTypeForm == "interval_hours" && (cc.TimeTriggerInterval < MinIntervalTriggerDurationHours || cc.TimeTriggerInterval > MaxIntervalTriggerDurationHours) {
tmpl.AddAlerts(web.ErrorAlert(fmt.Sprintf("Hour interval can be between %v and %v", MinIntervalTriggerDurationHours, MaxIntervalTriggerDurationHours)))
return false
}

Expand Down
5 changes: 5 additions & 0 deletions customcommands/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func CalcNextRunTime(cc *models.CustomCommand, now time.Time) time.Time {
return time.Time{}
}

if cc.TimeTriggerInterval < MinIntervalTriggerDurationMinutes || cc.TimeTriggerInterval > MaxIntervalTriggerDurationMinutes {
// the interval is out of bounds and should never run
return time.Time{}
}

tNext := cc.LastRun.Time.Add(time.Minute * time.Duration(cc.TimeTriggerInterval))
// run it immedietely if this is the case
if tNext.Before(now) {
Expand Down
1 change: 1 addition & 0 deletions frontend/static/vendorr/bootstrap/css/bootstrap.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions moderation/assets/moderation.html
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ <h4>Delete ALL server warnings?</h4>

<div class="form-group">
<label>Default mute duration in minutes. Set to 0 for infinite</label>
<input type="number" name="DefaultMuteDuration.Int64" class="form-control"
<input type="number" min="0" name="DefaultMuteDuration.Int64" class="form-control"
value="{{.ModConfig.DefaultMuteDuration.Int64}}">
</div>
<hr />
Expand Down Expand Up @@ -351,7 +351,7 @@ <h4>Delete ALL server warnings?</h4>

<div class="form-group">
<label>Default number of days of messages to delete while banning. Range 0 to 7.</label>
<input type="number" name="DefaultBanDeleteDays.Int64" class="form-control"
<input type="number" min="0" max="7" name="DefaultBanDeleteDays.Int64" class="form-control"
value="{{.ModConfig.DefaultBanDeleteDays.Int64}}">
</div>
<hr />
Expand Down
2 changes: 1 addition & 1 deletion moderation/modlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func CreateModlogEmbed(config *Config, author *discordgo.User, action ModlogActi
URL: discordgo.EndpointUserAvatar(target.ID, target.Avatar),
},
Color: action.Color,
Description: fmt.Sprintf("**%s%s %s**#%s *(ID %d)*\n📄**Reason:** %s",
Description: fmt.Sprintf("**%s%s** %s#%s *(ID %d)*\n📄**Reason:** %s",
action.Emoji, action.Prefix, target.Username, target.Discriminator, target.ID, reason),
}

Expand Down
4 changes: 2 additions & 2 deletions reddit/assets/reddit.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ <h3>New feed</h3>
</div>
{{if .Slow}}<div class="form-group col">
<label>Minimum needed score</label>
<input type="number" name="min_upvotes" value="5" class="form-control">
<input type="number" min="0" name="min_upvotes" value="5" class="form-control">
</div>{{end}}
</div>

Expand Down Expand Up @@ -129,7 +129,7 @@ <h3>Current reddit feeds</h3>
</div>
{{if .Slow}}<div class="form-group col">
<label>Minimum needed score</label>
<input type="number" name="min_upvotes" value="{{.MinUpvotes}}" class="form-control">
<input type="number" min="0" name="min_upvotes" value="{{.MinUpvotes}}" class="form-control">
</div>{{end}}
</div>
</div>
Expand Down
6 changes: 3 additions & 3 deletions reputation/assets/reputation_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ <h2>Reputation settings - <a href="/public/{{.ActiveGuild.ID}}/reputation/leader
</div>
<div class="form-group">
<label for="cooldown">Rep cooldown in seconds</label>
<input type="number" class="form-control" id="cooldown" name="Cooldown"
<input type="number" min="0" class="form-control" id="cooldown" name="Cooldown"
value="{{.RepSettings.Cooldown}}">
</div>
<div class="form-group">
<label for="max-rep-amount-give">Maximum amount of rep that can be given in one
command.</label>
<input type="number" class="form-control" id="max-rep-amount-give" name="MaxGiveAmount"
<input type="number" min="0" class="form-control" id="max-rep-amount-give" name="MaxGiveAmount"
value="{{.RepSettings.MaxGiveAmount}}">
</div>
<div class="form-group">
<label for="max-rep-amount-remove">Maximum amount of rep that can be removed in one
command.</label>
<input type="number" class="form-control" id="max-rep-amount-remove"
<input type="number" min="0" class="form-control" id="max-rep-amount-remove"
name="MaxRemoveAmount" value="{{.RepSettings.MaxRemoveAmount}}">
</div>
</div>
Expand Down
Loading

0 comments on commit 608383b

Please sign in to comment.