Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6 from keshon/dev
Browse files Browse the repository at this point in the history
feat: config hostname, randomness to about
  • Loading branch information
keshon authored Dec 1, 2023
2 parents c15c5a0 + 81df1dd commit 033fdc0
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ REST_ENABLED=true
# Set REST API server to release mode (empty value will switch to debug mode)
REST_GIN_RELEASE=true

# Hostname for REST API server, may optionally contain port e.g. "localhost:9000"
REST_HOSTNAME="localhost:9000"

# Audio frame duration (can be 20, 40, or 60 ms)
# Everything above 20 will ruin sound quality
DCA_FRAME_DURATION=20
Expand Down
23 changes: 18 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"app/internal/manager"
"app/internal/melodix"
"app/internal/version"
"net"
"os"
"os/signal"
"syscall"
Expand Down Expand Up @@ -72,7 +73,7 @@ func main() {
defer dg.Close()

if config.RestEnabled {
startRestServer(config.RestGinRelease)
startRestServer(config.RestGinRelease, config.RestHostname)
}

slog.Infof("%v is now running. Press Ctrl+C to exit", version.AppName)
Expand Down Expand Up @@ -106,7 +107,7 @@ func startBotInstances(session *discordgo.Session, guildID string) {
botInstances[guildID].Melodix.Start(guildID)
}

func startRestServer(isReleaseMode bool) {
func startRestServer(isReleaseMode bool, hostname string) {
if isReleaseMode {
gin.SetMode("release")
}
Expand All @@ -117,9 +118,21 @@ func startRestServer(isReleaseMode bool) {
restAPI.Start(router)

go func() {
port := "8080" // TODO: move out port number to .env file
slog.Infof("REST API server started on port %v\n", port)
if err := router.Run(":" + port); err != nil {
// parse hostname var - if it has port - use it or fallback to 8080
host, port, err := net.SplitHostPort(hostname)
if err != nil {
// If there's an error, assume the entire input is the host (without port)
host = hostname
port = "8080"
}

// If hostname is empty, set it to the default port (8080)
if host == "" {
host = "localhost"
}

slog.Infof("REST API server started on %s:%s\n", host, port)
if err := router.Run(net.JoinHostPort(host, port)); err != nil {
slog.Fatalf("Error starting REST API server: %v", err)
}
}()
Expand Down
3 changes: 3 additions & 0 deletions docker/.env
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ REST_ENABLED=true
# Set REST API server to release mode (empty value will switch to debug mode)
REST_GIN_RELEASE=true

# Hostname for REST API server, may optionally contain port e.g. "localhost:9000"
REST_HOSTNAME=${HOST}

# Audio frame duration (can be 20, 40, or 60 ms)
# Everything above 20 will ruin sound quality
DCA_FRAME_DURATION=20
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Config struct {
DiscordBotToken string
RestEnabled bool
RestGinRelease bool
RestHostname string
DcaFrameDuration int
DcaBitrate int
DcaPacketLoss int
Expand Down Expand Up @@ -48,6 +49,7 @@ func NewConfig() (*Config, error) {
DiscordBotToken: os.Getenv("DISCORD_BOT_TOKEN"),
RestEnabled: getenvAsBool("REST_ENABLED"),
RestGinRelease: getenvAsBool("REST_GIN_RELEASE"),
RestHostname: os.Getenv("REST_HOSTNAME"),
DcaFrameDuration: getenvAsInt("DCA_FRAME_DURATION"),
DcaBitrate: getenvAsInt("DCA_BITRATE"),
DcaPacketLoss: getenvAsInt("DCA_PACKET_LOSS"),
Expand Down Expand Up @@ -76,6 +78,7 @@ func (c *Config) String() string {
"DiscordBotToken": c.DiscordBotToken,
"RestEnabled": c.RestEnabled,
"RestGinRelease": c.RestGinRelease,
"RestHostname": c.RestHostname,
"DcaFrameDuration": c.DcaFrameDuration,
"DcaBitrate": c.DcaBitrate,
"DcaPacketLoss": c.DcaPacketLoss,
Expand Down Expand Up @@ -110,6 +113,7 @@ func validateMandatoryConfig() error {
// Extra overlay to ensure we have all necessary values even if they have default values (e.g. ffmpeg has own)
// ignore:
// - REST_GIN_RELEASE
// - REST_HOSTNAME
// - DCA_FFMPEG_BINARY_PATH

mandatoryKeys := []string{
Expand Down
14 changes: 11 additions & 3 deletions internal/melodix/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,17 +493,25 @@ func (d *Discord) handleHistoryCommand(s *discordgo.Session, m *discordgo.Messag
func (d *Discord) handleAboutCommand(s *discordgo.Session, m *discordgo.MessageCreate) {
d.changeAvatar(s)

content := version.AppName + " is a simple music bot that allows you to play music in voice channels on a Discord server."
config, err := config.NewConfig()
if err != nil {
slog.Fatalf("Error loading config: %v", err)
}

avatarUrl := inferProtocolByPort(config.RestHostname, 443) + config.RestHostname + "/avatar/random?" + fmt.Sprint(time.Now().UnixNano())

title := getRandomAboutTitlePhrase()
content := getRandomAboutDescriptionPhrase()

embedStr := fmt.Sprintf("📻 **About %v**\n\n%v", version.AppName, content)
embedStr := fmt.Sprintf("**%v**\n\n%v", title, content)

embedMsg := embed.NewEmbed().
SetDescription(embedStr).
AddField("```"+version.BuildDate+"```", "Build date").
AddField("```"+version.GoVersion+"```", "Go version").
AddField("```Created by Innokentiy Sokolov```", "[Linkedin](https://www.linkedin.com/in/keshon), [GitHub](https://github.com/keshon), [Homepage](https://keshon.ru)").
InlineAllFields().
SetImage("https://melodix-bot.keshon.ru/avatar/random"). // TODO: move out to config .env file
SetImage(avatarUrl).
SetColor(0x9f00d4).SetFooter(version.AppFullName + " <" + d.Player.GetCurrentStatus().String() + ">").MessageEmbed

s.ChannelMessageSendEmbed(m.Message.ChannelID, embedMsg)
Expand Down
70 changes: 70 additions & 0 deletions internal/melodix/utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math"
"math/rand"
"net"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -257,3 +258,72 @@ func getRandomWaitPhrase() string {

return phrases[index]
}

func getRandomAboutTitlePhrase() string {
phrases := []string{
"Hello there!",
"Who do we have here?",
"Brace yourselves for Melodix!",
"Get ready to laugh and groove!",
"Peek behind the musical curtain!",
"Unleashing Melodix magic!",
"Prepare for some bot banter!",
"It's showtime with Melodix!",
"Allow me to introduce myself",
"Heeeey amigos!",
"Unleashing Melodix magic!",
"Did someone order beats?",
"Well, look who's curious!",
}

index := rand.Intn(len(phrases))

return phrases[index]
}

func getRandomAboutDescriptionPhrase() string {
phrases := []string{
"🎶 The Discord DJ That Won't Take Requests From Your In-Laws! 🔊 Crank up the tunes and drown out the chaos. No commercials, no cover charges—just pure, unfiltered beats. Because when life hands you a mic, you drop it with Melodix! 🎤🎉 #MelodixMadness #NoRequestsAllowed",
"🎵 Groovy Bot: Where Beats Meet Banter! 🤖 Tune in for the ultimate audio fiesta. Tracks that hit harder than Monday mornings and a vibe that won't quit. Request, rewind, and revel in the groove. Life's a party; let's make it legendary! 🚀🕺 #GroovyBot #UnleashTheBeats",
"Melodix: Unleash the Epic Beats! 🚀🎵 Your Discord, Your Soundtrack—Elevate your server experience with the ultimate music companion. No boundaries, just epicness! Turn up the volume and let Melodix redefine your sonic adventure. 🎧🔥 #EpicBeats #MelodixUnleashed",
"🤖 Welcome to the Groovy Bot Experience! 🎶 Unleash the musical mayhem with a sprinkle of humor. I'm your DJ, serving beats hotter than a summer grill. 🔥 Request a jam, peek into your play history, and let's dance like nobody's watching. It's music with a side of laughter – because why not? Let the groove take the wheel! 🕺🎉 #BotLife #DanceTillYouDrop",
"🎶 Melodix: Your Personal Discord DJ! 🔊 I spin tunes better than your grandma spins knitting yarn. No song requests? No problem! I play what I want, when I want. Get ready for a musical rollercoaster, minus the safety harness! 🎢🎤 #MelodixMagic #GrandmaApproved",
"🎵 Melodix: The Bot with the Moves! 🕺 Break out your best dance moves because I'm dropping beats that even the neighbors can't resist. Turn up the volume, lock the door, and dance like nobody's watching—except me, of course! 💃🎉 #DanceFloorOnDiscord #BeatDropper",
"Melodix: Where Music Meets Mischief! 🤖🎶 Your server's audio adventure begins here. I play music that hits harder than your morning alarm and cracks more jokes than your favorite stand-up comedian. Buckle up; it's gonna be a hilarious ride! 🚀😂 #MusicMischief #JokesterBot",
"🤖 Meet Melodix: The Discord DJ on a Comedy Tour! 🎤 Unleash the laughter and the beats with a bot that's funnier than your uncle's dad jokes. Request a track, sit back, and enjoy the show. Warning: I may cause uncontrollable fits of joy! 😆🎵 #ComedyTourBot #LaughOutLoud",
"🎧 Melodix: Beats that Hit Harder Than Life's Problems! 💥 When reality knocks, I turn up the volume. Melodix delivers beats that punch harder than Monday mornings and leave you wondering why life isn't always this epic. Buckle up; it's time to conquer the airwaves! 🚀🎶 #EpicBeats #LifePuncher",
"🔊 Groovy Bot: Making Discord Groovy Again! 🕺 Shake off the stress, kick back, and let Groovy Bot do the heavy lifting. My beats are so groovy; even your grandma would break into the moonwalk. Get ready to rediscover your groove on Discord! 🌙💫 #GroovyAgain #DiscordDanceRevolution",
"🚀 Melodix: Your Gateway to Musical Awesomeness! 🌟 I'm not just a bot; I'm your VIP pass to a sonic wonderland. No queues, no limits—just pure, unadulterated musical awesomeness. Fasten your seatbelts; the journey to epic sounds begins now! 🎸🎉 #MusicalAwesomeness #VIPPass",
"🎶 Melodix: More Than Just a Bot—It's a Vibe! 🤖🕶️ Elevate your server with vibes so cool, even penguins envy me. I'm not your average bot; I'm a mood-altering, vibe-creating, beat-dropping phenomenon. Prepare for a vibe check, Melodix style! 🌊🎵 #VibeMaster #BotGoals",
"🔊 Step into Melodix's Audio Playground! 🎉 Your ticket to the ultimate sonic adventure is here. With beats that rival a theme park ride and humor sharper than a stand-up special, Melodix is your all-access pass to the audio amusement park. Let the fun begin! 🎢🎤 #AudioPlayground #RollercoasterBeats",
"🎵 Melodix: Where Discord Gets Its Groove On! 💃 I'm not just a bot; I'm the rhythm that keeps your server dancing. My beats are so infectious; even the toughest critics tap their feet. Get ready to groove; Melodix is in the house! 🕺🎶 #DiscordGrooveMaster #BeatCommander",
"🚀 Unleash Melodix: The Bot with a Sonic Punch! 💥 Dive into a world where beats hit harder than a superhero landing. Melodix isn't just a bot; I'm a powerhouse of sonic awesomeness. Get ready for an audio experience that packs a punch! 🎤👊 #SonicPowerhouse #BeatHero",
"🔊 Melodix: Your Server's Audio Magician! 🎩✨ Watch as I turn ordinary moments into extraordinary memories with a wave of my musical wand. Beats appear, laughter ensues, and your server becomes the stage for an epic audio performance. Prepare to be enchanted! 🎶🔮 #AudioMagician #DiscordWizard",
"🎧 Melodix: Beats That Speak Louder Than Words! 📢 When words fail, music speaks. I deliver beats so powerful; even a whisper could start a party. Say goodbye to silence; it's time to let the music do the talking. Turn it up; let's break the sound barrier! 🚀🎵 #BeatsNotWords #MusicSpeaksVolumes",
"🤖 Melodix: The Bot That Takes the Stage! 🎤 Roll out the red carpet; Melodix is here to steal the show. My beats command attention, and my humor steals the spotlight. It's not just music; it's a performance. Get ready for a standing ovation! 👏🎶 #StageStealer #BotOnTheMic",
"🎵 Groovy Bot: Turning Discord into a Dance Floor! 💃 I'm not just a bot; I'm the DJ that turns your server into a non-stop dance party. Groovy Bot's beats are so infectious; even the furniture wants to boogie. Get ready to dance like nobody's watching! 🎉🎶 #DancePartyBot #BoogieMaster",
"🚀 Melodix: Your Sonic Co-Pilot on the Discord Journey! 🎶 Buckle up; we're about to take off on a musical adventure. Melodix isn't just a bot; I'm your co-pilot navigating the airspace of epic beats. Fasten your seatbelts; the journey awaits! ✈️🔊 #SonicCoPilot #DiscordAdventure",
"🔊 Melodix: Bringing the Beats, Igniting the Vibes! 🔥 I'm not just a bot; I'm the ignition switch for a server-wide party. My beats are so fire; even the speakers need a cooldown. Prepare for a musical blaze that'll leave you in awe! 🎵🎉 #IgniteTheVibes #DiscordInferno",
"🎶 Melodix: Turning Mundane into Musical! 🌟 Say goodbye to the ordinary; Melodix is here to transform the mundane into a symphony of epic proportions. My beats are the soundtrack to your server's extraordinary journey. Let's make every moment musical! 🎤🚀 #MusicalTransformation #EpicSymphony",
"🤖 Melodix: The Bot That Doesn't Miss a Beat—Literally! 🥁 Precision beats, flawless execution, and humor that lands every time. Melodix is the maestro of your server's audio orchestra. No missed beats, no dull moments—just pure musical perfection! 🎶👌 #NoMissedBeats #AudioMaestro",
"🎵 Groovy Bot: Where Discord Finds Its Rhythm! 🕺 We're not just a bot; we're the rhythm that keeps your server in sync. Groovy Bot's beats are so contagious; even the skeptics catch the vibe. Get ready for a rhythmic revolution on Discord! 🎶🔄 #RhythmicRevolution #DiscordSyncMaster",
"🚀 Melodix: Elevate Your Discord, Elevate Your Beats! 🎧 We're not just a bot; we're the elevator to the next level of sonic greatness. Melodix's beats are the soundtrack to your server's ascension. Get ready to elevate your vibes to new heights! 🌌🔊 #ElevateYourBeats #DiscordAscent",
}

index := rand.Intn(len(phrases))

return phrases[index]
}

// inferProtocolByPort attempts to infer the protocol based on the availability of a specific port.
func inferProtocolByPort(hostname string, port int) string {
conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", hostname, port))
if err != nil {
// Assuming it's not available, use HTTP
return "http://"
}
defer conn.Close()

// The port is available, use HTTPS
return "https://"
}

0 comments on commit 033fdc0

Please sign in to comment.