Skip to content

Commit

Permalink
Merge pull request #350 from denverquane/spectator
Browse files Browse the repository at this point in the history
Spectator Setting
  • Loading branch information
denverquane committed Jan 5, 2021
2 parents 7c53bd5 + 54ed71c commit 621f040
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 16 deletions.
1 change: 0 additions & 1 deletion amongus/playerData.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type PlayerData struct {
}

const UnlinkedPlayerName = "UnlinkedPlayer"
const SpectatorPlayerName = "SpectatorSpectator" //this is silly, but is >10 chars, so can never happen in-amongus...

var UnlinkedPlayer = PlayerData{
Color: -1,
Expand Down
6 changes: 3 additions & 3 deletions discord/eventHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ func (bot *Bot) SubscribeToGameByConnectCode(guildID, connectCode string, endGam
correlatedUserID = userID
case task.GameOverJob:
var gameOverResult game.Gameover
log.Println("Successfully identified game over event:")
log.Println(job.Payload)
// log.Println("Successfully identified game over event:")
// log.Println(job.Payload)
err := json.Unmarshal([]byte(job.Payload.(string)), &gameOverResult)
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -477,7 +477,7 @@ func dumpGameToPostgres(dgs GameState, psql *storage.PsqlInterface, gameOver gam
gameOver.GameOverReason == game.ImpostorDisconnect

for _, v := range dgs.UserData {
if v.GetPlayerName() != amongus.UnlinkedPlayerName && v.GetPlayerName() != amongus.SpectatorPlayerName {
if v.GetPlayerName() != amongus.UnlinkedPlayerName {
inGameData, found := dgs.AmongUsData.GetByName(v.GetPlayerName())
if !found {
log.Println("No game data found for that player")
Expand Down
17 changes: 17 additions & 0 deletions discord/gameStateMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ var DeferredEditsLock = sync.Mutex{}
// Note this is not a pointer; we never expect the underlying DGS to change on an edit
func (dgs GameState) Edit(s *discordgo.Session, me *discordgo.MessageEmbed) bool {
newEdit := false

if !ValidFields(me) {
return false
}

DeferredEditsLock.Lock()

// if it isn't found, then start the worker to wait to start it (this is a UNIQUE edit)
Expand All @@ -74,6 +79,18 @@ func (dgs GameState) Edit(s *discordgo.Session, me *discordgo.MessageEmbed) bool
return newEdit
}

func ValidFields(me *discordgo.MessageEmbed) bool {
for _, v := range me.Fields {
if v == nil {
return false
}
if v.Name == "" || v.Value == "" {
return false
}
}
return true
}

func RemovePendingDGSEdit(messageID string) {
DeferredEditsLock.Lock()
delete(DeferredEdits, messageID)
Expand Down
22 changes: 17 additions & 5 deletions discord/message_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"strings"
"time"

"github.com/denverquane/amongusdiscord/amongus"
"github.com/denverquane/amongusdiscord/storage"

"github.com/bwmarrin/discordgo"
Expand All @@ -29,7 +28,7 @@ const DefaultMaxActiveGames = 150
const downloadURL = "https://capture.automute.us"

func (bot *Bot) handleMessageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// IgnoreSpectator all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
Expand Down Expand Up @@ -152,7 +151,7 @@ func (bot *Bot) handleMessageCreate(s *discordgo.Session, m *discordgo.MessageCr
}

func (bot *Bot) handleReactionGameStartAdd(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
// Ignore all reactions created by the bot itself
// IgnoreSpectator all reactions created by the bot itself
if m.UserID == s.State.User.ID {
return
}
Expand Down Expand Up @@ -318,9 +317,22 @@ func (bot *Bot) handleVoiceStateChange(s *discordgo.Session, m *discordgo.VoiceS
tracked := m.ChannelID != "" && dgs.Tracking.ChannelID == m.ChannelID

auData, found := dgs.AmongUsData.GetByName(userData.InGameName)

var isAlive bool

// only actually tracked if we're in a tracked channel AND linked to a player
tracked = tracked && (found || userData.GetPlayerName() == amongus.SpectatorPlayerName)
mute, deaf := sett.GetVoiceState(auData.IsAlive, tracked, dgs.AmongUsData.GetPhase())
if !sett.GetMuteSpectator() {
tracked = tracked && found
isAlive = auData.IsAlive
} else {
if !found {
// we just assume the spectator is dead
isAlive = false
} else {
isAlive = auData.IsAlive
}
}
mute, deaf := sett.GetVoiceState(isAlive, tracked, dgs.AmongUsData.GetPhase())
// check the userdata is linked here to not accidentally undeafen music bots, for example
if found && (userData.ShouldBeDeaf != deaf || userData.ShouldBeMute != mute) && (mute != m.Mute || deaf != m.Deaf) {
userData.SetShouldBeMuteDeaf(mute, deaf)
Expand Down
20 changes: 20 additions & 0 deletions discord/setting/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
LeaderboardMention
LeaderboardSize
LeaderboardMin
MuteSpectators
Show
Reset
NullSetting
Expand Down Expand Up @@ -302,6 +303,25 @@ var AllSettings = []Setting{
Aliases: []string{"lboardmin", "boardmin", "leadermin", "min"},
Premium: true,
},
{
SettingType: MuteSpectators,
Name: "muteSpectators",
Example: "muteSpectators true",
ShortDesc: &i18n.Message{
ID: "settings.AllSettings.MuteSpectators.shortDesc",
Other: "Mute Spectators like Dead Players",
},
Description: &i18n.Message{
ID: "settings.AllSettings.MuteSpectators.desc",
Other: "Whether or not the bot should treat spectators like dead players (respecting your voice rules).\n**Note, this can cause delays or slowdowns when not self-hosting, or using a Premium worker bot!**",
},
Arguments: &i18n.Message{
ID: "settings.AllSettings.MuteSpectators.args",
Other: "<true/false>",
},
Aliases: []string{"mutespectator", "mutespec", "spectators", "spectator", "spec"},
Premium: true,
},
{
SettingType: Show,
Name: "show",
Expand Down
57 changes: 57 additions & 0 deletions discord/setting_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ func (bot *Bot) HandleSettingsCommand(s *discordgo.Session, m *discordgo.Message
break
}
isValid = SettingLeaderboardMin(s, m, sett, args)
case setting.MuteSpectators:
if !prem {
s.ChannelMessageSend(m.ChannelID, nonPremiumSettingResponse(sett))
break
}
isValid = SettingMuteSpectators(s, m, sett, args)
case setting.Show:
jBytes, err := json.MarshalIndent(sett, "", " ")
if err != nil {
Expand Down Expand Up @@ -1049,3 +1055,54 @@ func SettingLeaderboardMin(s *discordgo.Session, m *discordgo.MessageCreate, set

return true
}

func SettingMuteSpectators(s *discordgo.Session, m *discordgo.MessageCreate, sett *storage.GuildSettings, args []string) bool {
muteSpec := sett.GetMuteSpectator()
if len(args) == 2 {
current := "false"
if muteSpec {
current = "true"
}
embed := ConstructEmbedForSetting(current, setting.AllSettings[setting.MuteSpectators], sett)
s.ChannelMessageSendEmbed(m.ChannelID, &embed)
return false
}
switch {
case args[2] == "true":
if muteSpec {
s.ChannelMessageSend(m.ChannelID, sett.LocalizeMessage(&i18n.Message{
ID: "settings.SettingUnmuteDeadDuringTasks.true_noUnmuteDead",
Other: "It's already true!",
}))
} else {
s.ChannelMessageSend(m.ChannelID, sett.LocalizeMessage(&i18n.Message{
ID: "settings.SettingMuteSpectators.true_noMuteSpectators",
Other: "I will now mute spectators just like dead players. \n**Note, this can cause delays or slowdowns when not self-hosting, or using a Premium worker bot!**",
}))
sett.SetMuteSpectator(true)
return true
}
case args[2] == "false":
if muteSpec {
s.ChannelMessageSend(m.ChannelID, sett.LocalizeMessage(&i18n.Message{
ID: "settings.SettingMuteSpectators.false_muteSpectators",
Other: "I will no longer mute spectators like dead players",
}))
sett.SetMuteSpectator(false)
return true
}
s.ChannelMessageSend(m.ChannelID, sett.LocalizeMessage(&i18n.Message{
ID: "settings.SettingUnmuteDeadDuringTasks.false_noUnmuteDead",
Other: "It's already false!",
}))
default:
s.ChannelMessageSend(m.ChannelID, sett.LocalizeMessage(&i18n.Message{
ID: "settings.SettingUnmuteDeadDuringTasks.wrongArg",
Other: "Sorry, `{{.Arg}}` is neither `true` nor `false`.",
},
map[string]interface{}{
"Arg": args[2],
}))
}
return false
}
24 changes: 18 additions & 6 deletions discord/voice.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"github.com/automuteus/utils/pkg/task"
"github.com/bsm/redislock"
"github.com/bwmarrin/discordgo"
"github.com/denverquane/amongusdiscord/amongus"
"github.com/denverquane/amongusdiscord/storage"
"log"
"strconv"
Expand Down Expand Up @@ -126,25 +125,38 @@ func (bot *Bot) handleTrackedMembers(sess *discordgo.Session, sett *storage.Guil

tracked := voiceState.ChannelID != "" && dgs.Tracking.ChannelID == voiceState.ChannelID

auData, linked := dgs.AmongUsData.GetByName(userData.InGameName)
auData, found := dgs.AmongUsData.GetByName(userData.InGameName)
// only actually tracked if we're in a tracked channel AND linked to a player
tracked = tracked && (linked || userData.GetPlayerName() == amongus.SpectatorPlayerName)
shouldMute, shouldDeaf := sett.GetVoiceState(auData.IsAlive, tracked, dgs.AmongUsData.GetPhase())
var isAlive bool

// only actually tracked if we're in a tracked channel AND linked to a player
if !sett.GetMuteSpectator() {
tracked = tracked && found
isAlive = auData.IsAlive
} else {
if !found {
// we just assume the spectator is dead
isAlive = false
} else {
isAlive = auData.IsAlive
}
}
shouldMute, shouldDeaf := sett.GetVoiceState(isAlive, tracked, dgs.AmongUsData.GetPhase())

incorrectMuteDeafenState := shouldMute != userData.ShouldBeMute || shouldDeaf != userData.ShouldBeDeaf

// only issue a change if the User isn't in the right state already
// nicksmatch can only be false if the in-game data is != nil, so the reference to .audata below is safe
// check the userdata is linked here to not accidentally undeafen music bots, for example
if linked && incorrectMuteDeafenState {
if incorrectMuteDeafenState && (found || sett.GetMuteSpectator()) {
uid, _ := strconv.ParseUint(userData.User.UserID, 10, 64)
userModify := task.UserModify{
UserID: uid,
Mute: shouldMute,
Deaf: shouldDeaf,
}

if handlePriority != NoPriority && ((handlePriority == AlivePriority && auData.IsAlive) || (handlePriority == DeadPriority && !auData.IsAlive)) {
if handlePriority != NoPriority && ((handlePriority == AlivePriority && isAlive) || (handlePriority == DeadPriority && !isAlive)) {
users = append([]task.UserModify{userModify}, users...)
priorityRequests++ // counter of how many elements on the front of the arr should be sent first
} else {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ go 1.15
require (
github.com/BurntSushi/toml v0.3.1
github.com/automuteus/galactus v1.2.2
github.com/automuteus/utils v0.0.5
github.com/automuteus/utils v0.0.6
github.com/bsm/redislock v0.7.0
github.com/bwmarrin/discordgo v0.22.0
github.com/georgysavva/scany v0.2.7
Expand All @@ -23,3 +23,5 @@ require (
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
)

replace github.com/bwmarrin/discordgo v0.22.0 => github.com/automuteus/discordgo v0.22.1
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hC
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/aryann/difflib v0.0.0-20170710044230-e206f873d14a/go.mod h1:DAHtR1m6lCRdSC2Tm3DSWRPvIPr6xNKyeHdqDQSQT+A=
github.com/automuteus/discordgo v0.22.1 h1:IU58OXAtpTosrYq/MCMVbpoVuOuUeandgyju5MpuLQo=
github.com/automuteus/discordgo v0.22.1/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M=
github.com/automuteus/galactus v1.2.2 h1:8OMVzXutsHO+R1D1lW6qxzmZrAwNyTADw503B1ARGgg=
github.com/automuteus/galactus v1.2.2/go.mod h1:fWFnpCRyOs9wSVHvksupPlMPiAEO6CGhbI+uPtrmJcs=
github.com/automuteus/utils v0.0.5 h1:oKuCSLnEBk6zZMISRXqSGC3wK6hkpX4mSrVYHH+TmwM=
github.com/automuteus/utils v0.0.5/go.mod h1:3/DMXEOYYnBADTtb0rbBsDBD+d+QDdIpBRbvhy8cPoI=
github.com/automuteus/utils v0.0.6 h1:u14BvwfFzHN8A0aJ4qbeG7vkkyK2brnBYSOjMakWars=
github.com/automuteus/utils v0.0.6/go.mod h1:3/DMXEOYYnBADTtb0rbBsDBD+d+QDdIpBRbvhy8cPoI=
github.com/aws/aws-lambda-go v1.13.3/go.mod h1:4UKl9IzQMoD+QF79YdCuzCwp8VbmG4VAQwij/eHl5CU=
github.com/aws/aws-sdk-go v1.27.0/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go-v2 v0.18.0/go.mod h1:JWVYvqSMppoMJC0x5wdwiImzgXTI9FuZwxzkQq9wy+g=
Expand Down
10 changes: 10 additions & 0 deletions storage/guildSettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type GuildSettings struct {
LeaderboardMention bool `json:"leaderboardMention"`
LeaderboardSize int `json:"leaderboardSize"`
LeaderboardMin int `json:"leaderboardMin"`
MuteSpectator bool `json:"muteSpectator"`
}

func MakeGuildSettings() *GuildSettings {
Expand All @@ -50,6 +51,7 @@ func MakeGuildSettings() *GuildSettings {
LeaderboardMention: true,
LeaderboardSize: 3,
LeaderboardMin: 3,
MuteSpectator: false,
lock: sync.RWMutex{},
}
}
Expand Down Expand Up @@ -165,6 +167,14 @@ func (gs *GuildSettings) SetLeaderboardMin(v int) {
gs.LeaderboardMin = v
}

func (gs *GuildSettings) GetMuteSpectator() bool {
return gs.MuteSpectator
}

func (gs *GuildSettings) SetMuteSpectator(behavior bool) {
gs.MuteSpectator = behavior
}

func (gs *GuildSettings) GetMapVersion() string {
if gs.MapVersion == "" {
return "simple"
Expand Down

0 comments on commit 621f040

Please sign in to comment.