This repository has been archived by the owner on Oct 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
250 lines (202 loc) · 6.75 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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
from datetime import datetime
from typing import Optional
from uuid import UUID
import discord
import requests
from discord import app_commands
from pydantic import BaseModel, BaseSettings
class Config(BaseSettings):
class Config:
env_file = ".env"
env_file_encoding = "utf-8"
discord_token: str
hyperion_endpoint: str
hyperion_integration_token: str
testing_guild_id: str = "497544520695808000"
class Currency(BaseModel):
id: UUID
name: str
singular_form: str
plural_form: str
shortcode: str
owner_id: str
date_created: datetime
date_modified: datetime
class Transaction(BaseModel):
id: UUID
amount: int
state: str
state_reason: Optional[str]
description: Optional[str]
source_currency_id: UUID
source_account_id: str
dest_currency_id: UUID
dest_account_id: str
integration_id: UUID
date_created: datetime
date_modified: datetime
class Account(BaseModel):
id: str
currency_id: UUID
balance: int
effective_balance: int
date_created: datetime
date_modified: datetime
system_account: bool
display_name: Optional[str]
config = Config()
guild = discord.Object(config.testing_guild_id)
intents = discord.Intents.default()
client = discord.Client(intents=intents)
tree = app_commands.CommandTree(client)
hyperion_session = requests.session()
hyperion_session.headers.update(
{
"Authorization": f"Bearer {config.hyperion_integration_token}",
"User-Agent": "Hyperion Bot",
}
)
hyperion_session.post(
f"{config.hyperion_endpoint}/api/v1/accounts",
json={
"id": "recurring-payout",
"display_name": "Recurring Payout",
"system_account": True,
},
)
currency_details_resp = hyperion_session.get(
f"{config.hyperion_endpoint}/api/v1/integration/currency"
)
currency = Currency(**currency_details_resp.json())
def get_last_payout_time(user_id: int) -> Optional[datetime]:
transactions_resp = hyperion_session.get(
f"{config.hyperion_endpoint}/api/v1/transactions",
)
transactions = [
Transaction(**transaction) for transaction in transactions_resp.json()
]
user_payouts = [
transaction
for transaction in transactions
if transaction.source_account_id == "recurring-payout"
and transaction.dest_account_id == str(user_id)
]
if len(user_payouts) != 0:
return user_payouts[-1].date_created
return None
@tree.command(guild=guild)
async def openaccount(interaction: discord.Interaction):
"""Open a new Hyperion account."""
resp = hyperion_session.post(
f"{config.hyperion_endpoint}/api/v1/accounts",
json={
"id": interaction.user.id,
"display_name": interaction.user.name,
"system_account": False,
},
)
if resp.status_code == 409:
await interaction.response.send_message(
"You already have an account.", ephemeral=True
)
return
await interaction.response.send_message("Account opened.")
@tree.command(guild=guild)
async def daily(interaction: discord.Interaction):
"""Get your daily payout."""
last_payout_time = get_last_payout_time(interaction.user.id)
if (
last_payout_time is not None
and (datetime.utcnow() - last_payout_time).total_seconds() < 60 * 60 * 24
):
await interaction.response.send_message(
"You have already received your daily payout.", ephemeral=True
)
return
transaction_resp = hyperion_session.post(
f"{config.hyperion_endpoint}/api/v1/transactions",
json={
"source_account_id": "recurring-payout",
"dest_account_id": interaction.user.id,
"amount": 10,
"description": "Daily payout",
},
)
if transaction_resp.status_code != 200:
await interaction.response.send_message(
f"Error creating transaction.\n```\n{transaction_resp.json()['detail']}\n```",
)
return
transaction = Transaction(**transaction_resp.json())
execution_resp = hyperion_session.post(
f"{config.hyperion_endpoint}/api/v1/transactions/{transaction.id}/execute",
)
if execution_resp.status_code != 200:
await interaction.response.send_message(
f"Error executing transaction.\n```\n{execution_resp.json()['detail']}\n```",
)
return
await interaction.response.send_message(
f"You have received a daily payout of 10 {currency.plural_form}."
)
@tree.command(guild=guild)
async def balance(interaction: discord.Interaction, user: Optional[discord.User]):
"""Get balance of a user."""
if user is None:
user = interaction.user
resp = hyperion_session.get(f"{config.hyperion_endpoint}/api/v1/accounts/{user.id}")
if resp.status_code != 200:
await interaction.response.send_message(
f"Error getting account.\n```\n{resp.json()['detail']}\n```",
)
return
prefix = "You have" if user == interaction.user else f"{user.name} has"
account = Account(**resp.json())
await interaction.response.send_message(
f"{prefix} {account.balance} {currency.plural_form}."
)
@tree.command(guild=guild)
async def send(
interaction: discord.Interaction,
user: discord.User,
amount: int,
description: Optional[str],
):
"""Send currency to another user."""
resp = hyperion_session.post(
f"{config.hyperion_endpoint}/api/v1/transactions",
json={
"source_account_id": interaction.user.id,
"dest_account_id": user.id,
"amount": amount,
"description": description,
},
)
if resp.status_code != 200:
await interaction.response.send_message(
f"Error creating transaction.\n```\n{resp.json()['detail']}\n```",
)
return
transaction = Transaction(**resp.json())
execution_resp = hyperion_session.post(
f"{config.hyperion_endpoint}/api/v1/transactions/{transaction.id}/execute",
)
if execution_resp.status_code != 200:
await interaction.response.send_message(
f"Error executing transaction.\n```\n{execution_resp.json()['detail']}\n```",
)
return
new_transaction = Transaction(**execution_resp.json())
if new_transaction.state != "complete":
await interaction.response.send_message(
f"Transaction {new_transaction.state} - {new_transaction.state_reason}",
)
return
await interaction.response.send_message(
f"You have sent {amount} {currency.plural_form} to {user.name}."
)
@client.event
async def on_ready():
print("Bot ready, syncing commands")
await tree.sync(guild=guild)
client.run(config.discord_token)