-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update groups.groovy #3
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,40 +4,24 @@ import groovy.json.JsonOutput | |
def createGroup(String groupName, List groupMembers, String groupType) { | ||
return JsonOutput.toJson([ | ||
name: groupName, | ||
members: groupMembers, | ||
discordID: groupDiscordID, | ||
swarmID: groupSwarmID, | ||
type: groupType | ||
]); | ||
} | ||
|
||
def getGroupMembers(groups, String groupName) { | ||
for(group in groups) { | ||
if(group.name == groupName) { | ||
return group.members; | ||
} | ||
} | ||
|
||
log.error("Tried to get group members for group '${groupName}', but this group couldn't be found!"); | ||
return []; | ||
} | ||
|
||
def getGroupType(groups, String groupName) { | ||
for(group in groups) { | ||
if(group.name == groupName) { | ||
return group.type; | ||
} | ||
} | ||
|
||
log.error("Tried to get group type for group '${groupName}', but this group couldn't be found!"); | ||
return []; | ||
} | ||
|
||
def mentionGroup(groups, String groupName) { | ||
def groupMembers = getGroupMembers(groups, groupName); | ||
def groupType = getGroupType(groups, groupName); | ||
|
||
if(!groupMembers && !groupType) { | ||
return ''; | ||
} | ||
def groupType = "" | ||
def discordID = "" | ||
def groupsParsed = new JsonSlurper().parseText(groups) | ||
|
||
groupsParsed.groups.each { group -> | ||
if (group.name == groupName) | ||
{ | ||
groupType = group.type | ||
discordID = group.discordID | ||
} | ||
} | ||
Comment on lines
+18
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to break out of this loop prematurely? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Breaking out of for-each loops in Groovy is illegal ([https://stackoverflow.com/questions/3049790/can-you-break-from-a-groovy-each-closure]). However using find ([https://www.tutorialspoint.com/groovy/groovy_find.htm]) we can break out of the loop prematurely using return true. |
||
|
||
def tagTemplate = ''; | ||
|
||
|
@@ -59,15 +43,12 @@ def mentionGroup(groups, String groupName) { | |
|
||
def mentionString = ''; | ||
|
||
for(member in groupMembers) { | ||
discordID = member.value; | ||
|
||
if(discordID?.trim()) { | ||
mentionString += "${tagTemplate.replace('[id]', discordID)}, "; | ||
} | ||
} | ||
|
||
// Remove trailing ', ' | ||
if(discordID?.trim()) { | ||
mentionString += "${tagTemplate.replace('[id]', discordID)}, "; | ||
} | ||
|
||
// Not needed anymore? | ||
Remove trailing ', ' | ||
message = message.substring(0, message.length() - 2); | ||
Comment on lines
+46
to
52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yea |
||
|
||
message = "${groupName}: ${message}"; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't
loadGroups
already parse the groups?If you're passing the full group json into this function, maybe split
loadGroups
up intoloadGroupsFromFile
andloadGroupsFromString
, or haveloadGroups
return the json string rather than a parsed version. (This would also mean thatsaveGroups
shouldn't convert the groups to json, as they would already be in that format).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah missed that function. I suppose we can remove loadGroups (and pass file as string as argument) or convert loadGroups to just reading in the file and parsing it using JsonSlurper and return the JsonObject.