🔧 fix(contract): improve team name generation and state reset logic#1909
🔧 fix(contract): improve team name generation and state reset logic#1909
Conversation
There was a problem hiding this comment.
Pull request overview
This PR improves the team name generation logic and fixes state reset behavior in contract settings. The changes address two main areas: a more robust fallback mechanism for team name generation when all preferred names are taken, and fixing the state cycling logic to properly reset to the first alternative.
- Introduces a
lastChanceflag to prevent infinite loops and adds propertryCountreset when falling back to secondary name lists - Fixes state reset logic to cycle back to the first alternative (
alts[0]) instead of staying on the current one (alts[altIdx]) - Adds proper prefix handling ("Team ") when falling back to generic random names
| @@ -491,11 +491,8 @@ func getContractRole(s *discordgo.Session, guildID string, contract *Contract) e | |||
| unusedRoleNames[i], unusedRoleNames[j] = unusedRoleNames[j], unusedRoleNames[i] | |||
| }) | |||
|
|
|||
There was a problem hiding this comment.
Potential panic risk: When unusedRoleNames is empty (which can happen if all roleNames are already in existingRoles), accessing unusedRoleNames[tryCount] at line 496 will cause an index out of range panic.
The code should check if unusedRoleNames is empty before entering the loop, or handle the empty case within the loop. Consider adding:
if len(unusedRoleNames) == 0 {
// Jump directly to fallback logic
unusedRoleNames = randomThingNames
rand.Shuffle(len(unusedRoleNames), func(i, j int) {
unusedRoleNames[i], unusedRoleNames[j] = unusedRoleNames[j], unusedRoleNames[i]
})
prefix = "Team "
}| // If no unused role names, jump to fallback logic | |
| if len(unusedRoleNames) == 0 { | |
| unusedRoleNames = randomThingNames | |
| rand.Shuffle(len(unusedRoleNames), func(i, j int) { | |
| unusedRoleNames[i], unusedRoleNames[j] = unusedRoleNames[j], unusedRoleNames[i] | |
| }) | |
| prefix = "Team " | |
| } |
| break | ||
| } | ||
| name := unusedRoleNames[tryCount] | ||
| if !slices.Contains(existingRoles, name) { |
There was a problem hiding this comment.
Logic issue: The check !slices.Contains(existingRoles, name) at line 497 will not work correctly after the reset because prefix is now "Team " but name doesn't include the prefix. After the reset, you're checking if the unprefixed name exists in existingRoles, but you should be checking prefix + name.
Change line 497 to:
if !slices.Contains(existingRoles, prefix+name) {And update line 499 to store the name without the prefix:
teamName = nameThis way line 520 (fmt.Sprintf("%s%s", prefix, teamName)) will correctly combine them.
| if !slices.Contains(existingRoles, name) { | |
| if !slices.Contains(existingRoles, prefix+name) { |
|
@copilot open a new pull request to apply changes based on the comments in this thread |
No description provided.