Skip to content

Commit 9e97ec8

Browse files
committed
fix: dont hardcode role names
1 parent a44600a commit 9e97ec8

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

.env.example

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ AUTO_THREAD_CHANNEL_ID=... # Channel in which all messages are automati
1111
HELP_BANNED_ROLE_ID=... # THe role that is not allowed to interact with the help channel
1212
CONSUL_ADDR=... # Consul address for the consul database
1313
CONSUL_TOKEN=... # Consul token for the consul database
14-
UPDATES_ROLE_ID=... # The role that is pinged for updates announcements
15-
NEWS_ROLE_ID=... # The role that is pinged for other library/server news
14+
ASSIGNABLE_ROLE_IDS=... # A list of roles split by `,` that are assignable by members

cogs/roles.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,15 @@
1111
if TYPE_CHECKING:
1212
from nextcord.abc import Snowflake
1313

14-
1514
GUILD_ID = int(getenv("GUILD_ID", 0))
16-
UPDATES_ROLE_ID = int(getenv("UPDATES_ROLE_ID", 0))
17-
NEWS_ROLE_ID = int(getenv("NEWS_ROLE_ID", 0))
18-
19-
ROLE_VALUES: dict[str, int] = {
20-
"updates": UPDATES_ROLE_ID,
21-
"news": NEWS_ROLE_ID,
22-
}
15+
ASSIGNABLE_ROLE_IDS = {int(r) for r in getenv("ASSIGNABLE_ROLE_IDS", "[]").split(",")}
2316

2417

2518
class RolesView(View):
26-
def __init__(self, *, user: Member | None):
19+
def __init__(self, *, member: Member | None):
2720
super().__init__(timeout=None)
2821

29-
self.add_item(RolesSelect(user=user))
22+
self.add_item(RolesSelect(member=member))
3023

3124

3225
class FakeMember(Member):
@@ -38,25 +31,25 @@ def get_role(self, *_):
3831

3932

4033
class RolesSelect(Select["RolesView"]):
41-
def __init__(self, *, user: Member | None):
34+
def __init__(self, *, member: Member | None):
4235
# this is being invoked to add persistency
4336
# we only care about custom_id for the store
4437
# we cannot use guild.me as the bot is not ready so a guild is not available
45-
if user is None:
46-
user = FakeMember()
38+
if member is None:
39+
member = FakeMember()
4740

4841
super().__init__(
4942
custom_id="roles:select",
5043
placeholder="Select your new roles",
5144
min_values=0,
52-
max_values=2,
45+
max_values=len(ASSIGNABLE_ROLE_IDS),
5346
options=[
5447
SelectOption(
55-
label=name.capitalize(),
56-
value=name,
57-
default=user.get_role(role_id) is not None,
48+
label=member.guild.get_role(role_id).name, # type: ignore
49+
value=str(role_id),
50+
default=member.get_role(role_id) is not None,
5851
)
59-
for name, role_id in ROLE_VALUES.items()
52+
for role_id in ASSIGNABLE_ROLE_IDS
6053
],
6154
)
6255

@@ -67,27 +60,33 @@ async def callback(self, interaction: Interaction):
6760
# since list is invariant, it cannot be a union
6861
# but apparently Role does not implement Snowflake, this may need a fix
6962

70-
for role, role_id in ROLE_VALUES.items():
71-
if interaction.user.get_role(role_id) is None and role in self.values:
63+
for role_id in ASSIGNABLE_ROLE_IDS:
64+
if (
65+
interaction.user.get_role(role_id) is None
66+
and str(role_id) in self.values
67+
):
7268
# user does not have the role but wants it
7369
roles.append(Object(role_id))
74-
option = get(self.options, value=role)
70+
option = get(self.options, value=str(role_id))
7571
if option is not None:
7672
option.default = True
7773
elif (
7874
interaction.user.get_role(role_id) is not None
79-
and role not in self.values
75+
and str(role_id) not in self.values
8076
):
8177
# user has the role but does not want it
8278
role_ids = [r.id for r in roles]
8379
roles.pop(role_ids.index(role_id))
84-
option = get(self.options, value=role)
80+
option = get(self.options, value=str(role_id))
8581
if option is not None:
8682
option.default = False
8783

8884
await interaction.user.edit(roles=roles)
8985

90-
new_roles = [value.capitalize() for value in self.values]
86+
new_roles = [
87+
interaction.guild.get_role(int(value)).name # type: ignore
88+
for value in self.values
89+
]
9190

9291
await interaction.edit(
9392
content=f"You now have {', '.join(new_roles) or 'no roles'}", view=self.view
@@ -102,7 +101,7 @@ def __init__(self, bot):
102101

103102
async def create_views(self):
104103
if getattr(self.bot, "role_view_set", False) is False:
105-
self.bot.add_view(RolesView(user=None))
104+
self.bot.add_view(RolesView(member=None))
106105
# the view will accept None and only give us a select with a custom_id
107106

108107
self.bot.role_view_set = True
@@ -115,7 +114,7 @@ async def roles(self, interaction: Interaction):
115114

116115
await interaction.send(
117116
"Select your new roles",
118-
view=RolesView(user=interaction.user),
117+
view=RolesView(member=interaction.user),
119118
ephemeral=True,
120119
)
121120

main.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
from nextcord.ext.commands import errors
1111
from nextcord.ext.application_checks import errors as application_errors
1212

13-
bot = commands.Bot("=", intents=Intents(messages=True, guilds=True, members=True, message_content=True))
13+
bot = commands.Bot(
14+
"=", intents=Intents(messages=True, guilds=True, members=True, message_content=True)
15+
)
1416
bot.load_extension("jishaku")
1517

1618
issue_regex = compile(r"##(\d+)")
@@ -36,21 +38,28 @@ async def on_command_error(ctx, error):
3638
return
3739
elif isinstance(error, errors.MissingRole):
3840
role = ctx.guild.get_role(int(error.missing_role)) # type: ignore
39-
await ctx.send(f"\"{role.name}\" is required to use this command.") # type: ignore
41+
await ctx.send(f'"{role.name}" is required to use this command.') # type: ignore
4042
return
4143
else:
4244
await ctx.send(
4345
f"This command raised an exception: `{type(error)}:{str(error)}`"
4446
)
4547

48+
4649
@bot.event
47-
async def on_application_command_error(interaction: Interaction, error: Exception) -> None:
50+
async def on_application_command_error(
51+
interaction: Interaction, error: Exception
52+
) -> None:
4853
if isinstance(error, application_errors.ApplicationMissingRole):
4954
role = interaction.guild.get_role(int(error.missing_role)) # type: ignore
5055
await interaction.send(f"{role.mention} role is required to use this command.", ephemeral=True) # type: ignore
5156
return
5257
else:
53-
await interaction.send(f"This command raised an exception: `{type(error)}:{str(error)}`", ephemeral=True)
58+
await interaction.send(
59+
f"This command raised an exception: `{type(error)}:{str(error)}`",
60+
ephemeral=True,
61+
)
62+
5463

5564
@bot.listen()
5665
async def on_message(message):

0 commit comments

Comments
 (0)