Skip to content

Commit

Permalink
added info command
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed May 7, 2023
1 parent 9c1ab99 commit 1836b2e
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 26 deletions.
8 changes: 7 additions & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
12 changes: 11 additions & 1 deletion data/lang/de.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
12 changes: 11 additions & 1 deletion data/lang/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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"
30 changes: 15 additions & 15 deletions event/command/birthday/handlerSubcommandSet.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,23 +285,23 @@ 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
if b.Year > 0 {
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("<t:%d:R>%s", b.NextUnix(), age),
Inline: true,
})
util.AddEmbedField(e,
lang.Get(tp+"msg.next", lang.FallbackLang()),
fmt.Sprintf("<t:%d:R>%s", b.NextUnix(), age),
true,
)

if before.Visible != b.Visible {
var visibility string
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions event/command/birthday/userCommandShow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
}

Expand Down
91 changes: 91 additions & 0 deletions event/command/info/chatCommand.go
Original file line number Diff line number Diff line change
@@ -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("<t:%d:R>", 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
}
16 changes: 10 additions & 6 deletions event/command/util/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions event/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"cake4everybot/event/command"
"cake4everybot/event/command/birthday"
"cake4everybot/event/command/info"

"github.com/bwmarrin/discordgo"
)
Expand All @@ -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{})
Expand Down
34 changes: 34 additions & 0 deletions status/status.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 1836b2e

Please sign in to comment.