Skip to content

Commit 7c77a3c

Browse files
Merge pull request #19 from WrichikBasu/dev
Modified logic in on_edit and on_delete
2 parents ed7627f + 7cb33ce commit 7c77a3c

File tree

1 file changed

+36
-15
lines changed

1 file changed

+36
-15
lines changed

main.py

+36-15
Original file line numberDiff line numberDiff line change
@@ -393,43 +393,64 @@ async def on_message_delete(self, message: discord.Message) -> None:
393393

394394
if not self.is_ready():
395395
return
396-
397396
if message.author == self.user:
398397
return
399-
400-
# Check if the message is in the channel
401398
if message.channel.id != self._config.channel_id:
402399
return
403-
if not message.reactions:
404-
return
400+
401+
# Check if the deleted message was a legal message
405402
if not all(c in POSSIBLE_CHARACTERS for c in message.content):
406-
return
403+
# The deleted message was not a legal message.
404+
# But, it is possible that this message was edited prior to deletion,
405+
# and the original message could have been a legal number.
406+
# If so, the bot would have reacted to it in some way (tick or cross, we don't care).
407+
# So we iterate through the reactions and check if any of those had been
408+
# added by this bot.
409+
dont_return: bool = False
410+
if message.reactions:
411+
for reaction in message.reactions:
412+
if reaction.me:
413+
# We found a reaction that was posted by this bot.
414+
# => this message was a legal message in the past.
415+
# Therefore, we need to show a deletion alert.
416+
dont_return = True
417+
break
418+
if not dont_return:
419+
# No reactions were found under the message that were posted
420+
# by this bot. So we can safely ignore the message.
421+
return
407422

408423
await message.channel.send(
409-
f'{message.author.mention} deleted their number! '
410-
f'The **next** number is **{self._config.current_count + 1}**.')
424+
f'{message.author.mention} deleted their number!\n'
425+
f'Please note that deleting numbers is **prohibited**, even if it has messed up the count. Repeated '
426+
f'violation of this policy will force the Mods to revoke your access the counting channel permanently.\n\n'
427+
f'The **NEXT** number is **{self._config.current_count + 1}**.')
411428

412429
async def on_message_edit(self, before: discord.Message, after: discord.Message) -> None:
413430
"""Send a message in the channel if a user modifies their input."""
414431

415432
if not self.is_ready():
416433
return
417-
418434
if before.author == self.user:
419435
return
420-
421-
# Check if the message is in the channel
422436
if before.channel.id != self._config.channel_id:
423437
return
424-
if not before.reactions:
425-
return
426-
if not all(c in POSSIBLE_CHARACTERS for c in before.content):
438+
439+
# Check whether the original message or the edited message are
440+
# legal messages. If either of those is a legal message,
441+
# we need to show an alert. No need to check for reactions.
442+
if not (all(c in POSSIBLE_CHARACTERS for c in before.content)
443+
or all(c in POSSIBLE_CHARACTERS for c in after.content)):
427444
return
445+
428446
if before.content == after.content:
429447
return
430448

431449
await after.channel.send(
432-
f'{after.author.mention} edited their number! The **next** number is **{self._config.current_count + 1}**.')
450+
f'{after.author.mention} edited their number! {before.jump_url}\n'
451+
f'Please note that editing numbers is **prohibited**, even if it has messed up the count. Repeated '
452+
f'violation of this policy will force the Mods to revoke your access the counting channel permanently.\n\n'
453+
f'The **NEXT** number is **{self._config.current_count + 1}**.')
433454

434455
async def setup_hook(self) -> None:
435456
await self.tree.sync()

0 commit comments

Comments
 (0)