This repository has been archived by the owner on Oct 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
148 lines (118 loc) · 4.04 KB
/
setup.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
import logging
import asyncpg
import config
def logging_setup():
# Discord
discord_logger = logging.getLogger("discord")
discord_logger.setLevel(logging.DEBUG)
discord_file_handler = logging.FileHandler(
filename="discord.log",
encoding="utf-8",
mode="w"
)
discord_console_handler = logging.StreamHandler()
discord_console_handler.setLevel(logging.WARNING)
discord_formatter = logging.Formatter(
"%(asctime)s - %(levelname)s - %(name)s: %(message)s"
)
discord_file_handler.setFormatter(discord_formatter)
discord_console_handler.setFormatter(discord_formatter)
discord_logger.addHandler(discord_file_handler)
discord_logger.addHandler(discord_console_handler)
# Reddit (praw module)
reddit_file_handler = logging.FileHandler(
filename="reddit.log",
encoding="utf-8",
mode="w"
)
reddit_console_handler = logging.StreamHandler()
reddit_console_handler.setLevel(logging.WARNING)
praw_logger = logging.getLogger("praw")
praw_logger.setLevel(logging.DEBUG)
praw_logger.addHandler(reddit_file_handler)
praw_logger.addHandler(reddit_console_handler)
prawcore_logger = logging.getLogger("prawcore")
prawcore_logger.setLevel(logging.DEBUG)
prawcore_logger.addHandler(reddit_file_handler)
prawcore_logger.addHandler(reddit_console_handler)
def check_environment():
global on_heroku
if "DYNO" in os.environ:
on_heroku = True
else:
on_heroku = False
def handle_constants():
if not on_heroku:
choice = input(
"Have any constants changed or NOT been set? (y/n) - "
).lower().strip(" ")
if choice in ("y", "yes"):
token_input = input(
"Input Discord bot token: "
).strip(" ")
client_id_input = input(
"Input Reddit client ID: "
).strip(" ")
client_secret_input = input(
"Input Reddit client secret: "
).strip(" ")
username_input = input(
"Input Reddit username: "
).strip(" ")
password_input = input(
"Input Reddit password: "
).strip(" ")
user_agent_input = input(
"Input Reddit user agent: "
).strip(" ")
# creates a file called ".env" and stores the constants
with open(".env", "w") as file:
file.write(
f"D_TOKEN={token_input}\n"
f"R_CLIENT_ID={client_id_input}\n"
f"R_CLIENT_SECRET={client_secret_input}\n"
f"R_USERNAME={username_input}\n"
f"R_PASSWORD={password_input}\n"
f"R_USER_AGENT=\"{user_agent_input}\"\n"
)
del token_input
del client_id_input
del client_secret_input
del username_input
del password_input
del user_agent_input
elif choice in ("n", "no"):
pass
else:
print("Invalid input, try again.")
handle_constants()
from dotenv import load_dotenv
load_dotenv()
config.D_TOKEN = os.environ["D_TOKEN"]
config.R_CLIENT_ID = os.environ["R_CLIENT_ID"]
config.R_CLIENT_SECRET = os.environ["R_CLIENT_SECRET"]
config.R_USERNAME = os.environ["R_USERNAME"]
config.R_PASSWORD = os.environ["R_PASSWORD"]
config.R_USER_AGENT = os.environ["R_USER_AGENT"]
if on_heroku:
config.DATABASE_URL = os.environ["DATABASE_URL"]
async def database_setup():
if on_heroku:
config.connection = await asyncpg.connect(
config.DATABASE_URL,
ssl="require"
)
else:
# manually input arguments
config.connection = await asyncpg.connect(
user=None,
password=None,
host=None,
port=None,
database=None
)
logging_setup()
on_heroku = None
check_environment()
handle_constants()