Skip to content

Commit 34cec3b

Browse files
authored
Merge pull request #22 from CommanderBot-Dev/invite_descriptions
Added descriptions to `invite`
2 parents 03f010f + 0543ed6 commit 34cec3b

File tree

5 files changed

+70
-21
lines changed

5 files changed

+70
-21
lines changed

commanderbot_ext/ext/invite/invite_cog.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ async def cmd_invites_modify_tags(
155155
):
156156
await self.state[ctx.guild].modify_invite_tags(ctx, invite_key, tags)
157157

158+
@cmd_invites_modify.command(
159+
name="description",
160+
brief="Modify an invite's description"
161+
)
162+
async def cmd_invites_modify_description(
163+
self, ctx: GuildContext, invite_key: str, *, description: str
164+
):
165+
await self.state[ctx.guild].modify_invite_description(ctx, invite_key, description)
166+
158167
# @@ invites configure
159168

160169
@cmd_invites.group(

commanderbot_ext/ext/invite/invite_data.py

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class InviteDataInviteEntry:
3232
hits: int
3333
link: str
3434
tags: Set[str]
35+
description: str
3536

3637
@staticmethod
3738
def deserialize(data: JsonObject, key: str) -> "InviteDataInviteEntry":
@@ -42,6 +43,7 @@ def deserialize(data: JsonObject, key: str) -> "InviteDataInviteEntry":
4243
hits=data["hits"],
4344
link=data["link"],
4445
tags=set(data["tags"]),
46+
description=data["description"]
4547
)
4648

4749
def serialize(self) -> JsonObject:
@@ -51,6 +53,7 @@ def serialize(self) -> JsonObject:
5153
"hits": self.hits,
5254
"link": self.link,
5355
"tags": list(self.tags),
56+
"description": self.description
5457
}
5558

5659
# @implements InviteEntry
@@ -142,6 +145,7 @@ def add_invite(self, invite_key: str, link: str) -> InviteDataInviteEntry:
142145
hits=0,
143146
link=link,
144147
tags=set(),
148+
description=""
145149
)
146150
self.invite_entries[invite_key] = invite_entry
147151
# Return the newly-created invite entry.
@@ -159,29 +163,33 @@ def remove_invite(self, invite_key: str) -> InviteDataInviteEntry:
159163
raise NoSuchInvite(invite_key)
160164

161165
def modify_invite_link(self, invite_key: str, link: str) -> InviteDataInviteEntry:
162-
if invite_entry := self.require_invite_entry(invite_key):
163-
invite_entry.update_modified_on()
164-
invite_entry.link = link
165-
return invite_entry
166-
raise NoSuchInvite(invite_key)
167-
166+
invite_entry = self.require_invite_entry(invite_key):
167+
invite_entry.update_modified_on()
168+
invite_entry.link = link
169+
return invite_entry
170+
168171
def modify_invite_tags(
169172
self, invite_key: str, tags: Tuple[str, ...]
170173
) -> InviteDataInviteEntry:
171-
if invite_entry := self.require_invite_entry(invite_key):
172-
invite_entry.tags = set(tags)
173-
return invite_entry
174-
raise NoSuchInvite(invite_key)
175-
174+
invite_entry = self.require_invite_entry(invite_key):
175+
invite_entry.tags = set(tags)
176+
return invite_entry
177+
178+
def modify_invite_description(
179+
self, invite_key: str, description: str
180+
) -> InviteDataInviteEntry:
181+
invite_entry = self.require_invite_entry(invite_key):
182+
invite_entry.description = description
183+
return invite_entry
184+
176185
def configure_guild_key(self, invite_key: Optional[str]) -> Optional[InviteEntry]:
177186
if not invite_key:
178187
self.guild_key = None
179188
return
180-
if invite_entry := self.require_invite_entry(invite_key):
181-
self.guild_key = invite_key
182-
return invite_entry
183-
raise NoSuchInvite(invite_key)
184-
189+
invite_entry = self.require_invite_entry(invite_key):
190+
self.guild_key = invite_key
191+
return invite_entry
192+
185193

186194
def _guilds_defaultdict_factory() -> DefaultDict[GuildID, InviteDataGuild]:
187195
return defaultdict(lambda: InviteDataGuild())
@@ -266,6 +274,12 @@ async def modify_invite_tags(
266274
) -> InviteEntry:
267275
return self.guilds[guild.id].modify_invite_tags(invite_key, tags)
268276

