-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
214 lines (172 loc) · 7.24 KB
/
main.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import sys
sys.path.append("./discord.py")
import asyncio
import json
import traceback
from textwrap import indent
from io import StringIO
from datetime import datetime, timezone
import config
import discord # type: ignore
from discord import slash # type: ignore
from discord.http import Route # type: ignore
async def save_data(bot):
while True:
await asyncio.sleep(60)
with open("data.json", "w") as f:
f.write(json.dumps(bot.data, indent=4))
async def apply_wealth_tax(bot):
print("starting wealth tax task...")
while True:
now = int(datetime.now(timezone.utc).timestamp())
if "last_wealth_tax" not in bot.data["banking"] or (now - bot.data["banking"]["last_wealth_tax"]) >= 86400:
print("applying wealth tax, treasury balance is", bot.data["banking"]["organisations"]["Treasury"]["balance"])
tax = bot.data["banking"]["wealth_tax"]
for user in bot.data["banking"]["users"]:
original = bot.data["banking"]["users"][user]
bot.data["banking"]["users"][user] *= (1 - tax)
bot.data["banking"]["users"][user] = round(bot.data["banking"]["users"][user], 2)
bot.data["banking"]["organisations"]["Treasury"]["balance"] += original - bot.data["banking"]["users"][user]
for org in bot.data["banking"]["organisations"]:
original = bot.data["banking"]["organisations"][org]["balance"]
bot.data["banking"]["organisations"][org]["balance"] *= (1 - tax)
bot.data["banking"]["organisations"][org]["balance"] = round(bot.data["banking"]["organisations"][org]["balance"], 2)
bot.data["banking"]["organisations"]["Treasury"]["balance"] += original - bot.data["banking"]["organisations"][org]["balance"]
bot.data["banking"]["last_wealth_tax"] = now
print("finished wealth tax, treasury balance is", bot.data["banking"]["organisations"]["Treasury"]["balance"])
else:
last = bot.data["banking"]["last_wealth_tax"]
print("sleeping for", (last+86400) - now, "seconds")
await asyncio.sleep((last+86400) - now)
class Bot(slash.Bot):
LOG_CHANNEL = config.LOG_CHANNEL
GITHUB_TOKEN = config.GITHUB_TOKEN
def __init__(self, **kwargs) -> None:
with open("data.json") as f:
self.data = json.loads(f.read())
super().__init__(**kwargs)
self._save_task = self.loop.create_task(save_data(self))
self._wealth_tax = self.loop.create_task(apply_wealth_tax(self))
def send_multipart_helper(
self,
interaction,
*,
files=None,
content=None,
tts=False,
embed=None,
embeds=None,
nonce=None,
allowed_mentions=None,
message_reference=None,
stickers=None,
components=None,
ephemeral=False,
):
r = Route(
"POST",
"/interactions/{interaction_id}/{interaction_token}/callback",
interaction_id=interaction.id,
interaction_token=interaction.token,
)
form = []
options = {"tts": tts}
if content:
options["content"] = content
if embed:
options["embeds"] = [embed]
if embeds:
options["embeds"] = embeds
if nonce:
options["nonce"] = nonce
if allowed_mentions:
options["allowed_mentions"] = allowed_mentions
if message_reference:
options["message_reference"] = message_reference
if components:
options["components"] = components
if stickers:
options["sticker_ids"] = stickers
if ephemeral:
options["flags"] = 1 << 6
payload = {"type": 4, "data": options}
form.append({"name": "payload_json", "value": discord.utils._to_json(payload)})
if len(files) == 1:
file = files[0]
form.append(
{
"name": "file",
"value": file.fp,
"filename": file.filename,
"content_type": "application/octet-stream",
}
)
else:
for index, file in enumerate(files):
form.append(
{
"name": f"file{index}",
"value": file.fp,
"filename": file.filename,
"content_type": "application/octet-stream",
}
)
return self.http.request(r, form=form, files=files)
async def on_ready(self):
print(f"logged in as {self.user!s}")
async def close(self):
self._save_task.cancel()
with open("data.json", "w") as f:
f.write(json.dumps(self.data, indent=4))
await super().close()
async def on_message(self, message):
if message.author.bot:
return
if message.channel.id == 916431428693135360:
if not len(message.attachments) == 0:
if not (
message.attachments[0].height is None
or message.attachments[0].height < 10
):
await message.add_reaction("<:upvote:922104991869718548>")
await message.add_reaction("<:downvote:922104870696263680>")
if str(message.author.id) in self.data["followers"]:
for follower in self.data["followers"][str(message.author.id)]:
user = await self.fetch_user(follower)
await user.send(
f"{message.author!s} has sent a new post in <#916431428693135360>! Check it out here {message.jump_url}"
)
if message.author.id == 737928480389333004:
if message.content.startswith("```py"):
env = {
"__builtins__": __builtins__,
"discord": discord,
"message": message,
"author": message.author,
"guild": message.guild,
"channel": message.channel,
"bot": self
}
clean_content = "async def func():\n" + indent(
message.content.strip("```py"), " "
)
try:
exec(clean_content, env)
await env["func"]()
except:
await message.channel.send(
"```py\n" + traceback.format_exc() + "```"
)
async def log(self, message):
channel = self.get_channel(self.LOG_CHANNEL)
await channel.send(message)
bot = Bot()
extensions = ["tocktik", "police", "banking", "fishing"]
for extension in extensions:
bot.load_extension("cogs." + extension)
@bot.slash_command(name="data", description="Sends all of the data RoboLouis has", guild_id=907657508292792342)
async def senddata(interaction):
buffer = StringIO(json.dumps(bot.data, indent=4))
file = discord.File(buffer, filename="data.json")
await bot.send_multipart_helper(interaction, files=[file], ephemeral=True)
bot.run(config.TOKEN)