Lint: Fix all linter errors

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2021-07-31 20:58:11 +02:00
parent ad4099e9ce
commit a35d0739cc
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
7 changed files with 57 additions and 32 deletions

View file

@ -80,15 +80,15 @@ func (m moduleAttributeStore) Int64(name string) (int64, error) {
return 0, errValueNotSet return 0, errValueNotSet
} }
switch v.(type) { switch v := v.(type) {
case int: case int:
return int64(v.(int)), nil return int64(v), nil
case int16: case int16:
return int64(v.(int16)), nil return int64(v), nil
case int32: case int32:
return int64(v.(int32)), nil return int64(v), nil
case int64: case int64:
return v.(int64), nil return v, nil
} }
return 0, errValueMismatch return 0, errValueMismatch
@ -117,14 +117,14 @@ func (m moduleAttributeStore) StringSlice(name string) ([]string, error) {
return nil, errValueNotSet return nil, errValueNotSet
} }
switch v.(type) { switch v := v.(type) {
case []string: case []string:
return v.([]string), nil return v, nil
case []interface{}: case []interface{}:
var out []string var out []string
for _, iv := range v.([]interface{}) { for _, iv := range v {
sv, ok := iv.(string) sv, ok := iv.(string)
if !ok { if !ok {
return nil, errors.New("value in slice was not string") return nil, errors.New("value in slice was not string")

View file

@ -17,6 +17,10 @@ import (
* @module_desc Cleans up old messages from a channel (for example announcement channel) which are older than the retention time * @module_desc Cleans up old messages from a channel (for example announcement channel) which are older than the retention time
*/ */
const (
clearChannelNumberOfMessagesToLoad = 100
)
func init() { func init() {
RegisterModule("clearchannel", func() module { return &modClearChannel{} }) RegisterModule("clearchannel", func() module { return &modClearChannel{} })
} }
@ -79,15 +83,15 @@ func (m modClearChannel) cronClearChannel() {
} }
for { for {
msgs, err := m.discord.ChannelMessages(channelID, 100, "", after, "") msgs, err := m.discord.ChannelMessages(channelID, clearChannelNumberOfMessagesToLoad, "", after, "")
if err != nil { if err != nil {
log.WithError(err).Error("Unable to fetch announcement channel messages") log.WithError(err).Error("Unable to fetch announcement channel messages")
return return
} }
sort.Slice(msgs, func(i, j int) bool { sort.Slice(msgs, func(i, j int) bool {
iu, _ := strconv.ParseUint(msgs[i].ID, 10, 64) iu, _ := strconv.ParseUint(msgs[i].ID, 10, 64) //nolint: gomnd // These make no sense to define as constants
ju, _ := strconv.ParseUint(msgs[j].ID, 10, 64) ju, _ := strconv.ParseUint(msgs[j].ID, 10, 64) //nolint: gomnd // These make no sense to define as constants
return iu < ju return iu < ju
}) })

View file

@ -3,6 +3,7 @@ package main
import ( import (
"context" "context"
"net/url" "net/url"
"strconv"
"strings" "strings"
"time" "time"
@ -18,6 +19,16 @@ import (
* @module_desc Announces stream live status based on Discord streaming status * @module_desc Announces stream live status based on Discord streaming status
*/ */
const (
livePostingDefaultStreamFreshness = 5 * time.Minute
livePostingDiscordProfileHeight = 300
livePostingDiscordProfileWidth = 300
livePostingNumberOfMessagesToLoad = 100
livePostingPreviewHeight = 180
livePostingPreviewWidth = 320
livePostingTwitchColor = 0x6441a5
)
func init() { func init() {
RegisterModule("liveposting", func() module { return &modLivePosting{} }) RegisterModule("liveposting", func() module { return &modLivePosting{} })
} }
@ -46,6 +57,7 @@ func (m *modLivePosting) Initialize(crontab *cron.Cron, discord *discordgo.Sessi
return nil return nil
} }
//nolint: gocyclo // One directive too many, makes no sense to split
func (m modLivePosting) handlePresenceUpdate(d *discordgo.Session, p *discordgo.PresenceUpdate) { func (m modLivePosting) handlePresenceUpdate(d *discordgo.Session, p *discordgo.PresenceUpdate) {
if p.User == nil { if p.User == nil {
// The frick? Non-user presence? // The frick? Non-user presence?
@ -127,7 +139,7 @@ func (m modLivePosting) handlePresenceUpdate(d *discordgo.Session, p *discordgo.
} }
// @attr stream_freshness optional duration "5m" How long after stream start to post shoutout // @attr stream_freshness optional duration "5m" How long after stream start to post shoutout
ignoreTime := m.attrs.MustDuration("stream_freshness", ptrDuration(5*time.Minute)) ignoreTime := m.attrs.MustDuration("stream_freshness", ptrDuration(livePostingDefaultStreamFreshness))
if streams.Data[0].StartedAt.Add(ignoreTime).Before(time.Now()) { if streams.Data[0].StartedAt.Add(ignoreTime).Before(time.Now()) {
// Stream is too old, don't annoounce // Stream is too old, don't annoounce
return return
@ -156,12 +168,12 @@ func (m modLivePosting) sendLivePost(username, displayName, title, game, preview
) )
// @attr discord_channel_id required string "" ID of the Discord channel to post the message to // @attr discord_channel_id required string "" ID of the Discord channel to post the message to
msgs, err := m.discord.ChannelMessages(m.attrs.MustString("discord_channel_id", nil), 100, "", "", "") msgs, err := m.discord.ChannelMessages(m.attrs.MustString("discord_channel_id", nil), livePostingNumberOfMessagesToLoad, "", "", "")
if err != nil { if err != nil {
return errors.Wrap(err, "fetching previous messages") return errors.Wrap(err, "fetching previous messages")
} }
ignoreTime := m.attrs.MustDuration("stream_freshness", ptrDuration(5*time.Minute)) ignoreTime := m.attrs.MustDuration("stream_freshness", ptrDuration(livePostingDefaultStreamFreshness))
for _, msg := range msgs { for _, msg := range msgs {
mt, err := msg.Timestamp.Parse() mt, err := msg.Timestamp.Parse()
if err != nil { if err != nil {
@ -177,19 +189,19 @@ func (m modLivePosting) sendLivePost(username, displayName, title, game, preview
Name: displayName, Name: displayName,
IconURL: profileImage, IconURL: profileImage,
}, },
Color: 0x6441a5, Color: livePostingTwitchColor,
Fields: []*discordgo.MessageEmbedField{ Fields: []*discordgo.MessageEmbedField{
{Name: "Game", Value: game}, {Name: "Game", Value: game},
}, },
Image: &discordgo.MessageEmbedImage{ Image: &discordgo.MessageEmbedImage{
URL: strings.NewReplacer("{width}", "320", "{height}", "180").Replace(previewImage), URL: strings.NewReplacer("{width}", strconv.Itoa(livePostingPreviewWidth), "{height}", strconv.Itoa(livePostingPreviewHeight)).Replace(previewImage),
Width: 320, Width: livePostingPreviewWidth,
Height: 180, Height: livePostingPreviewHeight,
}, },
Thumbnail: &discordgo.MessageEmbedThumbnail{ Thumbnail: &discordgo.MessageEmbedThumbnail{
URL: profileImage, URL: profileImage,
Width: 300, Width: livePostingDiscordProfileWidth,
Height: 300, Height: livePostingDiscordProfileHeight,
}, },
Title: title, Title: title,
Type: discordgo.EmbedTypeRich, Type: discordgo.EmbedTypeRich,

View file

@ -83,7 +83,7 @@ func (m modLiveRole) handlePresenceUpdate(d *discordgo.Session, p *discordgo.Pre
return return
} }
var exitFunc func(string, string, []string) error = m.removeLiveStreamerRole var exitFunc func(string, string, []string) error
defer func() { defer func() {
if exitFunc != nil { if exitFunc != nil {
if err := exitFunc(p.GuildID, p.User.ID, member.Roles); err != nil { if err := exitFunc(p.GuildID, p.User.ID, member.Roles); err != nil {

View file

@ -18,6 +18,10 @@ import (
* @module_desc Updates the presence status of the bot to display the next stream * @module_desc Updates the presence status of the bot to display the next stream
*/ */
const (
presenceTimeDay = 24 * time.Hour
)
func init() { func init() {
RegisterModule("presence", func() module { return &modPresence{} }) RegisterModule("presence", func() module { return &modPresence{} })
} }
@ -49,7 +53,7 @@ func (m *modPresence) Initialize(crontab *cron.Cron, discord *discordgo.Session,
} }
func (m modPresence) cronUpdatePresence() { func (m modPresence) cronUpdatePresence() {
var nextStream *time.Time = nil var nextStream *time.Time
twitch := newTwitchAdapter( twitch := newTwitchAdapter(
// @attr twitch_client_id required string "" Twitch client ID the token was issued for // @attr twitch_client_id required string "" Twitch client ID the token was issued for
@ -95,8 +99,8 @@ func (m modPresence) cronUpdatePresence() {
func (m modPresence) durationToHumanReadable(d time.Duration) string { func (m modPresence) durationToHumanReadable(d time.Duration) string {
d = time.Duration(math.Abs(float64(d))) d = time.Duration(math.Abs(float64(d)))
if d > time.Hour*24 { if d > presenceTimeDay {
return fmt.Sprintf("%.0f Tagen", math.Round(float64(d)/float64(time.Hour*24))) return fmt.Sprintf("%.0f Tagen", math.Round(float64(d)/float64(presenceTimeDay)))
} }
var elements []string var elements []string

View file

@ -17,9 +17,14 @@ import (
* @module_desc Posts stream schedule derived from Twitch schedule as embed in Discord channel * @module_desc Posts stream schedule derived from Twitch schedule as embed in Discord channel
*/ */
const (
streamScheduleDefaultColor = 0x2ECC71
streamScheduleNumberOfMessagesToLoad = 100
)
var ( var (
defaultStreamScheduleEntries = ptrInt64(5) defaultStreamScheduleEntries = ptrInt64(5) //nolint: gomnd // This is already the "constant"
defaultStreamSchedulePastTime = ptrDuration(15 * time.Minute) defaultStreamSchedulePastTime = ptrDuration(15 * time.Minute) //nolint: gomnd // This is already the "constant"
) )
func init() { func init() {
@ -75,13 +80,13 @@ func (m modStreamSchedule) cronUpdateSchedule() {
} }
msgEmbed := &discordgo.MessageEmbed{ msgEmbed := &discordgo.MessageEmbed{
// @attr embed_color optional int64 "3066993" Integer representation of the hex color for the embed (default is #2ECC71) // @attr embed_color optional int64 "0x2ECC71" Integer representation of the hex color for the embed
Color: int(m.attrs.MustInt64("embed_color", ptrInt64(3066993))), Color: int(m.attrs.MustInt64("embed_color", ptrInt64(streamScheduleDefaultColor))),
// @attr embed_description optional string "" Description for the embed block // @attr embed_description optional string "" Description for the embed block
Description: m.attrs.MustString("embed_description", ptrStringEmpty), Description: m.attrs.MustString("embed_description", ptrStringEmpty),
Fields: []*discordgo.MessageEmbedField{}, Fields: []*discordgo.MessageEmbedField{},
Thumbnail: &discordgo.MessageEmbedThumbnail{ Thumbnail: &discordgo.MessageEmbedThumbnail{
// @attr embed_thumbnail_url optional string "" Publically hosted image URL to use as thumbnail // @attr embed_thumbnail_url optional string "" Publically hosted image URL to u100se as thumbnail
URL: m.attrs.MustString("embed_thumbnail_url", ptrStringEmpty), URL: m.attrs.MustString("embed_thumbnail_url", ptrStringEmpty),
// @attr embed_thumbnail_width optional int64 "" Width of the thumbnail // @attr embed_thumbnail_width optional int64 "" Width of the thumbnail
Width: int(m.attrs.MustInt64("embed_thumbnail_width", ptrInt64Zero)), Width: int(m.attrs.MustInt64("embed_thumbnail_width", ptrInt64Zero)),
@ -117,7 +122,7 @@ func (m modStreamSchedule) cronUpdateSchedule() {
} }
// @attr discord_channel_id required string "" ID of the Discord channel to post the message to // @attr discord_channel_id required string "" ID of the Discord channel to post the message to
msgs, err := m.discord.ChannelMessages(m.attrs.MustString("discord_channel_id", nil), 100, "", "", "") msgs, err := m.discord.ChannelMessages(m.attrs.MustString("discord_channel_id", nil), streamScheduleNumberOfMessagesToLoad, "", "", "")
if err != nil { if err != nil {
log.WithError(err).Error("Unable to fetch announcement channel messages") log.WithError(err).Error("Unable to fetch announcement channel messages")
return return

View file

@ -77,10 +77,10 @@ Posts stream schedule derived from Twitch schedule as embed in Discord channel
| `twitch_client_id` | ✅ | string | | Twitch client ID the token was issued for | | `twitch_client_id` | ✅ | string | | Twitch client ID the token was issued for |
| `twitch_token` | ✅ | string | | Token for the user the `twitch_channel_id` belongs to | | `twitch_token` | ✅ | string | | Token for the user the `twitch_channel_id` belongs to |
| `cron` | | string | `*/10 * * * *` | When to execute the schedule transfer | | `cron` | | string | `*/10 * * * *` | When to execute the schedule transfer |
| `embed_color` | | int64 | `3066993` | Integer representation of the hex color for the embed (default is #2ECC71) | | `embed_color` | | int64 | `0x2ECC71` | Integer representation of the hex color for the embed |
| `embed_description` | | string | | Description for the embed block | | `embed_description` | | string | | Description for the embed block |
| `embed_thumbnail_height` | | int64 | | Height of the thumbnail | | `embed_thumbnail_height` | | int64 | | Height of the thumbnail |
| `embed_thumbnail_url` | | string | | Publically hosted image URL to use as thumbnail | | `embed_thumbnail_url` | | string | | Publically hosted image URL to u100se as thumbnail |
| `embed_thumbnail_width` | | int64 | | Width of the thumbnail | | `embed_thumbnail_width` | | int64 | | Width of the thumbnail |
| `schedule_entries` | | int64 | `5` | How many schedule entries to add to the embed as fields | | `schedule_entries` | | int64 | `5` | How many schedule entries to add to the embed as fields |
| `schedule_past_time` | | duration | `15m` | How long in the past should the schedule contain an entry | | `schedule_past_time` | | duration | `15m` | How long in the past should the schedule contain an entry |