Initial commit
This commit is contained in:
2
Bot/utils/__init__.py
Normal file
2
Bot/utils/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
from .constants import * # noqa
|
||||
from .actions import * # noqa
|
||||
BIN
Bot/utils/__pycache__/__init__.cpython-311.pyc
Normal file
BIN
Bot/utils/__pycache__/__init__.cpython-311.pyc
Normal file
Binary file not shown.
BIN
Bot/utils/__pycache__/__init__.cpython-39.pyc
Normal file
BIN
Bot/utils/__pycache__/__init__.cpython-39.pyc
Normal file
Binary file not shown.
BIN
Bot/utils/__pycache__/actions.cpython-311.pyc
Normal file
BIN
Bot/utils/__pycache__/actions.cpython-311.pyc
Normal file
Binary file not shown.
BIN
Bot/utils/__pycache__/actions.cpython-39.pyc
Normal file
BIN
Bot/utils/__pycache__/actions.cpython-39.pyc
Normal file
Binary file not shown.
BIN
Bot/utils/__pycache__/constants.cpython-311.pyc
Normal file
BIN
Bot/utils/__pycache__/constants.cpython-311.pyc
Normal file
Binary file not shown.
BIN
Bot/utils/__pycache__/constants.cpython-39.pyc
Normal file
BIN
Bot/utils/__pycache__/constants.cpython-39.pyc
Normal file
Binary file not shown.
99
Bot/utils/actions.py
Normal file
99
Bot/utils/actions.py
Normal file
@@ -0,0 +1,99 @@
|
||||
# mypy: disable-error-code=attr-defined
|
||||
import os
|
||||
import datetime as dt
|
||||
from bot import Posts
|
||||
from pathlib import Path
|
||||
from enum import Enum
|
||||
from typing import List
|
||||
from logger import Logger
|
||||
from sqlitewrapper import Row
|
||||
|
||||
|
||||
__all__ = (
|
||||
'Flair',
|
||||
'get_flair',
|
||||
'modmail_removal_notification',
|
||||
'parse_cmd_line_args',
|
||||
'submission_is_older',
|
||||
'string_to_dt',
|
||||
)
|
||||
|
||||
|
||||
class Flair(Enum):
|
||||
SOLVED = 'Solved'
|
||||
ABANDONED = 'Abandoned'
|
||||
UKNOWN = 'Uknown'
|
||||
|
||||
|
||||
def get_flair(flair: str) -> Flair:
|
||||
try:
|
||||
return Flair(flair)
|
||||
except ValueError:
|
||||
return Flair('Uknown')
|
||||
|
||||
|
||||
def modmail_removal_notification(submission: Row, method: str) -> str:
|
||||
return f"""A post has been removed
|
||||
|
||||
OP: `{submission.username}`
|
||||
|
||||
Title: {submission.title}
|
||||
|
||||
Post ID: https://old.reddit.com/comments/{submission.post_id}
|
||||
|
||||
Date created: {submission.record_created}
|
||||
|
||||
Date found: {submission.record_edited}
|
||||
|
||||
Ban Template;
|
||||
|
||||
[Deleted post](https://reddit.com/comments/{submission.post_id}).
|
||||
|
||||
Deleting an answered post, without marking it solved, is against our rules.
|
||||
|
||||
You can read [our rules](https://reddit.com/r/MinecraftHelp/wiki/rules) to see if you're eligible to appeal this ban."""
|
||||
|
||||
|
||||
def parse_cmd_line_args(args: List[str], logger: Logger, config_file: Path, posts: Posts) -> bool:
|
||||
help_msg = """Command line help prompt
|
||||
Command: help
|
||||
Args: []
|
||||
Decription: Prints the help prompt
|
||||
|
||||
Command: reset_config
|
||||
Args: []
|
||||
Decription: Reset the bot credentials
|
||||
|
||||
Command: reset_db
|
||||
Args: []
|
||||
Decription: Reset the database
|
||||
"""
|
||||
if len(args) > 1:
|
||||
if args[1] == 'help':
|
||||
logger.info(help_msg)
|
||||
elif args[1] == 'reset_config':
|
||||
try:
|
||||
os.remove(config_file)
|
||||
except FileNotFoundError:
|
||||
logger.error("No configuration file found")
|
||||
elif args[1] == 'reset_db':
|
||||
try:
|
||||
os.remove(posts.path)
|
||||
except FileNotFoundError:
|
||||
logger.error("No database found")
|
||||
else:
|
||||
logger.info(help_msg)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def submission_is_older(submission_date: dt.date, max_days: int) -> bool:
|
||||
current_date = dt.datetime.now().date()
|
||||
time_difference = current_date - submission_date
|
||||
if time_difference.days > max_days:
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def string_to_dt(date_string: str) -> dt.datetime:
|
||||
return dt.datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S.%f')
|
||||
13
Bot/utils/constants.py
Normal file
13
Bot/utils/constants.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
__all__ = (
|
||||
'BASE_DIR',
|
||||
'BOT_NAME',
|
||||
'MSG_AWAIT_THRESHOLD',
|
||||
)
|
||||
|
||||
|
||||
BOT_NAME = 'CraftSleuthBot'
|
||||
BASE_DIR = Path(__file__).parent.parent.parent
|
||||
MSG_AWAIT_THRESHOLD = 5
|
||||
48
Bot/utils/tests.py
Normal file
48
Bot/utils/tests.py
Normal file
@@ -0,0 +1,48 @@
|
||||
import unittest
|
||||
import datetime as dt
|
||||
from .actions import (
|
||||
Flair,
|
||||
get_flair,
|
||||
string_to_dt,
|
||||
submission_is_older,
|
||||
)
|
||||
|
||||
|
||||
class TestActions(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
return super().setUp()
|
||||
|
||||
def tearDown(self) -> None:
|
||||
return super().tearDown()
|
||||
|
||||
def test_get_flair(self) -> None:
|
||||
solved = get_flair("Solved")
|
||||
self.assertEqual(solved, Flair.SOLVED)
|
||||
abandoned = get_flair('Abandoned')
|
||||
self.assertEqual(abandoned, Flair.ABANDONED)
|
||||
uknown = get_flair('Uknown')
|
||||
self.assertEqual(uknown, Flair.UKNOWN)
|
||||
uknown = get_flair('fsdafsd')
|
||||
self.assertEqual(uknown, Flair.UKNOWN)
|
||||
|
||||
def test_string_to_dt(self) -> None:
|
||||
datetime = dt.datetime.now()
|
||||
string_dt = str(datetime)
|
||||
back_to_dt = string_to_dt(string_dt)
|
||||
self.assertEqual(datetime, back_to_dt)
|
||||
|
||||
def test_submission_is_older(self) -> None:
|
||||
max_days = 7
|
||||
today = dt.datetime.now()
|
||||
|
||||
post_made = today - dt.timedelta(days=3)
|
||||
result = submission_is_older(post_made.date(), max_days)
|
||||
self.assertFalse(result)
|
||||
|
||||
post_made = today - dt.timedelta(days=max_days)
|
||||
result = submission_is_older(post_made.date(), max_days)
|
||||
self.assertFalse(result)
|
||||
|
||||
post_made = today - dt.timedelta(days=(max_days + 1))
|
||||
result = submission_is_older(post_made.date(), max_days)
|
||||
self.assertTrue(result)
|
||||
Reference in New Issue
Block a user