async def _dm_owner(self, owner: discord.Member, channel: discord.TextChannel): if owner.id in self.dmed: return with suppressor(Exception): await owner.send( "Hello. I tried to delete a message in {channel} because this " "channel is registered as cooldowned, but I was unable to delete " "the last message. I need the Manage messages permissions to " "delete messages in this channel.\nThis message won't reappear " "until the next bot reboot or cog reload.".format( channel=channel.mention)) self.dmed.append(owner.id)
async def _handle_channel_cooldown(self, message, cooldown_channel, send_dm): now = round(datetime.timestamp(datetime.now())) channel = message.channel user = message.author channel_data = cooldown_channel.get(str(channel.id), None) if not channel_data: return # If user is not registered, add him and stop. if str(user.id) not in channel_data["users_on_cooldown"]: channel_data["users_on_cooldown"][str(message.author.id)] = now cooldown_channel[str(message.channel.id)] = channel_data await self.config.guild(message.guild ).cooldown_channels.set(cooldown_channel) else: last_message_date = channel_data["users_on_cooldown"][str( message.author.id)] total_seconds = now - last_message_date if total_seconds <= channel_data["cooldown_time"]: try: await message.delete() deleted = True except (discord.NotFound, discord.Forbidden): deleted = False pass if send_dm and not user.bot: with suppressor(Exception): remaining_time = humanize_timedelta( seconds=(channel_data["cooldown_time"] - total_seconds)) message_string = Template(await self.config.guild( message.guild).channel_message()) send_me = self._prepare_message( message_string, remaining_time, user.name, channel.name, ) await user.send(send_me) else: deleted = None channel_data["users_on_cooldown"][str(message.author.id)] = now cooldown_channel[str(message.channel.id)] = channel_data await self.config.guild( message.guild).cooldown_channels.set(cooldown_channel) # If message deletion wasn't possible: if deleted is False: await self._dm_owner(message.guild.owner, channel)
async def _handle_category_cooldown(self, message, cooldown_categories, send_dm): now = datetime.now() channel = message.channel user = message.author category_data = cooldown_categories.get(str(channel.category.id), None) if not category_data: return category = channel.category if str(user.id) not in category_data["users_on_cooldown"]: category_data["users_on_cooldown"][str(user.id)] = now.strftime( self.date_format) cooldown_categories[str(category.id)] = category_data await self.config.guild( message.guild).cooldown_categories.set(cooldown_categories) else: last_message = category_data["users_on_cooldown"][str(user.id)] last_message_date = datetime.strptime(last_message, self.date_format) total_seconds = (now - last_message_date).total_seconds() if total_seconds <= category_data["cooldown_time"]: try: await message.delete() deleted = True except (discord.NotFound, discord.Forbidden): deleted = False pass if send_dm: if not user.bot: with suppressor(Exception): remaining_time = humanize_timedelta( seconds=category_data["cooldown_time"] - total_seconds) send_me = ((await self.config.guild( message.guild).category_message()).replace( "{time}", remaining_time).replace( "{category}", category.name).replace( "{member}", message.author.name)) await user.send(send_me) else: deleted = None category_data["users_on_cooldown"][str( user.id)] = now.strftime(self.date_format) cooldown_categories[str(channel.id)] = category_data await self.config.guild( message.guild).cooldown_categories.set(cooldown_categories) # If message deletion wasn't possible: if deleted is False: await self._dm_owner(message.guild.owner, channel)
async def _handle_category_cooldown(self, message, cooldown_category, send_dm): now = round(datetime.timestamp(datetime.now())) channel = message.channel user = message.author category_data = cooldown_category.get(str(channel.category.id), None) if not category_data: return category = channel.category if str(user.id) not in category_data["users_on_cooldown"]: category_data["users_on_cooldown"][str(user.id)] = now cooldown_category[str(category.id)] = category_data await self.config.guild( message.guild).cooldown_categories.set(cooldown_category) else: last_message_date = category_data["users_on_cooldown"][str( user.id)] total_seconds = now - last_message_date if total_seconds <= category_data["cooldown_time"]: try: await message.delete() deleted = True except discord.Forbidden: deleted = False if send_dm and not user.bot: with suppressor(Exception): remaining_time = humanize_timedelta( seconds=category_data["cooldown_time"] - total_seconds) message_string = Template(await self.config.guild( message.guild).category_message()) send_me = self._prepare_message( message_string, remaining_time, user.name, channel.category.name, ) await user.send(send_me) else: deleted = None category_data["users_on_cooldown"][str(user.id)] = now cooldown_category[str(channel.id)] = category_data await self.config.guild( message.guild).cooldown_categories.set(cooldown_category) # If message deletion wasn't possible: if deleted is False: await self._dm_owner(message.guild.owner, channel)