Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: initial roleplay module #22

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .example.env
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
APP_DISCORD_TOKEN=
APP_ENV=development
APP_API_URL=http://localhost:3000

# POSTGRES
DB_HOST=localhost
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.env
*.DS_STORE
*.DS_STORE
microservices/*
11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM golang:alpine
RUN mkdir /app
ADD . /app/
WORKDIR /app
COPY go.mod .
COPY go.sum .
RUN go mod download
RUN go build -o main .
RUN adduser -S -D -H -h /app appuser
USER appuser
CMD ["./main"]
1 change: 1 addition & 0 deletions config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type AppConfig struct {
Env string
RootPath string
DiscordToken string `split_words:"true" json:"APP_DISCORD_TOKEN"`
ApiUrl string `split_words:"true" json:"APP_API_URL"`
}

var App *AppConfig
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,18 @@ services:
networks:
- dev_network

# main:
# depends_on:
# - postgres
# build: .
# container_name: main
# restart: always
# env_file:
# - .env
# stdin_open: true
# tty: true
# networks:
# - dev_network

networks:
dev_network:
9 changes: 3 additions & 6 deletions pkg/commands/application_commands/modules/utility/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (p PingCommand) Run(ctx context.Context, s disgord.Session, interaction *di
// Record the current time.
t1 := time.Now()

loadingEmbed, _ := utility_embeds.GetPingEmbed(0, 0)
loadingEmbed, _ := utility_embeds.GetPingEmbed(0)

// Send a message to measure the time taken to edit it.
if err := interaction.Reply(ctx, s, &disgord.CreateInteractionResponse{
Expand All @@ -43,13 +43,10 @@ func (p PingCommand) Run(ctx context.Context, s disgord.Session, interaction *di
// Record the new time.
// Record the time taken to send the message and edit it.
t2 := time.Now()
latency := t2.Sub(t1).Milliseconds()

// Calculate the websocket ping.
ping, _ := s.AvgHeartbeatLatency()
latency := t2.Sub(t1)

// Create the embed.
_, updatedEmbed := utility_embeds.GetPingEmbed(latency, ping)
_, updatedEmbed := utility_embeds.GetPingEmbed(latency)

// Edit the message with the new embed.
if err := interaction.Edit(ctx, s, &disgord.UpdateMessage{
Expand Down
48 changes: 48 additions & 0 deletions pkg/commands/message_commands/modules/action/baka.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package action

import (
"context"

"github.com/BearTS/Tamako/pkg/common/embed_maps/action_embeds"
"github.com/andersfylling/disgord"
)

// BakaCommand struct
type BakaCommand struct{}

func (a BakaCommand) Name() string {
return "baka"
}

func (a BakaCommand) Aliases() []string {
return []string{}
}

func (a BakaCommand) Help() string {
return "Sends a baka gif"
}

func (a BakaCommand) Description() string {
return "Sends a baka gif"
}

func (a BakaCommand) Category() string {
return "action"
}

func (a BakaCommand) Run(ctx context.Context, s disgord.Session, msg *disgord.Message) error {
var embed *disgord.Embed

if len(msg.Mentions) > 0 {
embed = action_embeds.GetBakaEmbed(msg.Mentions[0])
} else {
embed = action_embeds.GetBakaEmbed(msg.Author)
}

// Send the reply
if _, err := msg.Reply(context.Background(), s, embed); err != nil {
return err
}

return nil
}
4 changes: 4 additions & 0 deletions pkg/commands/message_commands/modules/enabledModules.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package modules

import (
"github.com/BearTS/Tamako/pkg/commands/message_commands/modules/action"
"github.com/BearTS/Tamako/pkg/commands/message_commands/modules/utility"
"github.com/BearTS/Tamako/pkg/interfaces"
)
Expand All @@ -12,4 +13,7 @@ var CommandsMap = map[string]interfaces.MessageCommand{
utility.HelpCommand{}.Name(): utility.HelpCommand{},
utility.UserInfoCommand{}.Name(): utility.UserInfoCommand{},
utility.ServerinfoCommand{}.Name(): utility.ServerinfoCommand{},

// action Commands
action.BakaCommand{}.Name(): action.BakaCommand{},
}
36 changes: 12 additions & 24 deletions pkg/commands/message_commands/modules/utility/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utility

import (
"context"
"time"

"github.com/BearTS/Tamako/pkg/common/embed_maps/utility_embeds"

Expand Down Expand Up @@ -33,51 +32,40 @@ func (p PingCommand) Category() string {
}

func (p PingCommand) Run(ctx context.Context, s disgord.Session, msg *disgord.Message) error {
// Record the current time.
t1 := time.Now()

loadingEmbed, _ := utility_embeds.GetPingEmbed(0, 0)
// Create the loading embed.
loadingEmbed, _ := utility_embeds.GetPingEmbed(0)

// Send a message to measure the time taken to edit it.
editMsg, err := msg.Reply(context.Background(), s, loadingEmbed)
if err != nil {
return err
}

// Record the time taken to send the message and edit it.
t2 := time.Now()
latency := t2.Sub(t1).Milliseconds()

// Calculate the websocket ping.
ping, _ := s.AvgHeartbeatLatency()
// Check difference between the two times.
ping := editMsg.Timestamp.Time.Sub(msg.Timestamp.Time)

// Create the embed.
_, embed := utility_embeds.GetPingEmbed(latency, ping)
_, embed := utility_embeds.GetPingEmbed(ping)

// delete the original message.
if err = s.Channel(editMsg.ChannelID).Message(editMsg.ID).Delete(); err != nil {
return err
}

// Edit the message with the embed.
editMsg, err = editMsg.Reply(context.Background(), s, embed)
_, err = editMsg.Reply(context.Background(), s, embed)

if err != nil {
return err
}

// Delete the message after 5 seconds.
go deleteMessageAfterDelay(s, editMsg, 5*time.Second)

// delete the user message after 5 seconds.
go deleteMessageAfterDelay(s, msg, 5*time.Second)

return nil
}

func deleteMessageAfterDelay(s disgord.Session, msg *disgord.Message, delay time.Duration) {
<-time.After(delay)
if err := s.Channel(msg.ChannelID).Message(msg.ID).Delete(); err != nil {
return
}
}
// func deleteMessageAfterDelay(s disgord.Session, msg *disgord.Message, delay time.Duration) {
// <-time.After(delay)
// if err := s.Channel(msg.ChannelID).Message(msg.ID).Delete(); err != nil {
// return
// }
// }
2 changes: 0 additions & 2 deletions pkg/commands/message_commands/modules/utility/userinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utility

import (
"context"
"fmt"

"github.com/BearTS/Tamako/pkg/common/embed_maps/utility_embeds"
"github.com/BearTS/Tamako/services/logger"
Expand Down Expand Up @@ -34,7 +33,6 @@ func (u UserInfoCommand) Category() string {

func (u UserInfoCommand) Run(ctx context.Context, s disgord.Session, msg *disgord.Message) error {
var embed *disgord.Embed
fmt.Println(msg.Mentions)
if len(msg.Mentions) > 0 {
embed = utility_embeds.GetUserinfoEmbed(msg.Mentions[0])
} else {
Expand Down
32 changes: 32 additions & 0 deletions pkg/common/embed_maps/action_embeds/baka.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package action_embeds

import (
"github.com/BearTS/Tamako/pkg/extensions/main_api/roleplay"
"github.com/BearTS/Tamako/services/logger"
"github.com/andersfylling/disgord"
)

func GetBakaEmbed(user *disgord.User) *disgord.Embed {

roleplay, err := roleplay.GetRoleplayData("baka")
if err != nil {
logger.Error("Failed to get roleplay data: %v", err)
}

url := roleplay.URL

finalEmbed := &disgord.Embed{
Title: "Baka~",
Color: 0x00ff00,
Image: &disgord.EmbedImage{
URL: url,
},
}

if user != nil {
description := user.Mention() + " Baka~"
finalEmbed.Description = description
}

return finalEmbed
}
45 changes: 29 additions & 16 deletions pkg/common/embed_maps/utility_embeds/utility_embeds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,27 @@ import (
"context"
"encoding/json"
"fmt"
"math/rand"
"time"

"github.com/BearTS/Tamako/pkg/structs"
"github.com/BearTS/Tamako/services/logger"
"github.com/BearTS/Tamako/services/registry"
"github.com/andersfylling/disgord"
)

// GetPingEmbed returns an embed with the given latency and ping.
func GetPingEmbed(latency int64, ping time.Duration) (*disgord.Embed, *disgord.Embed) {
func GetPingEmbed(ping time.Duration) (*disgord.Embed, *disgord.Embed) {

pingResponse := []string{
"I-It\"s not like I wanted to say pong or anything...",
"Pong...",
"Woo! A secret command!",
"Ping! ...I mean **pong!**",
"Does anyone even use this?",
"At your service!",
"Testing, testing, 1, 2, 3!",
}

loadingEmbed := &disgord.Embed{
Title: "Pinging...",
Expand All @@ -21,20 +33,21 @@ func GetPingEmbed(latency int64, ping time.Duration) (*disgord.Embed, *disgord.E
}

finalEmbed := &disgord.Embed{
Title: "Pong!",
Color: 0x00ff00,
Fields: []*disgord.EmbedField{
{
Name: "API Latency",
Value: fmt.Sprintf("%d ms", latency),
Inline: true,
},
{
Name: "Websocket Ping",
Value: fmt.Sprintf("%d ms", ping),
Inline: true,
},
},
Color: 0x00ff00,
Description: fmt.Sprintf("Latency: %d ms", ping.Milliseconds()),
}

rand.Seed(time.Now().UnixNano())
randomNumber := rand.Intn(len(pingResponse))

// finalEmbed.Fields = append(finalEmbed.Fields, &disgord.EmbedField{
// Name: "\u200b",
// Value: pingResponse[randomNumber],
// Inline: true,
// })

finalEmbed.Footer = &disgord.EmbedFooter{
Text: pingResponse[randomNumber],
}

return loadingEmbed, finalEmbed
Expand Down Expand Up @@ -86,7 +99,7 @@ func GetHelpEmbed(ctx context.Context) *disgord.Embed {
// Get From Registry
commandsFromRegistry, _ := registry.GetInstance().GetValue("commands")
if err := json.Unmarshal(commandsFromRegistry, &commandMap); err != nil {
fmt.Println(err)
logger.Error("Failed to unmarshal commands from registry: %v", err)
}

for _, command := range commandMap {
Expand Down
39 changes: 16 additions & 23 deletions pkg/events/guild_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,35 @@ package events

import (
"context"
"fmt"

"github.com/BearTS/Tamako/pkg/database/postgres"
"github.com/BearTS/Tamako/pkg/database/postgres/gorm/guilds"
"github.com/BearTS/Tamako/pkg/database/postgres/tables"
"github.com/BearTS/Tamako/services/logger"
"github.com/andersfylling/disgord"
)

func guildCreate(ctx context.Context, client *disgord.Client) {
gormDB, _ := postgres.GetConnection()
// gormDB, _ := postgres.GetConnection()

guildsGorm := guilds.Gorm(gormDB)

fmt.Println(disgord.AllEvents())
// guildsGorm := guilds.Gorm(gormDB)

// ! This does not work for some reason
// Needs further debugging
client.Gateway().GuildCreate(func(session disgord.Session, evt *disgord.GuildCreate) {
logger.Info(fmt.Sprintf("Joined guild %s", evt.Guild.Name))
_, err := guildsGorm.GetGuildByGuildID(evt.Guild.ID.String())
if err != nil {
// Guild does not exist in database
// Create guild record
// client.Gateway().GuildCreate(func(session disgord.Session, evt *disgord.GuildCreate) {
// logger.Info(fmt.Sprintf("Joined guild %s", evt.Guild.Name))
// _, err := guildsGorm.GetGuildByGuildID(evt.Guild.ID.String())
// if err != nil {
// // Guild does not exist in database
// // Create guild record

var guildRecord tables.Guilds
// var guildRecord tables.Guilds

guildRecord.GuildID = evt.Guild.ID.String()
// guildRecord.GuildID = evt.Guild.ID.String()

_, err := guildsGorm.CreateGuildRecord(guildRecord)
if err != nil {
logger.Error(err, "Error creating guild record")
}
}
// _, err := guildsGorm.CreateGuildRecord(guildRecord)
// if err != nil {
// logger.Error(err, "Error creating guild record")
// }
// }

})
// })

// This works, but I don't need it right now
// client.Gateway().MessageCreate(func(session disgord.Session, evt *disgord.MessageCreate) {
Expand Down
Loading