-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
122 lines (116 loc) · 4.36 KB
/
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
from nationstates_ai import ns_ai_bot
import asyncio
import logging
import json
import aiosqlite
import time
import os
import sys
logging.basicConfig(
filename="logs.log",
filemode="a",
format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s",
datefmt="%H:%M:%S",
level=logging.DEBUG,
)
USER_AGENT = os.environ.get("USER_AGENT", "[email protected] AI Issue Answering")
"""User agents need to include a form of contacting you as per NS API rules."""
try:
HF_API_TOKEN = os.environ["HF_API_TOKEN"]
except KeyError:
print("Input your Huggingface API token into your .env file under the name of HF_API_TOKEN.")
sys.exit(0)
API_URL = os.environ.get("API_URL", "https://api-inference.huggingface.co/models/distilbert-base-cased-distilled-squad")
try:
NATIONS = json.loads(os.environ["NATIONS"])
except KeyError:
print("Input your Nationstates nations into your .env file under the name of NATIONS. "
"See an example .env file at https://github.com/Bohaska/nationstates_ai/blob/main/.env.")
sys.exit(0)
try:
NATIONSTATES_PASSWORDS = json.loads(os.environ["NATIONSTATES_PASSWORDS"])
except KeyError:
print("Input your Nationstates passwords into your .env file under the name of NATIONSTATES_PASSWORDS. "
"See an example .env file at https://github.com/Bohaska/nationstates_ai/blob/main/.env.")
sys.exit(0)
try:
PROMPTS = json.loads(os.environ["PROMPTS"])
except KeyError:
print("Input the prompts for your nations into your .env file under the name of PROMPTS. "
"See an example .env file at https://github.com/Bohaska/nationstates_ai/blob/main/.env.")
sys.exit(0)
async def run_all_ais(
user_agent, hf_api_token, api_url, ns_passwords, nations, prompts
):
con = await aiosqlite.connect("nationstates_ai.db")
ns_ai_coroutines = []
counter = 0
for index in range(len(ns_passwords)):
cursor = await con.execute("SELECT name FROM sqlite_master WHERE name='next_issue_time'")
table = await cursor.fetchone()
if table is not None:
cursor = await con.execute(
"SELECT timestamp FROM next_issue_time WHERE nation = ?",
(nations[index],),
)
timestamp = await cursor.fetchone()
if timestamp is not None:
if timestamp[0] > time.time():
ns_ai_coroutines.append(
ns_ai_bot(
nations[index],
ns_passwords[index],
{"Authorization": f"Bearer {hf_api_token}"},
api_url,
prompts[index],
user_agent,
timestamp[0] - time.time() + 10,
)
)
else:
ns_ai_coroutines.append(
ns_ai_bot(
nations[index],
ns_passwords[index],
{"Authorization": f"Bearer {hf_api_token}"},
api_url,
prompts[index],
user_agent,
counter * 10,
)
)
counter += 1
else:
ns_ai_coroutines.append(
ns_ai_bot(
nations[index],
ns_passwords[index],
{"Authorization": f"Bearer {hf_api_token}"},
api_url,
prompts[index],
user_agent,
counter * 10,
)
)
counter += 1
else:
ns_ai_coroutines.append(
ns_ai_bot(
nations[index],
ns_passwords[index],
{"Authorization": f"Bearer {hf_api_token}"},
api_url,
prompts[index],
user_agent,
counter * 10,
)
)
counter += 1
await con.close()
thing = await asyncio.gather(*ns_ai_coroutines)
return thing
asyncio.run(
run_all_ais(
USER_AGENT, HF_API_TOKEN, API_URL, NATIONSTATES_PASSWORDS, NATIONS, PROMPTS
)
)