-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedded_messages.py
156 lines (123 loc) · 4.94 KB
/
embedded_messages.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
import discord
import datetime
import config
from fmp_api import Query
from player import Player
#-------------------------------------------------------------------------------------
# EMBEDED MESSAGE CREATOR
#-------------------------------------------------------------------------------------
def embedded_message(
status: str = 'info',
author: str = None,
author_icon: str = config.AUTHOR_ICON,
thumbnail: str = None,
title: str = None,
desc: str = None,
fields = None,
image: str = None,
footer_icon: str = config.PENDING_ICON,
footer_text: str = 'Status', # this default doesn't really actually make any sense
timestamp: datetime = datetime.datetime.now(),
colour = 0x11BB11):
if len(desc) > 3000:
desc = desc[:3000]
embed = discord.Embed(
title = title,
description = desc,
colour = colour,
timestamp = timestamp
)
if author:
embed.set_author(name=author, icon_url=author_icon)
if thumbnail:
embed.set_thumbnail(url=thumbnail)
if fields:
embed.add_field(name=fields[0][0], value=fields[0][1], inline=False)
for i in range(1, len(fields)):
embed.add_field(name=fields[i][0], value=fields[i][1], inline=True)
if image:
embed.set_image(url=image)
# footer_icon
match status:
case 'info':
footer_icon = config.INFO_ICON
case 'pending':
footer_icon = config.PENDING_ICON
case 'filled':
footer_icon = config.FILLED_ICON
case 'cancelled':
footer_icon = config.CANCELLED_ICON
embed.set_footer(text=footer_text, icon_url=footer_icon)
return embed
#-------------------------------------------------------------------------------------
# BUTTON CLASS
#-------------------------------------------------------------------------------------
class ConfirmButtons(discord.ui.View):
def __init__(self, symbol, quantity, buying, ctx, player: Player):
super().__init__()
self.symbol = symbol
self.quantity = quantity
self.query = Query(self.symbol)
self.quantity = quantity
self.player = player
self.buying = buying
self.ctx = ctx
self.button1 = discord.ui.Button(label="Confirm Trade", style=discord.ButtonStyle.success, emoji='✔')
self.button2 = discord.ui.Button(label="Cancel Trade", style=discord.ButtonStyle.danger, emoji='✖')
self.button1.callback = self.button1_callback
self.button2.callback = self.button2_callback
self.add_item(self.button1)
self.add_item(self.button2)
## CONFIRM BUTTON
async def button1_callback(self, interaction):
if interaction.user == self.ctx.author:
if self.buying == True:
valid_transaction = self.player.buy_asset(self.query, self.quantity) # add in the fee logic
else:
valid_transaction = self.player.sell_asset(self.query, self.quantity) # add in teh fee logic after
if valid_transaction == True:
await self.on_click(interaction, True)
else:
await interaction.response.edit_message(embed=discord.Embed(
title="Couldn't Process Transaction",
description=valid_transaction,
colour=0xB20D30
), view=None)
## CANCEL BUTTON
async def button2_callback(self, interaction):
if interaction.user == self.ctx.author:
await self.on_click(interaction, False)
async def on_click(self, interaction, confirmed):
match confirmed:
case True:
theme_color = 0x11BB11
foot_text = "Order Status: Filled"
case False:
theme_color = 0xB20D30
foot_text = "Order Status: Cancelled"
match self.query.exchange:
case "CRYPTO":
fee = self.query.price * self.quantity * 0.0015
thumbnail_url = config.CRYPTO_ICON
case _:
fee = 0.00
thumbnail_url = self.query.image
fields_list = [
["Order Type", "Market Buy"],
["Asset Price", f"${self.query.price:.2f}"],
["Quantity", self.quantity],
["Order Subtotal", f"${(self.quantity * self.query.price):.2f}"],
["Fees", f"${(fee):.2f}"],
["Order Total", f"${(self.quantity * self.query.price + fee):.2f}"],
[chr(173), chr(173)] # change this to balance remaining
]
await interaction.response.edit_message(embed=embedded_message(
status='cancelled',
author=self.query.exchange,
title=self.symbol,
thumbnail=thumbnail_url,
fields=fields_list,
desc=self.query.name,
footer_text=foot_text,
colour=theme_color
), view=None)