2020-06-27 16:48:33 +00:00
package discord
import (
"fmt"
"math"
"strconv"
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"
"github.com/bwmarrin/discordgo"
"github.com/tribalwarshelp/dcbot/utils"
"github.com/tribalwarshelp/golang-sdk/sdk"
)
const (
HelpCommand Command = "help"
TribeCommand Command = "tribe"
TopAttCommand Command = "topatt"
TopDefCommand Command = "topdef"
TopSuppCommand Command = "topsupp"
TopTotalCommand Command = "toptotal"
TopPointsCommand Command = "toppoints"
AuthorCommand Command = "author"
)
2020-07-19 10:16:02 +00:00
func ( s * Session ) handleHelpCommand ( ctx commandCtx , m * discordgo . MessageCreate ) {
2020-06-27 16:48:33 +00:00
tribeCMDWithPrefix := TribeCommand . WithPrefix ( s . cfg . CommandPrefix )
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-07-19 13:03:53 +00:00
MessageID : "help.tribe.topatt" ,
DefaultMessage : message . FallbackMsg ( "help.tribe.topatt" ,
"**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODA." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : tribeCMDWithPrefix + " " + TopAttCommand . String ( ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "help.tribe.topdef" ,
DefaultMessage : message . FallbackMsg ( "help.tribe.topdef" ,
"**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODD." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : tribeCMDWithPrefix + " " + TopDefCommand . String ( ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "help.tribe.topsupp" ,
DefaultMessage : message . FallbackMsg ( "help.tribe.topsupp" ,
"**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by ODS." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : tribeCMDWithPrefix + " " + TopSuppCommand . String ( ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "help.tribe.toptotal" ,
DefaultMessage : message . FallbackMsg ( "help.tribe.toptotal" ,
"**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by OD." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : tribeCMDWithPrefix + " " + TopTotalCommand . String ( ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "help.tribe.toppoints" ,
DefaultMessage : message . FallbackMsg ( "help.tribe.toppoints" ,
"**{{.Command}}** [server] [page] [id1] [id2] [id3] [n id] - generates a player list from selected tribes ordered by points." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : tribeCMDWithPrefix + " " + TopPointsCommand . String ( ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "help.author" ,
DefaultMessage : message . FallbackMsg ( "help.author" ,
"**{{.Command}}** - shows how to contact 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-07-19 13:03:53 +00:00
MessageID : "help.addgroup" ,
DefaultMessage : message . FallbackMsg ( "help.addgroup" ,
"**{{.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-07-19 13:03:53 +00:00
MessageID : "help.groups" ,
DefaultMessage : message . FallbackMsg ( "help.groups" ,
"**{{.Command}}** - shows you a list of groups created by this guild." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "help.deletegroup" ,
DefaultMessage : message . FallbackMsg ( "help.deletegroup" ,
"**{{.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-07-19 13:03:53 +00:00
MessageID : "help.showennobledbarbs" ,
DefaultMessage : message . FallbackMsg ( "help.showennobledbarbs" ,
"**{{.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-07-19 13:03:53 +00:00
MessageID : "help.observe" ,
DefaultMessage : message . FallbackMsg ( "help.observe" ,
"**{{.Command}}** [group id from {{.GroupsCommand}}] [server] [tribe id] - command 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-07-19 13:03:53 +00:00
MessageID : "help.observations" ,
DefaultMessage : message . FallbackMsg ( "help.observations" ,
"**{{.Command}}** [group id from {{.GroupsCommand}}] shows a list of observed tribes by 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-07-19 13:03:53 +00:00
MessageID : "help.unobserve" ,
DefaultMessage : message . FallbackMsg ( "help.unobserve" ,
"**{{.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 { } {
"Command" : UnObserveCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"ObservationsCommand" : ObservationsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "help.conqueredvillages" ,
DefaultMessage : message . FallbackMsg ( "help.conqueredvillages" ,
"**{{.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-07-19 13:03:53 +00:00
MessageID : "help.unobserveconqueredvillages" ,
DefaultMessage : message . FallbackMsg ( "help.unobserveconqueredvillages" ,
"**{{.Command}}** [group id from {{.GroupsCommand}}] - disable notifications about conquered villages." ) ,
2020-07-19 10:42:22 +00:00
TemplateData : map [ string ] interface { } {
"Command" : UnObserveConqueredVillagesCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"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
- % s
2020-07-15 10:17:33 +00:00
` ,
2020-07-19 10:42:22 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "help.lostvillages" ,
DefaultMessage : message . FallbackMsg ( "help.lostvillages" ,
"**{{.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-07-19 13:03:53 +00:00
MessageID : "help.unobservelostvillages" ,
DefaultMessage : message . FallbackMsg ( "help.unobservelostvillages" ,
"*{{.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" : UnObserveLostVillagesCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
"GroupsCommand" : GroupsCommand . WithPrefix ( s . cfg . CommandPrefix ) ,
} ,
} ) ,
2020-06-27 16:48:33 +00:00
)
2020-07-19 10:16:02 +00:00
forAdmins := ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "help.forAdmins" ,
DefaultMessage : message . FallbackMsg ( "help.forAdmins" , "For admins" ) ,
} )
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 {
MessageID : "help.title" ,
DefaultMessage : message . FallbackMsg ( "help.title" , "Help" ) ,
} ) ) .
SetDescription ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "help.description" ,
DefaultMessage : message . FallbackMsg ( "help.description" , "Commands offered by bot" ) ,
} ) ) .
SetFooter ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "help.footer" ,
DefaultMessage : message . FallbackMsg ( "help.footer" , "Check bot website -> https://dcbot.tribalwarshelp.com/." ) ,
} ) ) .
AddField ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "help.forAllUsers" ,
DefaultMessage : message . FallbackMsg ( "help.forAllUsers" , "For all guild members." ) ,
} ) , commandsForAll ) .
AddField ( forAdmins , commandsForGuildAdmins ) .
AddField ( forAdmins + " 2" , commandsForGuildAdmins2 ) .
2020-06-27 16:48:33 +00:00
MessageEmbed )
}
func ( s * Session ) handleAuthorCommand ( m * discordgo . MessageCreate ) {
2020-07-19 13:03:53 +00:00
s . SendMessage ( m . ChannelID ,
fmt . Sprintf ( "%s Discord: Kichiyaki#2064 | https://dawid-wysokinski.pl/#contact." ,
m . Author . Mention ( ) ) )
2020-06-27 16:48:33 +00:00
}
2020-07-19 11:22:59 +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 ] )
world := args [ 1 ]
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 {
MessageID : "tribe.invalidPage" ,
DefaultMessage : message . FallbackMsg ( "tribe.invalidPage" , "{{.Mention}} The page must be a number greater than 0." ) ,
TemplateData : map [ string ] interface { } {
"Mention" : m . Author . Mention ( ) ,
} ,
} ) )
2020-06-27 16:48:33 +00:00
return
}
ids := [ ] int { }
for _ , arg := range args [ 3 : argsLength ] {
id , err := strconv . Atoi ( arg )
if err != nil || id <= 0 {
continue
}
ids = append ( ids , id )
}
if len ( ids ) == 0 {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "tribe.noTribeID" ,
DefaultMessage : message . FallbackMsg ( "tribe.noTribeID" , "{{.Mention}} You haven't entered the tribe ID." ) ,
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 {
Exists : & exists ,
TribeID : ids ,
Limit : limit ,
Offset : offset ,
}
title := ""
switch command {
case TopAttCommand :
filter . RankAttGTE = 1
filter . Sort = "rankAtt ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "tribe.title.sortedByODA" ,
DefaultMessage : message . FallbackMsg ( "tribe.title.sortedByODA" , "Sorted by ODA" ) ,
} )
2020-06-27 16:48:33 +00:00
case TopDefCommand :
filter . RankDefGTE = 1
filter . Sort = "rankDef ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "tribe.title.sortedByODD" ,
DefaultMessage : message . FallbackMsg ( "tribe.title.sortedByODD" , "Sorted by ODD" ) ,
} )
2020-06-27 16:48:33 +00:00
case TopSuppCommand :
filter . RankSupGTE = 1
filter . Sort = "rankSup ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "tribe.title.sortedByODS" ,
DefaultMessage : message . FallbackMsg ( "tribe.title.sortedByODS" , "Sorted by ODS" ) ,
} )
2020-06-27 16:48:33 +00:00
case TopTotalCommand :
filter . RankTotalGTE = 1
filter . Sort = "rankTotal ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "tribe.title.sortedByOD" ,
DefaultMessage : message . FallbackMsg ( "tribe.title.sortedByOD" , "Sorted by OD" ) ,
} )
2020-06-27 16:48:33 +00:00
case TopPointsCommand :
filter . Sort = "rank ASC"
2020-07-19 11:22:59 +00:00
title = ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
MessageID : "tribe.title.sortedByPoints" ,
DefaultMessage : message . FallbackMsg ( "tribe.title.sortedByPoints" , "Sorted by points" ) ,
} )
2020-06-27 16:48:33 +00:00
default :
return
}
playersList , err := s . cfg . API . Players . Browse ( world , filter , & sdk . PlayerInclude {
Tribe : true ,
} )
if err != nil {
s . SendMessage ( m . ChannelID ,
2020-07-19 11:22:59 +00:00
ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "api.defaultError" ,
DefaultMessage : message . FallbackMsg ( "api.defaultError" ,
"{{.Mention}} There was an error fetching data from the API, 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
}
if playersList == nil || playersList . Total == 0 {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "tribe.tribesNotFound" ,
DefaultMessage : message . FallbackMsg ( "tribe.tribesNotFound" ,
"{{.Mention}} There was an error fetching data from the API, 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
}
totalPages := int ( math . Ceil ( float64 ( playersList . Total ) / float64 ( limit ) ) )
if page > totalPages {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "tribe.exceededMaximumNumberOfPages" ,
DefaultMessage : message . FallbackMsg ( "tribe.exceededMaximumNumberOfPages" ,
"{{.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
}
langTag := utils . LanguageTagFromWorldName ( world )
langVersion , err := s . cfg . API . LangVersions . Read ( langTag )
if err != nil || langVersion == nil {
2020-07-19 11:22:59 +00:00
s . SendMessage ( m . ChannelID , ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "internalServerError" ,
DefaultMessage : message . FallbackMsg ( "internalServerError" ,
"{{.Mention}} Internal server error 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
}
msg := & EmbedMessage { }
for i , player := range playersList . Items {
if player == nil {
continue
}
rank := 0
score := 0
switch command {
case TopAttCommand :
rank = player . RankAtt
score = player . ScoreAtt
case TopDefCommand :
rank = player . RankDef
score = player . ScoreDef
case TopSuppCommand :
rank = player . RankSup
score = player . ScoreSup
case TopTotalCommand :
rank = player . RankTotal
score = player . ScoreTotal
case TopPointsCommand :
rank = player . Rank
score = player . Points
}
tribeTag := "-"
tribeURL := "-"
if player . Tribe != nil {
tribeTag = player . Tribe . Tag
tribeURL = utils . FormatTribeURL ( world , langVersion . Host , player . Tribe . ID )
}
2020-07-19 11:22:59 +00:00
msg . Append ( ctx . localizer . MustLocalize ( & i18n . LocalizeConfig {
2020-07-19 13:03:53 +00:00
MessageID : "tribe.messageLine" ,
DefaultMessage : message . FallbackMsg ( "tribe.messageLine" ,
"**{{.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 ,
"PlayerURL" : utils . FormatPlayerURL ( world , langVersion . Host , player . ID ) ,
"TribeTag" : tribeTag ,
"TribeURL" : tribeURL ,
"Rank" : rank ,
"Score" : score ,
} ,
} ) )
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 {
MessageID : "pagination.labelDisplayedPage" ,
DefaultMessage : message . FallbackMsg ( "pagination.labelDisplayedPage" , "{{.Page}} from {{.MaxPage}}" ) ,
TemplateData : map [ string ] interface { } {
"Page" : page ,
"MaxPage" : totalPages ,
} ,
} ) ) .
2020-06-27 16:48:33 +00:00
MessageEmbed )
}