Adding user privacy mode, closes #21

master
Matthieu Lalonde 5 years ago
parent dbd2fc12d3
commit f9d8b36993

@ -28,6 +28,55 @@ type UserEmoji struct {
Name string Name string
} }
type userPrivacy uint8
const (
userPrivacyNone userPrivacy = 0
userPrivacyPart userPrivacy = 1 << iota
userPrivacyJoin userPrivacy = 1 << iota
userPrivacyStatus userPrivacy = 1 << iota
userPrivacyAll userPrivacy = 0xFF
)
func upSet(b, flag userPrivacy) userPrivacy { return b | flag }
func upClear(b, flag userPrivacy) userPrivacy { return b &^ flag }
func upToggle(b, flag userPrivacy) userPrivacy { return b ^ flag }
func upHas(b, flag userPrivacy) bool { return b&flag != 0 }
func (up userPrivacy) String() string {
switch up {
case userPrivacyPart:
return "part"
case userPrivacyJoin:
return "join"
case userPrivacyStatus:
return "status"
case userPrivacyAll:
return "all"
case userPrivacyNone:
return "none"
}
return "none"
}
func userPrivacyFromString(up string) userPrivacy {
switch up {
case userPrivacyPart.String():
return userPrivacyPart
case userPrivacyJoin.String():
return userPrivacyJoin
case userPrivacyStatus.String():
return userPrivacyStatus
case userPrivacyAll.String():
return userPrivacyAll
case userPrivacyNone.String():
return userPrivacyNone
}
return userPrivacyNone
}
const userPrivacyDefault = userPrivacyNone
// User is the structure for a single bot user // User is the structure for a single bot user
type User struct { type User struct {
AltVRUserID string AltVRUserID string
@ -36,6 +85,7 @@ type User struct {
DiscordEmoji UserEmoji DiscordEmoji UserEmoji
Role Roles Role Roles
MsgMode msgMode MsgMode msgMode
Privacy userPrivacy
} }
// GetDiscordEmoji If it exists, builds a discord emoji for the user // GetDiscordEmoji If it exists, builds a discord emoji for the user
@ -190,15 +240,23 @@ func (b *Type) periodicTicker() {
} }
func (b *Type) userConnected(u altvr.User) { func (b *Type) userConnected(u altvr.User) {
uu, err := b.getUserByAltVRUserID(u.UserID)
if err != nil {
return
}
s := fmt.Sprintf("%s**%s is now online in %s!**", b.getUserEmojiByAltVRUser(u), u.GetDisplayName(), u.CurrentSpace.Name) s := fmt.Sprintf("%s**%s is now online in %s!**", b.getUserEmojiByAltVRUser(u), u.GetDisplayName(), u.CurrentSpace.Name)
if !b.isQuiet { if !b.isQuiet && !upHas(uu.Privacy, userPrivacyJoin) {
b.dg.Session.ChannelMessageSend(b.DGcID, s) b.dg.Session.ChannelMessageSend(b.DGcID, s)
} }
} }
func (b *Type) userDisconnected(u altvr.User) { func (b *Type) userDisconnected(u altvr.User) {
uu, err := b.getUserByAltVRUserID(u.UserID)
if err != nil {
return
}
s := fmt.Sprintf("%s_%s is now offline!_", b.getUserEmojiByAltVRUser(u), u.GetDisplayName()) s := fmt.Sprintf("%s_%s is now offline!_", b.getUserEmojiByAltVRUser(u), u.GetDisplayName())
if !b.isQuiet { if !b.isQuiet && !upHas(uu.Privacy, userPrivacyPart) {
b.dg.Session.ChannelMessageSend(b.DGcID, s) b.dg.Session.ChannelMessageSend(b.DGcID, s)
} }
} }
@ -294,34 +352,34 @@ func (b *Type) getUserEmojiByAltVRUser(au altvr.User) string {
return "" return ""
} }
func (b *Type) getUserByAltVRUserID(avrID string) (User, error) { func (b *Type) getUserByAltVRUserID(avrID string) (*User, error) {
for _, u := range b.users { for k, u := range b.users {
if u.AltVRUserID == avrID { if u.AltVRUserID == avrID {
return u, nil return &b.users[k], nil
} }
} }
return User{}, errors.New("User not found") return &User{}, errors.New("User not found")
} }
func (b *Type) getUserByDiscordID(discordID string) (User, error) { func (b *Type) getUserByDiscordID(discordID string) (*User, error) {
for _, u := range b.users { for k, u := range b.users {
if u.DiscordID == discordID { if u.DiscordID == discordID {
return u, nil return &b.users[k], nil
} }
} }
return User{}, errors.New("User not found") return &User{}, errors.New("User not found")
} }
func (b *Type) getUserByDiscordName(discordName string) (User, error) { func (b *Type) getUserByDiscordName(discordName string) (*User, error) {
for _, u := range b.users { for k, u := range b.users {
if u.DiscordName == discordName { if u.DiscordName == discordName {
return u, nil return &b.users[k], nil
} }
} }
return User{}, errors.New("User not found") return &User{}, errors.New("User not found")
} }
func (b *Type) buildMention(uu User) string { func (b *Type) buildMention(uu *User) string {
var msg string var msg string
if uu.DiscordEmoji.Name != "" { if uu.DiscordEmoji.Name != "" {
msg = fmt.Sprintf("<:%s:%s> ", uu.DiscordEmoji.Name, uu.DiscordEmoji.ID) msg = fmt.Sprintf("<:%s:%s> ", uu.DiscordEmoji.Name, uu.DiscordEmoji.ID)

@ -25,15 +25,6 @@ func (mm msgMode) String() string {
const msgModeLast = msgModeFlirty const msgModeLast = msgModeFlirty
var (
commandFormats = map[string]string{
"auser": "<Altspace VR Username> <Discord Mention> [Discord Emoji]",
"aemoji": "<Discord Mention> <Discord Emoji>",
"msg": "<Discord Mention> ...",
"accept": "(As reply) <Discord Mention> [Discord Emoji]",
"deny": "(As reply)",
"status": "[Discord Mention...]",
}
msgStrings = map[msgMode]msgMap{ msgStrings = map[msgMode]msgMap{
msgModeNormal: { msgModeNormal: {
@ -102,8 +93,8 @@ func (b *Type) getMessageString(msgID string, params ...interface{}) string {
mode := b.msgMode mode := b.msgMode
p := params p := params
if len(p) >= 1 { if len(p) >= 1 {
if reflect.TypeOf(p[0]) == reflect.TypeOf(User{}) { if reflect.TypeOf(p[0]) == reflect.TypeOf(&User{}) {
u := p[0].(User) u := p[0].(*User)
if u.MsgMode != msgModeNone { if u.MsgMode != msgModeNone {
mode = u.MsgMode mode = u.MsgMode
} }

@ -20,3 +20,13 @@ func SliceContainsChannelType(s []discordgo.ChannelType, e discordgo.ChannelType
} }
return false return false
} }
// SliceContainsString looks for int value in a slice
func SliceContainsString(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}

Loading…
Cancel
Save