277+
# @implements InviteStore
278+
async def modify_invite_description(
279+
self, guild: Guild, invite_key: str, description: str
280+
) -> InviteEntry:
281+
return self.guilds[guild.id].modify_invite_description(invite_key, description)
282+
269283
# @implements InviteStore
270284
async def configure_guild_key(
271285
self, guild: Guild, invite_key: Optional[str]

commanderbot_ext/ext/invite/invite_guild_state.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class InviteGuildState(CogGuildState):
2727

2828
async def _show_invite_entry(self, ctx: GuildContext, invite_entry: InviteEntry):
2929
await self.store.increment_invite_hits(invite_entry)
30-
await ctx.send(invite_entry.link)
30+
await ctx.send(f"{invite_entry.description} - {invite_entry.link}")
3131

3232
async def show_invite(self, ctx: GuildContext, invite_query: str):
3333
if invite_entries := await async_expand(
@@ -61,6 +61,7 @@ async def show_invite_details(self, ctx: GuildContext, invite_query: str):
6161
"```",
6262
f"Key: {invite_entry.key}",
6363
f"Tags: {tags_str}",
64+
f"Description: {invite_entry.description}",
6465
f"Added on: {added_on_str}",
6566
f"Modified on: {modified_on_str}",
6667
f"Hits: {invite_entry.hits}",
@@ -78,11 +79,11 @@ async def list_invites(self, ctx: GuildContext):
7879
invite_entries, key=lambda invite_entry: invite_entry.key
7980
)
8081
count = len(sorted_invite_entries)
81-
invite_keys = (invite_entry.key for invite_entry in sorted_invite_entries)
82+
invites = (f"{invite_entry.key} - {invite_entry.description}" for invite_entry in sorted_invite_entries)
8283
text = (
83-
f"There are {count} invites available: `"
84-
+ "` `".join(invite_keys)
85-
+ "`"
84+
f"There are {count} invites available: ```"
85+
+ "` `".join(invites)
86+
+ "```"
8687
)
8788
await ctx.send(text)
8889
else:
@@ -148,6 +149,17 @@ async def modify_invite_tags(
148149
except InviteException as ex:
149150
await ex.respond(ctx)
150151

152+
async def modify_invite_description(
153+
self, ctx: GuildContext, invite_key: str, description: str
154+
):
155+
try:
156+
invite_entry = await self.store.modify_invite_description(self.guild, invite_key, description)
157+
await ctx.send(
158+
f"Set description for invite `{invite_entry.key}` to `{description}`"
159+
)
160+
except InviteException as ex:
161+
await ex.respond(ctx)
162+
151163
async def configure_guild_key(self, ctx: GuildContext, invite_key: Optional[str]):
152164
try:
153165
if invite_entry := await self.store.configure_guild_key(

commanderbot_ext/ext/invite/invite_json_store.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ async def modify_invite_tags(
7878
await self.db.dirty()
7979
return invite_entry
8080

81+
async def modify_invite_description(
82+
self, guild: Guild, invite_key: str, description: str
83+
) -> InviteEntry:
84+
cache = await self.db.get_cache()
85+
invite_entry = await cache.modify_invite_description(guild, invite_key, description)
86+
await self.db.dirty()
87+
return invite_entry
88+
8189
# @implements InviteStore
8290
async def configure_guild_key(
8391
self, guild: Guild, invite_key: Optional[str]

commanderbot_ext/ext/invite/invite_store.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class InviteEntry(Protocol):
2929
hits: int
3030
link: str
3131
tags: Set[str]
32+
description: str
3233

3334
@property
3435
def sorted_tags(self) -> List[str]:
@@ -73,6 +74,11 @@ async def modify_invite_tags(
7374
) -> InviteEntry:
7475
...
7576

77+
async def modify_invite_description(
78+
self, guild: Guild, invite_key: str, description: str
79+
) -> InviteEntry:
80+
...
81+
7682
async def configure_guild_key(
7783
self, guild: Guild, invite_key: Optional[str]
7884
) -> Optional[InviteEntry]:

0 commit comments

Comments
 (0)