-
-
Notifications
You must be signed in to change notification settings - Fork 256
Add ability to view previous topics with .topic command #1148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
ecc33e0
7c1f0f8
6241f1e
823e8e7
9ec251a
fdc1894
53ff19f
aee35d7
8bcd48b
a149c6f
5ff99a3
d763ea9
9c518be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,7 @@ def __init__(self, bot: Bot): | |
self.bot = bot | ||
|
||
@staticmethod | ||
def _build_topic_embed(channel_id: int) -> discord.Embed: | ||
def _build_topic_embed(channel_id: int, previous_topic: None | str) -> discord.Embed: | ||
""" | ||
Build an embed containing a conversation topic. | ||
|
||
|
@@ -56,21 +56,45 @@ def _build_topic_embed(channel_id: int) -> discord.Embed: | |
color=discord.Colour.og_blurple() | ||
) | ||
|
||
try: | ||
channel_topics = TOPICS[channel_id] | ||
except KeyError: | ||
# Channel doesn't have any topics. | ||
embed.title = f"**{next(TOPICS['default'])}**" | ||
if previous_topic is None: | ||
# Message first sent | ||
try: | ||
channel_topics = TOPICS[channel_id] | ||
except KeyError: | ||
# Channel doesn't have any topics. | ||
embed.title = f"**{next(TOPICS['default'])}**" | ||
else: | ||
embed.title = f"**{next(channel_topics)}**" | ||
else: | ||
embed.title = f"**{next(channel_topics)}**" | ||
# Message is being edited | ||
try: | ||
channel_topics = TOPICS[channel_id] | ||
except KeyError: | ||
# Channel doesn't have any topics. | ||
new_topic = f"**{next(TOPICS['default'])}**" | ||
else: | ||
new_topic = f"**{next(channel_topics)}**" | ||
|
||
total_topics = previous_topic.count("\n") + 1 | ||
|
||
# Add 1 before first topic | ||
if total_topics == 1: | ||
previous_topic = f"1. {previous_topic}" | ||
|
||
embed.title = previous_topic + f"\n{total_topics + 1}. {new_topic}" | ||
|
||
# When the embed will be larger than the limit, use the previous embed instead | ||
if len(embed.title) > 256: | ||
embed.title = previous_topic | ||
|
||
|
||
return embed | ||
|
||
@staticmethod | ||
def _predicate( | ||
command_invoker: Union[discord.User, discord.Member], | ||
message: discord.Message, | ||
reaction: discord.Reaction, | ||
user: discord.User | ||
command_invoker: Union[discord.User, discord.Member], | ||
message: discord.Message, | ||
reaction: discord.Reaction, | ||
user: discord.User | ||
Anonymous4045 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) -> bool: | ||
user_is_moderator = any(role.id in MODERATION_ROLES for role in getattr(user, "roles", [])) | ||
user_is_invoker = user.id == command_invoker.id | ||
|
@@ -83,9 +107,9 @@ def _predicate( | |
return is_right_reaction | ||
|
||
async def _listen_for_refresh( | ||
self, | ||
command_invoker: Union[discord.User, discord.Member], | ||
message: discord.Message | ||
self, | ||
command_invoker: Union[discord.User, discord.Member], | ||
message: discord.Message | ||
Anonymous4045 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
) -> None: | ||
await message.add_reaction("🔄") | ||
while True: | ||
|
@@ -101,23 +125,25 @@ async def _listen_for_refresh( | |
break | ||
|
||
try: | ||
await message.edit(embed=self._build_topic_embed(message.channel.id)) | ||
# The returned discord.Message object from discord.Message.edit is different from the current | ||
# discord.Message object, so it must be reassigned to update properly | ||
message = await message.edit(embed=self._build_topic_embed(message.channel.id, message.embeds[0].title)) | ||
except discord.NotFound: | ||
break | ||
|
||
with suppress(discord.NotFound): | ||
await message.remove_reaction(reaction, user) | ||
|
||
@commands.command() | ||
@commands.cooldown(1, 60*2, commands.BucketType.channel) | ||
@commands.cooldown(1, 60 * 2, commands.BucketType.channel) | ||
Anonymous4045 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@whitelist_override(channels=ALL_ALLOWED_CHANNELS) | ||
async def topic(self, ctx: commands.Context) -> None: | ||
""" | ||
Responds with a random topic to start a conversation. | ||
|
||
Allows the refresh of a topic by pressing an emoji. | ||
""" | ||
message = await ctx.send(embed=self._build_topic_embed(ctx.channel.id)) | ||
message = await ctx.send(embed=self._build_topic_embed(ctx.channel.id, None)) | ||
self.bot.loop.create_task(self._listen_for_refresh(ctx.author, message)) | ||
|
||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.