-
Notifications
You must be signed in to change notification settings - Fork 0
/
axyl_stats_bot.py
165 lines (123 loc) · 4.18 KB
/
axyl_stats_bot.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import os
import hikari
import psycopg2 as pgres
from dotenv import load_dotenv
load_dotenv()
# Required .env vars
BOT_TOKEN = os.getenv("BOT_TOKEN")
COUNTER_CHANNEL = os.getenv("COUNTER_CHANNEL")
REPO_OWNER = os.getenv("REPO_OWNER")
REPO_NAME = os.getenv("REPO_NAME")
DB_NAME = os.getenv("DB_NAME")
# Optional .env vars
INTERVAL = os.getenv("INTERVAL", 60)
PREFIX = os.getenv("BOT_PREFIX", ".stats")
DB_USER = os.getenv("DB_USER", None)
DB_PASS = os.getenv("DB_PASS", None)
DB_HOST = os.getenv("DB_HOST", "127.0.0.1")
DB_PORT = os.getenv("DB_PORT", "5432")
if BOT_TOKEN is None:
raise Exception("No BOT_TOKEN set in the .env file.")
elif DB_NAME is None:
raise Exception("Database name (DB_NAME) not set in the '.env' file.")
elif REPO_OWNER is None:
raise Exception("No REPO_OWNER set in the .env file.")
elif REPO_NAME is None:
raise Exception("No REPO_NAME set in the .env file.")
elif COUNTER_CHANNEL is None:
raise Exception("No COUNTER_CHANNEL set in the .env file.")
repo_full_name = REPO_OWNER + '/' + REPO_NAME
bot = hikari.GatewayBot(token=BOT_TOKEN)
# Initialize the connection to the PostgreSQL db
connection = pgres.connect(database=DB_NAME,
user=DB_USER,
password=DB_PASS,
host=DB_HOST,
port=DB_PORT)
# Initialize the cursor
cursor = connection.cursor()
info_commands = {
"stats":
"""`{repo_name}` stats:
**⬇️ Downloads (Total)**
`{total_downloads}`
**🔥 Downloads (Latest Release)**
`{latest_downloads}`
**⭐ Stars**
`{stars}`
**🌱 Forks**
`{forks}`
**🔭 Watchers**
`{watchers}`""",
"downloads":
"""**⬇️ Downloads (Total)** - `{repo_name}`
`{total_downloads}`
**🔥 Downloads (Latest Release)**
`{latest_downloads}`""",
"stars":
"""**⭐ Stars** - `{repo_name}`
`{stars}`""",
"forks":
"""**🌱 Forks** - `{repo_name}`
`{forks}`""",
"watchers":
"""**🔭 Watchers** - `{repo_name}`
`{watchers}`""",
}
def list_help_commands(commands: dict, prefix: str) -> str:
help_str = "🔎 List of commands:\n"
for command_name in commands:
help_str += f"`{prefix} {command_name}`\n"
# Strip the last new line
help_str = help_str.rstrip("\n")
return help_str
def unknown_command() -> str:
return f"❓ Unknown command. Try `{PREFIX} help` for the list of commands."
def fetch_latest_db_stats(cursor: pgres.extensions.cursor) \
-> tuple[int, int, int, int, int]:
cursor.execute("""SELECT total_downloads, latest_downloads,
stars, watchers, forks
FROM repo_stats
ORDER BY date DESC LIMIT 1;""")
(total_downloads,
latest_downloads,
stars,
watchers,
forks) = cursor.fetchone()
return total_downloads, latest_downloads, stars, watchers, forks
@bot.listen()
async def on_message(event: hikari.GuildMessageCreateEvent) -> None:
if (event.is_bot or not
event.content or not
event.content.startswith(PREFIX)):
return
args = event.content[1:].split()
if len(args) > 1:
(total_downloads,
latest_downloads,
stars,
watchers,
forks) = fetch_latest_db_stats(cursor)
if args[1] in info_commands:
await event.message.respond(info_commands[args[1]]
.format(
repo_name=repo_full_name,
total_downloads=total_downloads,
latest_downloads=latest_downloads,
stars=stars,
watchers=watchers,
forks=forks,
))
elif args[1] == "help":
await event.message.respond(list_help_commands(info_commands,
PREFIX))
else:
await event.message.respond(unknown_command())
else:
await event.message.respond(unknown_command())
def main(debug=False) -> int:
if not debug:
bot.run()
return 0
if __name__ == "__main__":
exit(main())