2020-06-27 16:48:33 +00:00
package discord
import (
"fmt"
"math"
"strconv"
2021-01-08 19:15:11 +00:00
"strings"
2020-06-27 16:48:33 +00:00
2020-07-19 10:16:02 +00:00
"github.com/tribalwarshelp/dcbot/message"
"github.com/nicksnyder/go-i18n/v2/i18n"
2020-06-27 16:48:33 +00:00
shared_models "github.com/tribalwarshelp/shared/models"
2020-10-31 11:16:21 +00:00
"github.com/tribalwarshelp/shared/tw"
2020-06-27 16:48:33 +00:00
"github.com/bwmarrin/discordgo"
2021-02-06 09:36:13 +00:00
"github.com/dustin/go-humanize"
2020-06-27 16:48:33 +00:00
"github.com/tribalwarshelp/golang-sdk/sdk"
)
const (
HelpCommand Command = "help"
TribeCommand Command = "tribe"
2020-08-08 12:02:32 +00:00
TopODACommand Command = "topoda"
TopODDCommand Command = "topodd"
TopODSCommand Command = "topods"
TopODCommand Command = "topod"
2020-06-27 16:48:33 +00:00
TopPointsCommand Command = "toppoints"
AuthorCommand Command = "author"
)
2020-10-17 08:27:58 +00:00
func ( s * Session ) handleHelpCommand ( ctx * commandCtx , m * discordgo . MessageCreate , args ... string ) {
2020-06-27 16:48:33 +00:00
commandsForAll := fmt . Sprintf ( `
2020-07-19 10:42:22 +00:00
- % s
- % s
- % s
- % s
- % s
- % s
2020-06-27 16:48:33 +00:00
` ,
2020-07-19 10:42:22 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpTribeTopODA ,
DefaultMessage : message . FallbackMsg ( message . HelpTribeTopODA ,
2021-01-08 19:15:11 +00:00
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by ODA." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
2020-10-31 11:16:21 +00:00
"Command" : TribeCommand . WithPrefix ( s . cfg . CommandPrefix ) + " " + TopODACommand ,
2020-07-19 10:42:22 +00:00
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpTribeTopODD ,
DefaultMessage : message . FallbackMsg ( message . HelpTribeTopODD ,
2021-01-08 19:15:11 +00:00
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by ODD." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
2020-10-31 11:16:21 +00:00
"Command" : TribeCommand . WithPrefix ( s . cfg . CommandPrefix ) + " " + TopODDCommand ,
2020-07-19 10:42:22 +00:00
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpTribeTopODS ,
DefaultMessage : message . FallbackMsg ( message . HelpTribeTopODS ,
2021-01-08 19:15:11 +00:00
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by ODS." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
2020-10-31 11:16:21 +00:00
"Command" : TribeCommand . WithPrefix ( s . cfg . CommandPrefix ) + " " + TopODSCommand ,
2020-07-19 10:42:22 +00:00
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpTribeTopOD ,
DefaultMessage : message . FallbackMsg ( message . HelpTribeTopOD ,
2021-01-08 19:15:11 +00:00
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by OD." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
2020-10-31 11:16:21 +00:00
"Command" : TribeCommand . WithPrefix ( s . cfg . CommandPrefix ) + " " + TopODCommand ,
2020-07-19 10:42:22 +00:00
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpTribeTopPoints ,
DefaultMessage : message . FallbackMsg ( message . HelpTribeTopPoints ,
2021-01-08 19:15:11 +00:00
"**{{.Command}}** [server] [page] [tribe id or tribe tag, you can enter more than one] - generates a player list from selected tribes ordered by points." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
2020-10-31 11:16:21 +00:00
"Command" : TribeCommand . WithPrefix ( s . cfg . CommandPrefix ) + " " + TopPointsCommand ,
2020-07-19 10:42:22 +00:00
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpAuthor ,
DefaultMessage : message . FallbackMsg ( message . HelpAuthor ,
2020-08-08 12:02:32 +00:00
"**{{.Command}}** - shows how to get in touch with the author." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : AuthorCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
2020-06-27 16:48:33 +00:00
)
commandsForGuildAdmins := fmt . Sprintf ( `
2020-07-19 10:42:22 +00:00
- % s
- % s
- % s
- % s
- % s
- % s
- % s
- % s
- % s
2020-06-27 16:48:33 +00:00
` ,
2020-07-19 10:42:22 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpAddGroup ,
DefaultMessage : message . FallbackMsg ( message . HelpAddGroup ,
2020-07-19 13:03:53 +00:00
"**{{.Command}}** - adds a new observation group." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : AddGroupCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpGroups ,
DefaultMessage : message . FallbackMsg ( message . HelpGroups ,
2020-08-08 12:02:32 +00:00
"**{{.Command}}** - shows you a list of groups created by this server." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpDeleteGroup ,
DefaultMessage : message . FallbackMsg ( message . HelpDeleteGroup ,
2020-07-19 13:03:53 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] - deletes an observation group." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : DeleteGroupCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpShowEnnobledBarbs ,
DefaultMessage : message . FallbackMsg ( message . HelpShowEnnobledBarbs ,
2020-07-19 13:03:53 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] - enables/disables notifications about ennobling barbarian villages." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : ShowEnnobledBarbariansCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpObserve ,
DefaultMessage : message . FallbackMsg ( message . HelpObserve ,
2021-01-08 19:15:11 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] [server] [tribe id or tribe tag] - adds a tribe to the observation group." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : ObserveCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpGroups ,
DefaultMessage : message . FallbackMsg ( message . HelpGroups ,
2020-10-17 05:58:10 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] - shows a list of monitored tribes added to this group." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : ObservationsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpDeleteObservation ,
DefaultMessage : message . FallbackMsg ( message . HelpDeleteObservation ,
2020-07-19 13:03:53 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] [id from {{.ObservationsCommand}}] - removes a tribe from the observation group." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
2020-07-19 14:56:05 +00:00
"Command" : DeleteObservationCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
2020-07-19 10:42:22 +00:00
"ObservationsCommand" : ObservationsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpConqueredVillages ,
DefaultMessage : message . FallbackMsg ( message . HelpConqueredVillages ,
2020-07-19 13:03:53 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] - changes the channel on which notifications about conquered village will show. IMPORTANT! Call this command on the channel you want to display these notifications." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : ConqueredVillagesCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpDisableConqueredVillages ,
DefaultMessage : message . FallbackMsg ( message . HelpDisableConqueredVillages ,
2020-09-03 13:13:49 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] - disables notifications about conquered villages." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
2020-07-19 14:56:05 +00:00
"Command" : DisableConqueredVillagesCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
2020-07-19 10:42:22 +00:00
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
2020-07-15 10:17:33 +00:00
)
commandsForGuildAdmins2 := fmt . Sprintf ( `
2020-07-19 10:42:22 +00:00
- % s
2020-07-19 15:21:44 +00:00
- % s
2020-07-21 12:01:08 +00:00
- % s
2020-08-09 15:20:23 +00:00
- % s
- % s
2020-07-19 10:42:22 +00:00
- % s
2020-07-15 10:17:33 +00:00
` ,
2020-07-19 10:42:22 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpLostVillages ,
DefaultMessage : message . FallbackMsg ( message . HelpLostVillages ,
2020-07-19 15:21:44 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] - changes the channel on which notifications about lost village will show. IMPORTANT! Call this command on the channel you want to display these notifications." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : LostVillagesCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpDisableLostVillages ,
DefaultMessage : message . FallbackMsg ( message . HelpDisableLostVillages ,
2020-07-19 15:21:44 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] - changes the channel on which notifications about lost village will show. IMPORTANT! Call this command on the channel you want to display these notifications." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
2020-07-19 14:56:05 +00:00
"Command" : DisableLostVillagesCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
2020-07-19 10:42:22 +00:00
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
2020-08-09 15:20:23 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpShowInternals ,
DefaultMessage : message . FallbackMsg ( message . HelpShowInternals ,
2020-08-09 15:20:23 +00:00
"**{{.Command}}** [group id from {{.GroupsCommand}}] - enables/disables notifications about in-group/in-tribe conquering." ) ,
TemplateData : map [ string ] interface { } {
"Command" : ShowInternalsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
2020-07-19 15:21:44 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpChangageLanguage ,
DefaultMessage : message . FallbackMsg ( message . HelpChangageLanguage ,
2020-09-03 13:13:49 +00:00
"**{{.Command}}** [{{.Languages}}] - changes language." ) ,
2020-07-19 15:21:44 +00:00
TemplateData : map [ string ] interface { } {
"Command" : ChangeLanguageCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"Languages" : getAvailableLanguages ( ) ,
} ,
} ) ,
2020-07-21 12:01:08 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpCoordsTranslation ,
DefaultMessage : message . FallbackMsg ( message . HelpCoordsTranslation ,
2020-09-03 13:13:49 +00:00
"**{{.Command}}** [server] - enables coords translation feature." ) ,
2020-07-21 12:01:08 +00:00
TemplateData : map [ string ] interface { } {
2020-08-09 15:20:23 +00:00
"Command" : CoordsTranslationCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpDisableCoordsTranslation ,
DefaultMessage : message . FallbackMsg ( message . HelpDisableCoordsTranslation ,
2020-09-03 13:13:49 +00:00
"**{{.Command}}** - disables coords translation feature." ) ,
2020-08-09 15:20:23 +00:00
TemplateData : map [ string ] interface { } {
"Command" : DisableCoordsTranslationCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
2020-07-21 12:01:08 +00:00
} ,
} ) ,
2020-06-27 16:48:33 +00:00
)
2020-07-19 10:16:02 +00:00
forAdmins := ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpForAdmins ,
DefaultMessage : message . FallbackMsg ( message . HelpForAdmins , "For admins" ) ,
2020-07-19 10:16:02 +00:00
} )
2020-06-27 16:48:33 +00:00
s . SendEmbed ( m . ChannelID , NewEmbed ( ) .
2020-07-19 10:16:02 +00:00
SetTitle ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpTitle ,
DefaultMessage : message . FallbackMsg ( message . HelpTitle , "Help" ) ,
2020-07-19 10:16:02 +00:00
} ) ) .
2020-07-19 14:56:05 +00:00
SetURL ( "https://dcbot.tribalwarshelp.com/" ) .
2020-07-19 10:16:02 +00:00
SetDescription ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpDescription ,
DefaultMessage : message . FallbackMsg ( message . HelpDescription , "Commands offered by bot" ) ,
2020-07-19 10:16:02 +00:00
} ) ) .
AddField ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 03:45:31 +00:00
MessageID : message . HelpForAllUsers ,
2020-10-17 08:27:58 +00:00
DefaultMessage : message . FallbackMsg ( message . HelpForAllUsers , "For everyone" ) ,
2020-07-19 10:16:02 +00:00
} ) , commandsForAll ) .
AddField ( forAdmins , commandsForGuildAdmins ) .
AddField ( forAdmins + " 2" , commandsForGuildAdmins2 ) .
2020-06-27 16:48:33 +00:00
MessageEmbed )
}
2020-10-17 08:27:58 +00:00
func ( s * Session ) handleAuthorCommand ( ctx * commandCtx , m * discordgo . MessageCreate , args ... string ) {
2020-07-19 13:03:53 +00:00
s . SendMessage ( m . ChannelID ,
2021-01-16 21:08:32 +00:00
fmt . Sprintf ( "%s Discord: Kichiyaki#2064 | https://dwysokinski.me/#contact." ,
2020-07-19 13:03:53 +00:00
m . Author . Mention ( ) ) )
2020-06-27 16:48:33 +00:00
}
2020-10-17 08:27:58 +00:00
func ( s * Session ) handleTribeCommand ( ctx * commandCtx , m * discordgo . MessageCreate , args ... string ) {
2020-06-27 16:48:33 +00:00
argsLength := len ( args )
2020-07-19 11:22:59 +00:00
if argsLength < 3 {
2020-06-27 16:48:33 +00:00
return
}
command := Command ( args [ 0 ] )
2020-11-06 19:37:49 +00:00
server := args [ 1 ]
2020-06-27 16:48:33 +00:00
page , err := strconv . Atoi ( args [ 2 ] )
if err != nil || page <= 0 {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeInvalidPage ,
DefaultMessage : message . FallbackMsg ( message . TribeInvalidPage , "{{.Mention}} The page must be a number greater than 0." ) ,
2020-07-19 11:22:59 +00:00
TemplateData : map [ string ] interface { } {
"Mention" : m . Author . Mention ( ) ,
} ,
} ) )
2020-06-27 16:48:33 +00:00
return
}
ids := [ ] int { }
2021-01-08 19:15:11 +00:00
tags := [ ] string { }
2020-06-27 16:48:33 +00:00
for _ , arg := range args [ 3 : argsLength ] {
2021-01-08 19:15:11 +00:00
trimmed := strings . TrimSpace ( arg )
if trimmed == "" {
2020-06-27 16:48:33 +00:00
continue
}
2021-01-08 19:15:11 +00:00
id , err := strconv . Atoi ( trimmed )
if err != nil || id <= 0 {
tags = append ( tags , trimmed )
} else {
ids = append ( ids , id )
}
2020-06-27 16:48:33 +00:00
}
2021-01-08 19:15:11 +00:00
if len ( ids ) == 0 && len ( tags ) == 0 {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeNoTribeID ,
2021-01-08 19:15:11 +00:00
DefaultMessage : message . FallbackMsg ( message . TribeNoTribeID , "{{.Mention}} At least one tribe id/tag is required." ) ,
2020-07-19 11:22:59 +00:00
TemplateData : map [ string ] interface { } {
"Mention" : m . Author . Mention ( ) ,
} ,
} ) )
2020-06-27 16:48:33 +00:00
return
}
exists := true
limit := 10
offset := ( page - 1 ) * limit
filter := & shared_models . PlayerFilter {
2021-01-08 19:15:11 +00:00
Exists : & exists ,
TribeFilter : & shared_models . TribeFilter {
Or : & shared_models . TribeFilterOr {
ID : ids ,
Tag : tags ,
} ,
} ,
2020-06-27 16:48:33 +00:00
}
title := ""
2020-11-21 12:13:29 +00:00
sort := ""
2020-06-27 16:48:33 +00:00
switch command {
2020-08-08 12:02:32 +00:00
case TopODACommand :
2020-06-27 16:48:33 +00:00
filter . RankAttGTE = 1
2020-11-21 12:13:29 +00:00
sort = "rankAtt ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeTitleOrderedByODA ,
DefaultMessage : message . FallbackMsg ( message . TribeTitleOrderedByODA , "Ordered by ODA" ) ,
2020-07-19 11:22:59 +00:00
} )
2020-08-08 12:02:32 +00:00
case TopODDCommand :
2020-06-27 16:48:33 +00:00
filter . RankDefGTE = 1
2020-11-21 12:13:29 +00:00
sort = "rankDef ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeTitleOrderedByODD ,
DefaultMessage : message . FallbackMsg ( message . TribeTitleOrderedByODD , "Ordered by ODD" ) ,
2020-07-19 11:22:59 +00:00
} )
2020-08-08 12:02:32 +00:00
case TopODSCommand :
2020-06-27 16:48:33 +00:00
filter . RankSupGTE = 1
2020-11-21 12:13:29 +00:00
sort = "rankSup ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeTitleOrderedByODS ,
DefaultMessage : message . FallbackMsg ( message . TribeTitleOrderedByODS , "Ordered by ODS" ) ,
2020-07-19 11:22:59 +00:00
} )
2020-08-08 12:02:32 +00:00
case TopODCommand :
2020-06-27 16:48:33 +00:00
filter . RankTotalGTE = 1
2020-11-21 12:13:29 +00:00
sort = "rankTotal ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeTitleOrderedByOD ,
DefaultMessage : message . FallbackMsg ( message . TribeTitleOrderedByOD , "Ordered by OD" ) ,
2020-07-19 11:22:59 +00:00
} )
2020-06-27 16:48:33 +00:00
case TopPointsCommand :
2020-11-21 12:13:29 +00:00
sort = "rank ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeTitleOrderedByPoints ,
DefaultMessage : message . FallbackMsg ( message . TribeTitleOrderedByPoints , "Ordered by points" ) ,
2020-07-19 11:22:59 +00:00
} )
2020-06-27 16:48:33 +00:00
default :
return
}
2020-11-21 12:13:29 +00:00
playerList , err := s . cfg . API . Player . Browse ( server ,
limit ,
offset ,
[ ] string { sort } ,
filter ,
& sdk . PlayerInclude {
Tribe : true ,
} )
2020-06-27 16:48:33 +00:00
if err != nil {
s . SendMessage ( m . ChannelID ,
2020-07-19 11:22:59 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . ApiDefaultError ,
DefaultMessage : message . FallbackMsg ( message . ApiDefaultError ,
2021-01-13 19:48:19 +00:00
"{{.Mention}} Can't fetch data from the API at the moment, please try again later." ) ,
2020-07-19 11:22:59 +00:00
TemplateData : map [ string ] interface { } {
"Mention" : m . Author . Mention ( ) ,
} ,
} ) )
2020-06-27 16:48:33 +00:00
return
}
2020-10-14 04:06:15 +00:00
if playerList == nil || playerList . Total == 0 {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeTribesNotFound ,
DefaultMessage : message . FallbackMsg ( message . TribeTribesNotFound , "{{.Mention}} Tribes not found." ) ,
2020-07-19 11:22:59 +00:00
TemplateData : map [ string ] interface { } {
"Mention" : m . Author . Mention ( ) ,
} ,
} ) )
2020-06-27 16:48:33 +00:00
return
}
2020-10-14 04:06:15 +00:00
totalPages := int ( math . Ceil ( float64 ( playerList . Total ) / float64 ( limit ) ) )
2020-06-27 16:48:33 +00:00
if page > totalPages {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeExceededMaximumNumberOfPages ,
DefaultMessage : message . FallbackMsg ( message . TribeExceededMaximumNumberOfPages ,
2020-07-19 13:03:53 +00:00
"{{.Mention}} You have exceeded the maximum number of pages ({{.Page}}/{{.MaxPage}})." ) ,
2020-07-19 11:22:59 +00:00
TemplateData : map [ string ] interface { } {
"Mention" : m . Author . Mention ( ) ,
"Page" : page ,
"MaxPage" : totalPages ,
} ,
} ) )
2020-06-27 16:48:33 +00:00
return
}
2020-11-06 19:37:49 +00:00
code := tw . VersionCodeFromServerKey ( server )
version , err := s . cfg . API . Version . Read ( code )
if err != nil || version == nil {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . InternalServerError ,
DefaultMessage : message . FallbackMsg ( message . InternalServerError ,
2020-10-27 07:27:35 +00:00
"{{.Mention}} An internal server error has occurred, please try again later." ) ,
2020-07-19 11:22:59 +00:00
TemplateData : map [ string ] interface { } {
"Mention" : m . Author . Mention ( ) ,
} ,
} ) )
2020-06-27 16:48:33 +00:00
return
}
2020-10-17 06:47:39 +00:00
msg := & MessageEmbed { }
2020-10-14 04:06:15 +00:00
for i , player := range playerList . Items {
2020-06-27 16:48:33 +00:00
if player == nil {
continue
}
rank := 0
score := 0
switch command {
2020-08-08 12:02:32 +00:00
case TopODACommand :
2020-06-27 16:48:33 +00:00
rank = player . RankAtt
score = player . ScoreAtt
2020-08-08 12:02:32 +00:00
case TopODDCommand :
2020-06-27 16:48:33 +00:00
rank = player . RankDef
score = player . ScoreDef
2020-08-08 12:02:32 +00:00
case TopODSCommand :
2020-06-27 16:48:33 +00:00
rank = player . RankSup
score = player . ScoreSup
2020-08-08 12:02:32 +00:00
case TopODCommand :
2020-06-27 16:48:33 +00:00
rank = player . RankTotal
score = player . ScoreTotal
case TopPointsCommand :
rank = player . Rank
score = player . Points
}
tribeTag := "-"
tribeURL := "-"
if player . Tribe != nil {
tribeTag = player . Tribe . Tag
2020-11-06 19:37:49 +00:00
tribeURL = tw . BuildTribeURL ( server , version . Host , player . Tribe . ID )
2020-06-27 16:48:33 +00:00
}
2020-07-19 11:22:59 +00:00
msg . Append ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . TribeMessageLine ,
DefaultMessage : message . FallbackMsg ( message . TribeMessageLine ,
2021-02-06 09:36:13 +00:00
"**{{.Index}}**. [`{{.PlayerName}}`]({{.PlayerURL}}) (Tribe: [`{{.TribeTag}}`]({{.TribeURL}}) | Rank: **{{.Rank}}** | Score: **{{.Score}}**)\n" ) ,
2020-07-19 11:22:59 +00:00
TemplateData : map [ string ] interface { } {
"Index" : offset + i + 1 ,
"PlayerName" : player . Name ,
2020-11-06 19:37:49 +00:00
"PlayerURL" : tw . BuildPlayerURL ( server , version . Host , player . ID ) ,
2020-07-19 11:22:59 +00:00
"TribeTag" : tribeTag ,
"TribeURL" : tribeURL ,
"Rank" : rank ,
2021-02-06 09:36:13 +00:00
"Score" : humanize . Comma ( int64 ( score ) ) ,
2020-07-19 11:22:59 +00:00
} ,
} ) )
2020-06-27 16:48:33 +00:00
}
s . SendEmbed ( m . ChannelID , NewEmbed ( ) .
SetTitle ( title ) .
SetFields ( msg . ToMessageEmbedFields ( ) ) .
2020-07-19 11:22:59 +00:00
SetFooter ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-10-14 04:06:15 +00:00
MessageID : message . PaginationLabelDisplayedPage ,
DefaultMessage : message . FallbackMsg ( message . PaginationLabelDisplayedPage , "{{.Page}} of {{.MaxPage}}" ) ,
2020-07-19 11:22:59 +00:00
TemplateData : map [ string ] interface { } {
"Page" : page ,
"MaxPage" : totalPages ,
} ,
} ) ) .
2020-06-27 16:48:33 +00:00
MessageEmbed )
}