-
Notifications
You must be signed in to change notification settings - Fork 1
/
testbot.py
81 lines (61 loc) · 2.24 KB
/
testbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import os
import sys
import traceback
from discord import Embed, Intents
from discord.ext.commands import Bot, when_mentioned_or
from discord.ext.commands.context import Context
from dotenv import load_dotenv
from datetimeparser.evaluator import Evaluator
from datetimeparser.parser import Parser
from datetimeparser import parse as parse_date
load_dotenv()
client = Bot(
command_prefix=when_mentioned_or("+"),
case_insensitive=True,
intents=Intents.all()
)
def get_latest_stacktrace():
# Helper function for retrieving the latest stacktrace
exc = sys.exc_info()[0]
stack = traceback.extract_stack()[:-1]
if exc is not None:
del stack[-1]
trc = 'Traceback (most recent call last):\n'
stack_str = trc + ''.join(traceback.format_list(stack))
if exc is not None:
stack_str += ' ' + traceback.format_exc().lstrip(trc)
return stack_str
@client.event
async def on_ready():
print("Ready")
@client.command(
name="parse",
aliases=["p"],
description="This command can be used to translate a string into a datetime-object",
brief="Translate a string into a datetime-object"
)
async def parse(ctx: Context, *, datetime_string: str):
color = 0x00FF00
result = None
try:
res = Parser(datetime_string).parse()
parser_res = res
if res is None:
result = "```mkd\nNone (Parser returned None)```"
else:
res = Evaluator(res).evaluate()
if res is None:
result = f"```mkd\nNone (Evaluator returned None)```\nParser output: ```mkd\n{parser_res}```"
else:
result = f"```python\nfrom datetimeparser import parse\n\nparse(\"{datetime_string if len(datetime_string) < 3000 else datetime_string[:3000]}\")```\n```mkd\n\n# {parse_date(datetime_string)}```"
except: # noqa
result = f"```mkd\n{get_latest_stacktrace()}```"
color = 0xFF0000
finally:
embed = Embed(
title="Parsed datetime object",
color=color,
description=f"Create an [Issue](https://github.com/aridevelopment-de/datetimeparser/issues/new/choose) if a constant is missing\nㅤ\n{result}"
)
await ctx.reply(embed=embed)
client.run(os.environ["TOKEN"])