1package discord 2 3import ( 4 "crypto/sha256" 5 "encoding/hex" 6 "fmt" 7 "github.com/automuteus/utils/pkg/game" 8 "log" 9 "math/rand" 10 "regexp" 11 "strings" 12 13 "github.com/bwmarrin/discordgo" 14) 15 16type UserPatchParameters struct { 17 GuildID string 18 Userdata UserData 19 Deaf bool 20 Mute bool 21} 22 23func getPhaseFromString(input string) game.Phase { 24 if len(input) == 0 { 25 return game.UNINITIALIZED 26 } 27 28 switch strings.ToLower(input) { 29 case "lobby": 30 fallthrough 31 case "l": 32 return game.LOBBY 33 case "task": 34 fallthrough 35 case "t": 36 fallthrough 37 case "tasks": 38 fallthrough 39 case "game": 40 fallthrough 41 case "g": 42 return game.TASKS 43 case "discuss": 44 fallthrough 45 case "disc": 46 fallthrough 47 case "d": 48 fallthrough 49 case "discussion": 50 return game.DISCUSS 51 default: 52 return game.UNINITIALIZED 53 } 54} 55 56func getRoleFromString(s *discordgo.Session, guildID string, input string) string { 57 // find which role the User was referencing in their message 58 // first check if is mentionned 59 ID, err := extractRoleIDFromMention(input) 60 if err == nil { 61 return ID 62 } 63 roles, _ := s.GuildRoles(guildID) 64 for _, role := range roles { 65 if input == role.ID || input == strings.ToLower(role.Name) { 66 return role.ID 67 } 68 } 69 return "" 70} 71 72func generateConnectCode(guildID string) string { 73 h := sha256.New() 74 h.Write([]byte(guildID)) 75 76 // add some randomness 77 h.Write([]byte(fmt.Sprintf("%f", rand.Float64()))) 78 return strings.ToUpper(hex.EncodeToString(h.Sum(nil))[0:8]) 79} 80 81var urlregex = regexp.MustCompile(`^http(?P<secure>s?)://(?P<host>[\w.-]+)(?::(?P<port>\d+))?/?$`) 82 83func formCaptureURL(url, connectCode string) (hyperlink, minimalURL string) { 84 if match := urlregex.FindStringSubmatch(url); match != nil { 85 secure := match[urlregex.SubexpIndex("secure")] == "s" 86 host := match[urlregex.SubexpIndex("host")] 87 port := ":" + match[urlregex.SubexpIndex("port")] 88 89 if port == ":" { 90 if secure { 91 port = ":443" 92 } else { 93 port = ":80" 94 } 95 } 96 97 insecure := "?insecure" 98 protocol := "http://" 99 if secure { 100 insecure = "" 101 protocol = "https://" 102 } 103 104 hyperlink = fmt.Sprintf("aucapture://%s%s/%s%s", host, port, connectCode, insecure) 105 minimalURL = fmt.Sprintf("%s%s%s", protocol, host, port) 106 } else { 107 hyperlink = "Invalid HOST provided (should resemble something like `http://localhost:8123`)" 108 minimalURL = "Invalid HOST provided" 109 } 110 return 111} 112 113func mentionByUserID(userID string) string { 114 return "<@!" + userID + ">" 115} 116 117func sendMessageDM(s *discordgo.Session, userID string, message *discordgo.MessageEmbed) *discordgo.Message { 118 dmChannel, err := s.UserChannelCreate(userID) 119 if err != nil { 120 log.Println(err) 121 } 122 m, err := s.ChannelMessageSendEmbed(dmChannel.ID, message) 123 if err != nil { 124 log.Println(err) 125 } 126 return m 127} 128 129func sendMessageEmbed(s *discordgo.Session, channelID string, message *discordgo.MessageEmbed) *discordgo.Message { 130 msg, err := s.ChannelMessageSendEmbed(channelID, message) 131 if err != nil { 132 log.Println(err) 133 } 134 return msg 135} 136 137func editMessageEmbed(s *discordgo.Session, channelID string, messageID string, message *discordgo.MessageEmbed) *discordgo.Message { 138 msg, err := s.ChannelMessageEditEmbed(channelID, messageID, message) 139 if err != nil { 140 log.Println(err) 141 } 142 return msg 143} 144 145func deleteMessage(s *discordgo.Session, channelID string, messageID string) { 146 err := s.ChannelMessageDelete(channelID, messageID) 147 if err != nil { 148 log.Println(err) 149 } 150} 151 152func addReaction(s *discordgo.Session, channelID, messageID, emojiID string) { 153 err := s.MessageReactionAdd(channelID, messageID, emojiID) 154 if err != nil { 155 log.Println(err) 156 } 157} 158 159func removeAllReactions(s *discordgo.Session, channelID, messageID string) { 160 err := s.MessageReactionsRemoveAll(channelID, messageID) 161 if err != nil { 162 log.Println(err) 163 } 164} 165 166func removeReaction(s *discordgo.Session, channelID, messageID, emojiNameOrID, userID string) { 167 err := s.MessageReactionRemove(channelID, messageID, emojiNameOrID, userID) 168 if err != nil { 169 log.Println(err) 170 } 171} 172 173func matchIDCode(connectCode string, matchID int64) string { 174 return fmt.Sprintf("%s:%d", connectCode, matchID) 175} 176