generated from nhcarrigan/template
feat: add remaining cohort scripts (#4)
CI / dependency-pin-check-typescript (push) Successful in 5s
CI / dependency-pin-check-python (push) Successful in 4s
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 54s
CI / python (push) Successful in 9m24s
CI / typescript (push) Successful in 9m41s
CI / dependency-pin-check-typescript (push) Successful in 5s
CI / dependency-pin-check-python (push) Successful in 4s
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 54s
CI / python (push) Successful in 9m24s
CI / typescript (push) Successful in 9m41s
### Explanation _No response_ ### Issue _No response_ ### Attestations - [ ] I have read and agree to the [Code of Conduct](https://docs.nhcarrigan.com/community/coc/) - [ ] I have read and agree to the [Community Guidelines](https://docs.nhcarrigan.com/community/guide/). - [ ] My contribution complies with the [Contributor Covenant](https://docs.nhcarrigan.com/dev/covenant/). ### Dependencies - [ ] I have pinned the dependencies to a specific patch version. ### Style - [ ] I have run the linter and resolved any errors. - [ ] My pull request uses an appropriate title, matching the conventional commit standards. - [ ] My scope of feat/fix/chore/etc. correctly matches the nature of changes in my pull request. ### Tests - [ ] My contribution adds new code, and I have added tests to cover it. - [ ] My contribution modifies existing code, and I have updated the tests to reflect these changes. - [ ] All new and existing tests pass locally with my changes. - [ ] Code coverage remains at or above the configured threshold. ### Documentation _No response_ ### Versioning _No response_ Reviewed-on: #4 Co-authored-by: Naomi Carrigan <commits@nhcarrigan.com> Co-committed-by: Naomi Carrigan <commits@nhcarrigan.com>
This commit was merged in pull request #4.
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Update Cohort Leads role permissions to allow pinging in team channels."""
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
# Discord configuration
|
||||
BOT_TOKEN = os.environ["DISCORD_BOT_TOKEN"]
|
||||
GUILD_ID = "692816967895220344"
|
||||
BASE_URL = "https://discord.com/api/v10"
|
||||
|
||||
# Team channel IDs from send_team_messages.py
|
||||
TEAM_CHANNELS = {
|
||||
"Jade Jasmine": {
|
||||
"channel_id": "1464316501573107886",
|
||||
"role_id": "1464314923780931677",
|
||||
},
|
||||
"Crimson Dahlia": {
|
||||
"channel_id": "1464316744909852682",
|
||||
"role_id": "1464315093402784015",
|
||||
},
|
||||
"Rose Camellia": {
|
||||
"channel_id": "1464316751268286611",
|
||||
"role_id": "1464315098452726106",
|
||||
},
|
||||
"Amber Wisteria": {
|
||||
"channel_id": "1464316761410113641",
|
||||
"role_id": "1464315105264275600",
|
||||
},
|
||||
"Ivory Orchid": {
|
||||
"channel_id": "1464316770889240730",
|
||||
"role_id": "1464315109873684593",
|
||||
},
|
||||
"Teal Iris": {
|
||||
"channel_id": "1464316776459407448",
|
||||
"role_id": "1464315114378498152",
|
||||
},
|
||||
"Peach Gardenia": {
|
||||
"channel_id": "1464316785040953543",
|
||||
"role_id": "1464315118904152107",
|
||||
},
|
||||
"Violet Carnation": {
|
||||
"channel_id": "1464316805261824032",
|
||||
"role_id": "1464315124251754559",
|
||||
},
|
||||
"Azure Lotus": {
|
||||
"channel_id": "1464316814455472139",
|
||||
"role_id": "1464315128437801177",
|
||||
},
|
||||
"Coral Sunflower": {
|
||||
"channel_id": "1464316819711066263",
|
||||
"role_id": "1464315132896088168",
|
||||
},
|
||||
"Indigo Tulip": {
|
||||
"channel_id": "1464316826384072925",
|
||||
"role_id": "1464315138428633241",
|
||||
},
|
||||
"Scarlet Hydrangea": {
|
||||
"channel_id": "1464316839306985506",
|
||||
"role_id": "1464315142710890520",
|
||||
},
|
||||
"Mint Narcissus": {
|
||||
"channel_id": "1464316844251807952",
|
||||
"role_id": "1464315149203804405",
|
||||
},
|
||||
"Sage Marigold": {
|
||||
"channel_id": "1464316850669093040",
|
||||
"role_id": "1464315153599299803",
|
||||
},
|
||||
}
|
||||
|
||||
HEADERS = {"Authorization": f"Bot {BOT_TOKEN}", "Content-Type": "application/json"}
|
||||
|
||||
|
||||
def get_guild_roles():
|
||||
"""Get all roles in the guild to find Cohort Leads"""
|
||||
url = f"{BASE_URL}/guilds/{GUILD_ID}/roles"
|
||||
response = requests.get(url, headers=HEADERS)
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
print(f"Error getting roles: {response.status_code} - {response.text}")
|
||||
return None
|
||||
|
||||
|
||||
def find_cohort_leads_role(roles):
|
||||
"""Find the Cohort Leads role from the list"""
|
||||
for role in roles:
|
||||
if "cohort" in role["name"].lower() and "lead" in role["name"].lower():
|
||||
return role
|
||||
return None
|
||||
|
||||
|
||||
def update_channel_permissions(channel_id, role_id, team_name):
|
||||
"""Update channel permissions for a specific role"""
|
||||
url = f"{BASE_URL}/channels/{channel_id}/permissions/{role_id}"
|
||||
|
||||
# Permission bits:
|
||||
# MENTION_EVERYONE = 1 << 17 = 131072
|
||||
# PIN_MESSAGES = 1 << 51 = 2251799813685248
|
||||
# Combined: 131072 + 2251799813685248 = 2251799813816320
|
||||
|
||||
data = {
|
||||
"allow": "2251799813816320", # MENTION_EVERYONE + PIN_MESSAGES permissions
|
||||
"deny": "0",
|
||||
"type": 0, # Role permission type
|
||||
}
|
||||
|
||||
response = requests.put(url, headers=HEADERS, json=data)
|
||||
|
||||
if response.status_code == 204:
|
||||
print(f"✓ Updated permissions for {team_name} channel")
|
||||
return True
|
||||
elif response.status_code == 429:
|
||||
retry_after = float(response.headers.get("Retry-After", 1))
|
||||
print(f" Rate limited! Waiting {retry_after:.2f}s...")
|
||||
time.sleep(retry_after)
|
||||
return update_channel_permissions(channel_id, role_id, team_name)
|
||||
else:
|
||||
print(
|
||||
f"✗ Failed to update {team_name} channel: "
|
||||
f"{response.status_code} - {response.text}"
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
print("Fetching guild roles...")
|
||||
roles = get_guild_roles()
|
||||
|
||||
if not roles:
|
||||
print("Failed to fetch roles!")
|
||||
return
|
||||
|
||||
cohort_leads_role = find_cohort_leads_role(roles)
|
||||
|
||||
if not cohort_leads_role:
|
||||
print("Could not find Cohort Leads role!")
|
||||
print("\nAvailable roles:")
|
||||
for role in roles:
|
||||
print(f" - {role['name']} (ID: {role['id']})")
|
||||
return
|
||||
|
||||
print(
|
||||
f"\nFound Cohort Leads role: {cohort_leads_role['name']} "
|
||||
f"(ID: {cohort_leads_role['id']})"
|
||||
)
|
||||
print(f"Updating permissions for {len(TEAM_CHANNELS)} team channels...")
|
||||
print("-" * 50)
|
||||
|
||||
success_count = 0
|
||||
fail_count = 0
|
||||
|
||||
for team_name, team_data in TEAM_CHANNELS.items():
|
||||
if update_channel_permissions(
|
||||
team_data["channel_id"], cohort_leads_role["id"], team_name
|
||||
):
|
||||
success_count += 1
|
||||
else:
|
||||
fail_count += 1
|
||||
|
||||
# Small delay between requests
|
||||
time.sleep(0.1)
|
||||
|
||||
print("-" * 50)
|
||||
print(f"Complete! Success: {success_count}, Failed: {fail_count}")
|
||||
|
||||
if success_count == len(TEAM_CHANNELS):
|
||||
print("\n✅ All team channels have been updated!")
|
||||
print("Cohort Leads can now:")
|
||||
print(" - Use @everyone, @here, and mention all roles")
|
||||
print(" - Pin and unpin messages")
|
||||
print(
|
||||
"\nNote: They cannot view these channels unless they have "
|
||||
"the specific team role."
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user