diff --git a/config.yaml b/config.yaml index 3500693..c3e4c30 100644 --- a/config.yaml +++ b/config.yaml @@ -1,3 +1,9 @@ +# Version of the bot. DO NOT CHANGE +version: 1.0.1 + +# Loading aditional files. This is primarily used to load tokens, +# keys, and other cridentials from files that are filtered by the +# .gitignore rules additionalConfigs: - config_env.yaml # For tokens and access keys - database/connection_env.yaml # Connection data for mySQL databse @@ -6,7 +12,7 @@ additionalConfigs: # The first one in this list will also be the fallback when trying to # read an unloaded language languages: - - en_us + - en - de discord: diff --git a/data/lang/de.yaml b/data/lang/de.yaml index 5bd5b86..d9cb446 100644 --- a/data/lang/de.yaml +++ b/data/lang/de.yaml @@ -31,7 +31,7 @@ discord.command: option.list.option.month: monat option.list.option.month.description: Der Monat, aus dem alle Geburtstage aufgelistet werden sollen - user_show: + user.show: base: Geburtstag zeigen weekday: @@ -96,6 +96,16 @@ discord.command: msg.announce.with_age: "%s (wird %s)" msg.next: Nächster Geburtstag + info: + base: info + base.description: Zeigt ein paar Infos über den Bot + display: Bot-Information + + title: Informationen und Status + start_time: Letzter Neustart + latency: Latenz + version: Version + youtube: embed_footer: YouTube Glocke msg.new_vid: "%s hat ein neues Video hochgeladen" diff --git a/data/lang/en.yaml b/data/lang/en.yaml index 8957fc6..73cb1de 100644 --- a/data/lang/en.yaml +++ b/data/lang/en.yaml @@ -31,7 +31,7 @@ discord.command: option.list.option.month: month option.list.option.month.description: The month to list all birthdays from - user_show: + user.show: base: Show birthday weekday: @@ -96,6 +96,16 @@ discord.command: msg.announce.with_age: "%s (turns %s)" msg.next: Next birthday + info: + base: info + base.description: Displays some infos about the bot + display: Bot information + + title: Informations and status + start_time: Latest restart + latency: Latency + version: Version + youtube: embed_footer: YouTube notification bell msg.new_vid: "%s just uploaded a new video" diff --git a/event/command/birthday/handlerSubcommandSet.go b/event/command/birthday/handlerSubcommandSet.go index f370810..2237a6f 100644 --- a/event/command/birthday/handlerSubcommandSet.go +++ b/event/command/birthday/handlerSubcommandSet.go @@ -285,11 +285,11 @@ func (cmd subcommandSet) handleUpdate(b birthdayEntry, e *discordgo.MessageEmbed e.Fields = []*discordgo.MessageEmbedField{f} if !f.Inline { - e.Fields = append(e.Fields, &discordgo.MessageEmbedField{ - Name: lang.Get(tp+"msg.set.date", lang.FallbackLang()), - Value: b.String(), - Inline: true, - }) + util.AddEmbedField(e, + lang.Get(tp+"msg.set.date", lang.FallbackLang()), + b.String(), + true, + ) } var age string @@ -297,11 +297,11 @@ func (cmd subcommandSet) handleUpdate(b birthdayEntry, e *discordgo.MessageEmbed age = fmt.Sprintf(" (%d)", b.Age()+1) } - e.Fields = append(e.Fields, &discordgo.MessageEmbedField{ - Name: lang.Get(tp+"msg.next", lang.FallbackLang()), - Value: fmt.Sprintf("%s", b.NextUnix(), age), - Inline: true, - }) + util.AddEmbedField(e, + lang.Get(tp+"msg.next", lang.FallbackLang()), + fmt.Sprintf("%s", b.NextUnix(), age), + true, + ) if before.Visible != b.Visible { var visibility string @@ -315,11 +315,11 @@ func (cmd subcommandSet) handleUpdate(b birthdayEntry, e *discordgo.MessageEmbed mentionCmd := util.MentionCommand(tp+"base", tp+"option.remove") visibility = fmt.Sprintf(visibility, mentionCmd) } - e.Fields = append(e.Fields, &discordgo.MessageEmbedField{ - Name: lang.Get(tp+"msg.set.update.visibility", lang.FallbackLang()), - Value: visibility, - Inline: false, - }) + util.AddEmbedField(e, + lang.Get(tp+"msg.set.update.visibility", lang.FallbackLang()), + visibility, + false, + ) } return nil diff --git a/event/command/birthday/userCommandShow.go b/event/command/birthday/userCommandShow.go index 0f87e58..124bcc3 100644 --- a/event/command/birthday/userCommandShow.go +++ b/event/command/birthday/userCommandShow.go @@ -36,8 +36,8 @@ type UserShow struct { func (cmd UserShow) AppCmd() *discordgo.ApplicationCommand { return &discordgo.ApplicationCommand{ Type: discordgo.UserApplicationCommand, - Name: lang.GetDefault(tp + "user_show.base"), - NameLocalizations: util.TranslateLocalization(tp + "user_show.base"), + Name: lang.GetDefault(tp + "user.show.base"), + NameLocalizations: util.TranslateLocalization(tp + "user.show.base"), } } diff --git a/event/command/info/chatCommand.go b/event/command/info/chatCommand.go new file mode 100644 index 0000000..6a86241 --- /dev/null +++ b/event/command/info/chatCommand.go @@ -0,0 +1,91 @@ +// Copyright 2022-2023 Kesuaheli +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package info + +import ( + "cake4everybot/data/lang" + "cake4everybot/event/command/util" + "cake4everybot/status" + "fmt" + + "github.com/bwmarrin/discordgo" + "github.com/spf13/viper" +) + +const ( + // Prefix for translation key, i.e.: + // key := tp+"base" // => info + tp = "discord.command.info." +) + +// The Chat (slash) command of the info package. Simply prints a +// little infomation about the bot. +type Chat struct { + util.InteractionUtil + + ID string +} + +// AppCmd (ApplicationCommand) returns the definition of the chat +// command +func (cmd Chat) AppCmd() *discordgo.ApplicationCommand { + return &discordgo.ApplicationCommand{ + Name: lang.GetDefault(tp + "base"), + NameLocalizations: util.TranslateLocalization(tp + "base"), + Description: lang.GetDefault(tp + "base.description"), + DescriptionLocalizations: util.TranslateLocalization(tp + "base.description"), + } +} + +// CmdHandler returns the functionality of a command +func (cmd Chat) CmdHandler() func(s *discordgo.Session, i *discordgo.InteractionCreate) { + + return func(s *discordgo.Session, i *discordgo.InteractionCreate) { + cmd.InteractionUtil = util.InteractionUtil{Session: s, Interaction: i} + + e := &discordgo.MessageEmbed{ + Title: lang.GetDefault(tp + "title"), + Color: 0x00FF00, + } + util.AddEmbedField(e, + lang.Get(tp+"start_time", lang.FallbackLang()), + fmt.Sprintf("", status.GetStartTime().Unix()), + true, + ) + util.AddEmbedField(e, + lang.Get(tp+"latency", lang.FallbackLang()), + fmt.Sprintf("%dms", s.LastHeartbeatAck.Sub(s.LastHeartbeatSent).Milliseconds()), + true, + ) + util.AddEmbedField(e, + lang.Get(tp+"version", lang.FallbackLang()), + fmt.Sprintf("v%s", viper.GetString("version")), + false, + ) + util.SetEmbedFooter(s, tp+"display", e) + + cmd.ReplyEmbed(e) + } +} + +// SetID sets the registered command ID for internal uses after uploading to discord +func (cmd *Chat) SetID(id string) { + cmd.ID = id +} + +// GetID gets the registered command ID +func (cmd Chat) GetID() string { + return cmd.ID +} diff --git a/event/command/util/discord.go b/event/command/util/discord.go index 2455886..b0087dc 100644 --- a/event/command/util/discord.go +++ b/event/command/util/discord.go @@ -75,15 +75,19 @@ func SetEmbedFooter(s *discordgo.Session, sectionName string, e *discordgo.Messa } } +// AddEmbedField is a short hand for appending one field to the embed +func AddEmbedField(e *discordgo.MessageEmbed, name, value string, inline bool) { + e.Fields = append(e.Fields, &discordgo.MessageEmbedField{Name: name, Value: value, Inline: inline}) +} + // AddReplyHiddenField appends the standard field for ephemral // embeds to the existing fields of the given embed. func AddReplyHiddenField(e *discordgo.MessageEmbed) { - f := &discordgo.MessageEmbedField{ - Name: lang.Get("discord.command.generic.msg.self_hidden", lang.FallbackLang()), - Value: lang.Get("discord.command.generic.msg.self_hidden.desc", lang.FallbackLang()), - Inline: false, - } - e.Fields = append(e.Fields, f) + AddEmbedField(e, + lang.Get("discord.command.generic.msg.self_hidden", lang.FallbackLang()), + lang.Get("discord.command.generic.msg.self_hidden.desc", lang.FallbackLang()), + false, + ) } // MentionCommand returns the mention string for a slashcommand diff --git a/event/commands.go b/event/commands.go index 30f189c..e07e1b6 100644 --- a/event/commands.go +++ b/event/commands.go @@ -20,6 +20,7 @@ import ( "cake4everybot/event/command" "cake4everybot/event/command/birthday" + "cake4everybot/event/command/info" "github.com/bwmarrin/discordgo" ) @@ -35,6 +36,7 @@ func registerCommands(s *discordgo.Session, guildID string) error { // chat (slash) commands commandsList = append(commandsList, &birthday.Chat{}) + commandsList = append(commandsList, &info.Chat{}) // messsage commands // user commands commandsList = append(commandsList, &birthday.UserShow{}) diff --git a/status/status.go b/status/status.go new file mode 100644 index 0000000..43b33f5 --- /dev/null +++ b/status/status.go @@ -0,0 +1,34 @@ +// Copyright 2022-2023 Kesuaheli +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package status + +import "time" + +// The point in time where the bot starts +var startTime time.Time + +func init() { + startTime = time.Now() +} + +// GetStartTime returns the time were the bot stared +func GetStartTime() time.Time { + return startTime +} + +// GetUptime returns the duration since the bot stared +func GetUptime() time.Duration { + return time.Since(startTime) +}