Skip to content

Commit 33b8f21

Browse files
committed
Meta role management
Allow members to join and leave meta roles, for games.
1 parent d84303e commit 33b8f21

1 file changed

Lines changed: 87 additions & 4 deletions

File tree

kiki/plugins/automod/cogs.py

Lines changed: 87 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
- https://discordpy.readthedocs.io/en/latest/ext/commands/cogs.html
99
"""
1010

11-
from discord.ext.commands import Cog
11+
import typing
12+
from discord.ext import commands
1213
from discord.utils import find
14+
from discord import Role
1315

1416

15-
class Automod(Cog):
17+
class Automod(commands.Cog):
1618
"""Automod Cog.
1719
1820
Discord.py extensions Cog, defining all commands and listeners related to
@@ -29,8 +31,89 @@ def __init__(self, bot):
2931
558027628502712334: 722115152832364624,
3032
558085901255704577: 722115302669811785
3133
}
34+
# List of available roles for users to join/leave as they please.
35+
# Eventually this will be moved to database.
36+
self.supported_roles = [
37+
673282506543464488,
38+
700130836527317002,
39+
703783696460939335,
40+
673282438037897284,
41+
673282603440406567,
42+
708866612626718820,
43+
712462587857600523,
44+
717925373647519774,
45+
722123884811386971,
46+
721215716564533279,
47+
722140497812127777,
48+
722140535133044787,
49+
722148256385335346,
50+
722148258516041779,
51+
722148300660277248
52+
]
53+
54+
@commands.command()
55+
async def list(self, ctx: commands.Context):
56+
"""List supported roles.
57+
58+
Args:
59+
ctx: Discord.py context object.
60+
"""
61+
62+
message = "Here's a list of supported roles:\n\n"
63+
64+
for role_id in self.supported_roles:
65+
role = find(lambda x: x.id == role_id, ctx.guild.roles)
66+
if role:
67+
message += f"\"{role.name}\"\n"
68+
69+
message += "\nYou can add roles using the `.join` command, "\
70+
"specifying each role by mention or by full name (in quotes)."
71+
72+
await ctx.send(message)
73+
74+
@commands.command()
75+
async def join(
76+
self,
77+
ctx: commands.Context,
78+
roles: commands.Greedy[typing.Union[Role, str]]):
79+
"""Join supported roles.
80+
81+
Args:
82+
ctx: Discord.py context object.
83+
roles: Collection of roles by mention or by name.
84+
"""
85+
86+
r = []
87+
for role in roles:
88+
if type(role) == str:
89+
role = find(lambda x: x.name == role, ctx.guild.roles)
90+
if role and role.id in self.supported_roles:
91+
r.append(role)
92+
if r:
93+
await ctx.author.add_roles(*r, reason="Subscribed to roles")
94+
95+
@commands.command()
96+
async def leave(
97+
self,
98+
ctx: commands.Context,
99+
roles: commands.Greedy[typing.Union[Role, str]]):
100+
"""Leave supported roles.
101+
102+
Args:
103+
ctx: Discord.py context object.
104+
roles: Collection of roles by mention or by name.
105+
"""
106+
107+
r = []
108+
for role in roles:
109+
if type(role) == str:
110+
role = find(lambda x: x.name == role, ctx.guild.roles)
111+
if role.id in self.supported_roles:
112+
r.append(role)
113+
if r:
114+
await ctx.author.remove_roles(*r, reason="Unsubscribed from roles")
32115

33-
@Cog.listener()
116+
@commands.Cog.listener()
34117
async def on_voice_state_update(self, member, before, after):
35118
"""Voice state listener.
36119
@@ -82,7 +165,7 @@ async def on_voice_state_update(self, member, before, after):
82165
role = find(lambda x: x.id == role_id, member.guild.roles)
83166
await member.add_roles(role, reason="In voice channel")
84167

85-
@Cog.listener()
168+
@commands.Cog.listener()
86169
async def on_member_join(self, member):
87170
"""Message listener.
88171

0 commit comments

Comments
 (0)