Adding project
This commit is contained in:
commit
3bd18050cb
9 files changed
+730
No files matched your search
@@ -0,0 +1,133 @@
|
||||
import discord
|
||||
from discord import app_commands
|
||||
from discord.ext import commands
|
||||
|
||||
import config
|
||||
import data
|
||||
|
||||
|
||||
class Channels(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
@app_commands.command(
|
||||
name="setup_county_channels",
|
||||
description="Create private county channels grouped by region",
|
||||
)
|
||||
@app_commands.guilds(config.GUILD)
|
||||
@app_commands.default_permissions(manage_roles=True)
|
||||
@app_commands.guild_only()
|
||||
async def setup_county_channels(self, interaction: discord.Interaction):
|
||||
await interaction.response.defer(ephemeral=True)
|
||||
guild = interaction.guild
|
||||
roles = {r.name: r for r in guild.roles}
|
||||
|
||||
assigned = [c for cs in data.REGION_COUNTIES.values() for c in cs]
|
||||
dups = sorted({c for c in assigned if assigned.count(c) > 1})
|
||||
unassigned = sorted(set(data.COUNTIES) - set(assigned))
|
||||
unknown = sorted(set(assigned) - set(data.COUNTIES))
|
||||
if dups or unassigned or unknown:
|
||||
await interaction.followup.send(
|
||||
"REGION_COUNTIES needs fixing. "
|
||||
f"Duplicates: {dups or 'none'}. Unassigned: {unassigned or 'none'}. "
|
||||
f"Unknown names: {unknown or 'none'}.",
|
||||
ephemeral=True,
|
||||
)
|
||||
return
|
||||
|
||||
required = data.COUNTIES + list(data.REGION_COUNTIES)
|
||||
missing = [n for n in required if n not in roles]
|
||||
if missing:
|
||||
await interaction.followup.send(
|
||||
f"{len(missing)} roles are missing ({', '.join(missing[:10])}...). "
|
||||
"Run /setup_roles first.",
|
||||
ephemeral=True,
|
||||
)
|
||||
return
|
||||
|
||||
notes = []
|
||||
created = updated = 0
|
||||
try:
|
||||
founder = roles.get(data.FOUNDER_ROLE)
|
||||
if founder is None:
|
||||
founder = await guild.create_role(
|
||||
name=data.FOUNDER_ROLE, reason="County channels setup"
|
||||
)
|
||||
notes.append(
|
||||
f"Created the {data.FOUNDER_ROLE} role. Assign it to people yourself."
|
||||
)
|
||||
|
||||
access = discord.PermissionOverwrite(
|
||||
view_channel=True, send_messages=True, read_message_history=True
|
||||
)
|
||||
hidden = discord.PermissionOverwrite(view_channel=False)
|
||||
|
||||
# Found by name so channels from earlier runs (including the old
|
||||
# "Counties: A to B" categories) are moved rather than duplicated.
|
||||
existing = {
|
||||
c.name: c
|
||||
for c in guild.text_channels
|
||||
if c.category
|
||||
and (
|
||||
c.category.name.startswith("Counties:")
|
||||
or c.category.name.endswith(" Alabama")
|
||||
)
|
||||
}
|
||||
|
||||
for region, counties in data.REGION_COUNTIES.items():
|
||||
cat_name = f"{region} Alabama"
|
||||
category = discord.utils.get(guild.categories, name=cat_name)
|
||||
if category is None:
|
||||
category = await guild.create_category(
|
||||
cat_name,
|
||||
overwrites={
|
||||
guild.default_role: hidden,
|
||||
founder: access,
|
||||
guild.me: access,
|
||||
},
|
||||
reason="County channels setup",
|
||||
)
|
||||
|
||||
wanted = [(f"{region.lower()}-general", roles[region])]
|
||||
wanted += [(c.lower().replace(" ", "-"), roles[c]) for c in counties]
|
||||
|
||||
for chan_name, role in wanted:
|
||||
overwrites = {
|
||||
guild.default_role: hidden,
|
||||
role: access,
|
||||
founder: access,
|
||||
guild.me: access,
|
||||
}
|
||||
channel = existing.get(chan_name)
|
||||
if channel is None:
|
||||
await guild.create_text_channel(
|
||||
chan_name,
|
||||
category=category,
|
||||
overwrites=overwrites,
|
||||
reason="County channels setup",
|
||||
)
|
||||
created += 1
|
||||
else:
|
||||
await channel.edit(category=category, overwrites=overwrites)
|
||||
updated += 1
|
||||
|
||||
for cat in guild.categories:
|
||||
if cat.name.startswith("Counties:") and not cat.channels:
|
||||
notes.append(
|
||||
f'The old category "{cat.name}" is empty now. You can delete it.'
|
||||
)
|
||||
except discord.Forbidden:
|
||||
await interaction.followup.send(
|
||||
"I'm missing a permission (Manage Channels and Manage Roles are needed).",
|
||||
ephemeral=True,
|
||||
)
|
||||
return
|
||||
|
||||
msg = f"Created {created} channels, updated {updated}."
|
||||
if notes:
|
||||
msg += "\n" + "\n".join(notes)
|
||||
await interaction.followup.send(msg[:1900], ephemeral=True)
|
||||
|
||||
|
||||
async def setup(bot):
|
||||
await bot.add_cog(Channels(bot))
|
||||
Reference in new issue
Block a user