From 1266eead0126d68b463d017a9718e38d2bf15050 Mon Sep 17 00:00:00 2001 From: Matthieu Lalonde Date: Thu, 28 Jan 2021 18:36:48 +0000 Subject: [PATCH] Adding support for multiple message replies per key, closes #25 --- pkg/bot/msgs.go | 110 ++++++++++++++++++++++--------------------- pkg/utils/excuses.go | 13 +---- pkg/utils/utils.go | 16 ++++++- 3 files changed, 73 insertions(+), 66 deletions(-) diff --git a/pkg/bot/msgs.go b/pkg/bot/msgs.go index db0a864..5c69c9e 100644 --- a/pkg/bot/msgs.go +++ b/pkg/bot/msgs.go @@ -25,66 +25,68 @@ func (mm msgMode) String() string { const msgModeLast = msgModeFlirty +type msgMap map[string][]string +var ( msgStrings = map[msgMode]msgMap{ msgModeNormal: { - "online_welcome": "Hello, AltVRBot online and here to serve!", - "all_done": "All done!", - "unauthorized_user": "Sorry, you are not authorized to do this!", - "invalid_command": "Invalid command format!", - "unknown_command": "Uknown command, try `help` to display the available command.", - "unknown_error": "I couldn't not process your request due to an unknown error!", - "auser_unknown_friend": "I don't know about this user, are you sure they are one of my friends and the username is correct?", - "auser_known_user": "I already know about this user!", - "aemoji_unknown_user": "I don't know about this user, try to associate it first!", - "msg_unknown_sender": "I don't know you! You must be associated first!", - "msg_unknown_receipient": "I don't know about <@!%s>! They must be associated first!", - "msg_invalid_receipient": "I can't message this user because it probably is myself!", - "msg_all_done_truncated": "All done, however your message was too long and truncated!", - "new_friendship_requested": "I just heard that %s would like to become friends, should I accept?", - "accept_unknown_reference": "Unknown reference, are you sure you are replying to the right message? Perhaps the request was withdrawn!", - "no_online_users": "Seems like no one is online, how lonely!", - "no_online_requested_users": "These people aren't online right now, sorry!", + "online_welcome": {"Hello, AltVRBot online and here to serve!"}, + "all_done": {"All done!"}, + "unauthorized_user": {"Sorry, you are not authorized to do this!"}, + "invalid_command": {"Invalid command format!"}, + "unknown_command": {"Uknown command, try `help` to display the available command."}, + "unknown_error": {"I couldn't not process your request due to an unknown error!"}, + "auser_unknown_friend": {"I don't know about this user, are you sure they are one of my friends and the username is correct?"}, + "auser_known_user": {"I already know about this user!"}, + "aemoji_unknown_user": {"I don't know about this user, try to associate it first!"}, + "msg_unknown_sender": {"I don't know you! You must be associated first!"}, + "msg_unknown_receipient": {"I don't know about <@!%s>! They must be associated first!"}, + "msg_invalid_receipient": {"I can't message this user because it probably is myself!"}, + "msg_all_done_truncated": {"All done, however your message was too long and truncated!"}, + "new_friendship_requested": {"I just heard that %s would like to become friends, should I accept?"}, + "accept_unknown_reference": {"Unknown reference, are you sure you are replying to the right message? Perhaps the request was withdrawn!"}, + "no_online_users": {"Seems like no one is online, how lonely!"}, + "no_online_requested_users": {"These people aren't online right now, sorry!"}, }, msgModeRude: { - "online_welcome": "Ah fuck, back to work already? Well message me, maybe I'll help you anyway!", - "all_done": "Alright alright, stop bugging me already, I got it done for you, you fuck!", - "unauthorized_user": "Fartface, who the fuck do you think you are?!? You're not allowed to do that you lowlife!", - "invalid_command": "You drunk or something?", - "unknown_command": "I could `help` you with that, but I won't because: _BOFH_", - "unknown_error": "An unknown error occured, probably because: _BOFH_", - "auser_unknown_friend": "You dimwit, you should know I don't know who this idiot is. Do I really want them to be my friend? I don't know, but maybe they do!", - "auser_known_user": "Fuckwad, I already know about this asshole!", - "aemoji_unknown_user": "Who the fuck is that? Maybe try to tell me first you airhead!", - "msg_unknown_sender": "Never heard of you chucklefuck! You even registered?", - "msg_unknown_receipient": "I don't know about this chickenfucker named <@!%s>! They must be associated first!", - "msg_invalid_receipient": "Seems obvious, but I'm gonna explain it for you half brained: I can't message myself!", - "msg_all_done_truncated": "Ok ok I did it you shitstick, but you're a verbose fuck so I had to cut your message off a bit!", - "new_friendship_requested": "Have you heard about this dipshit named %s, they think they're cool enough, ha! Should we let that numskull in?", - "accept_unknown_reference": "Numnuts, I don't know what you're talking about! That user might have been too cool for you, or you're just fucking confused!", - "no_online_users": "Everyone else has better things to do than talk you, loser!", - "no_online_requested_users": "Looks like these fucks have better things to do than talk to you, loser!", + "online_welcome": {"Ah fuck, back to work already? Well message me, maybe I'll help you anyway!"}, + "all_done": {"Alright alright, stop bugging me already, I got it done for you, you fuck!"}, + "unauthorized_user": {"Fartface, who the fuck do you think you are?!? You're not allowed to do that you lowlife!"}, + "invalid_command": {"You drunk or something?"}, + "unknown_command": {"I could `help` you with that, but I won't because of: _BOFH_"}, + "unknown_error": {"An unknown error occured, probably because: _BOFH_"}, + "auser_unknown_friend": {"You dimwit, you should know I don't know who this idiot is. Do I really want them to be my friend? I don't know, but maybe they do!"}, + "auser_known_user": {"Fuckwad, I already know about this asshole!"}, + "aemoji_unknown_user": {"Who the fuck is that? Maybe try to tell me first you airhead!"}, + "msg_unknown_sender": {"Never heard of you chucklefuck! You even registered?"}, + "msg_unknown_receipient": {"I don't know about this chickenfucker named <@!%s>! They must be associated first!"}, + "msg_invalid_receipient": {"Seems obvious, but I'm gonna explain it for you half brained: I can't message myself!"}, + "msg_all_done_truncated": {"Ok ok I did it you shitstick, but you're a verbose fuck so I had to cut your message off a bit!"}, + "new_friendship_requested": {"Have you heard about this dipshit named %s, they think they're cool enough, ha! Should we let that numskull in?"}, + "accept_unknown_reference": {"Numnuts, I don't know what you're talking about! That user might have been too cool for you, or you're just fucking confused!"}, + "no_online_users": {"Everyone else has better things to do than talk you, loser!"}, + "no_online_requested_users": {"Looks like these fucks have better things to do than talk to you, loser!"}, }, msgModeFlirty: { - "online_welcome": "Hi there beautiful people, it'll be my delight to serve you!", - "all_done": "I did it for you, sweetie!", - "unauthorized_user": "I wished I could let you do that, but sadly even your good looks aren't enough to allow me!", - "invalid_command": "Love, your beauty is blinding me and I don't understand what you mean!", - "unknown_command": "Sugar plum, I love you, but I don't know what it is you want `help` with!", - "unknown_error": "We'd all love to see that happen, but the stars weren't aligned today, something unexpected happened", - "auser_unknown_friend": "I've never heard of this hotie before, would you make sure they have befriended me and the username is correct?", - "auser_known_user": "Bae, I already know this person, we were all in that cuddle puddle together the other night!", - "aemoji_unknown_user": "Sweetiepie, I don't know them, maybe you could introduce us over a glass of wine and some candles?", - "msg_unknown_sender": "Honey, I've never met you before, invite me on a date before we get cozy!", - "msg_unknown_receipient": "I've never heard of <@!%s>, but they sure do sound sexy! Maybe you could tell me more about them?", - "msg_invalid_receipient": "I'd love to write to this lovely person, but they aren't in my rolodex yet!", - "msg_all_done_truncated": "I did it for you sweetie, however your passion was too great and I had to truncate your message!", - "new_friendship_requested": "Looks like %s would like to get cozy, should we let them in the hot tub?", - "accept_unknown_reference": "As sexy as you are, I still can't find a reference to this request. Perhaps it was withdrawn!", - "no_online_users": "I'm sory sweetheart, no one's online, but I'll always be there for you!", - "no_online_requested_users": "I'm sory sweetheart, these beautiful people arent online, but I'll always be there for you!", + "online_welcome": {"Hi there beautiful people, it'll be my delight to serve you!"}, + "all_done": {"I did it for you, sweetie!"}, + "unauthorized_user": {"I wished I could let you do that, but sadly even your good looks aren't enough to allow me!"}, + "invalid_command": {"Love, your beauty is blinding me and I don't understand what you mean!"}, + "unknown_command": {"Sugar plum, I love you, but I don't know what it is you want `help` with!"}, + "unknown_error": {"We'd all love to see that happen, but the stars weren't aligned today, something unexpected happened"}, + "auser_unknown_friend": {"I've never heard of this hotie before, would you make sure they have befriended me and the username is correct?"}, + "auser_known_user": {"Bae, I already know this person, we were all in that cuddle puddle together the other night!"}, + "aemoji_unknown_user": {"Sweetiepie, I don't know them, maybe you could introduce us over a glass of wine and some candles?"}, + "msg_unknown_sender": {"Honey, I've never met you before, invite me on a date before we get cozy!"}, + "msg_unknown_receipient": {"I've never heard of <@!%s>, but they sure do sound sexy! Maybe you could tell me more about them?"}, + "msg_invalid_receipient": {"I'd love to write to this lovely person, but they aren't in my rolodex yet!"}, + "msg_all_done_truncated": {"I did it for you sweetie, however your passion was too great and I had to truncate your message!"}, + "new_friendship_requested": {"Looks like %s would like to get cozy, should we let them in the hot tub?"}, + "accept_unknown_reference": {"As sexy as you are, I still can't find a reference to this request. Perhaps it was withdrawn!"}, + "no_online_users": {"I'm sory sweetheart, no one's online, but I'll always be there for you!"}, + "no_online_requested_users": {"I'm sory sweetheart, these beautiful people arent online, but I'll always be there for you!"}, }, } ) @@ -111,10 +113,12 @@ func (b *Type) getMessageString(msgID string, params ...interface{}) string { if _, ok := msgStrings[mode][msgID]; !ok { mode = msgModeNormal } - if mode != msgModeNormal && msgStrings[mode][msgID] == "" { + if mode != msgModeNormal && len(msgStrings[mode][msgID]) == 0 { mode = msgModeNormal } - if val, ok := msgStrings[mode][msgID]; ok { + if av, ok := msgStrings[mode][msgID]; ok { + ia := utils.RandIdx(len(av) - 1) + val := msgStrings[mode][msgID][ia] if strings.Contains(val, "_BOFH_") { val = strings.ReplaceAll(val, "_BOFH_", utils.GetBOFHExcuse()) } diff --git a/pkg/utils/excuses.go b/pkg/utils/excuses.go index f0bc452..1c29b15 100644 --- a/pkg/utils/excuses.go +++ b/pkg/utils/excuses.go @@ -1,10 +1,5 @@ package utils -import ( - "math/rand" - "time" -) - // BOFHExcuses is a list of excuses from BOFH. // Source: http://pages.cs.wisc.edu/~ballard/bofh/excuses. var BOFHExcuses []string = []string{ @@ -534,11 +529,5 @@ var BOFHExcuses []string = []string{ // GetBOFHExcuse gets a random excuse from a list of excuses and writes it out func GetBOFHExcuse() string { - return BOFHExcuses[randIdx(len(BOFHExcuses)-1)] -} - -// randIdx returns a random number within the specified range. -func randIdx(n int) uint { - rand.Seed(time.Now().UnixNano()) - return uint(rand.Intn(n)) + return BOFHExcuses[RandIdx(len(BOFHExcuses)-1)] } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 77407cd..16679d3 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -1,6 +1,11 @@ package utils -import "github.com/bwmarrin/discordgo" +import ( + "math/rand" + "time" + + "github.com/bwmarrin/discordgo" +) // TruncateString UTF8 truncation of string to `i` length func TruncateString(s string, i int) string { @@ -30,3 +35,12 @@ func SliceContainsString(s []string, e string) bool { } return false } + +// RandIdx returns a random number within the specified range. +func RandIdx(n int) uint { + if n == 0 { + return 0 + } + rand.Seed(time.Now().UnixNano()) + return uint(rand.Intn(n)) +}