-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
131 lines (113 loc) · 4.2 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
import os
import re
from typing import Annotated
from interactions import (listen, slash_command, Activity, ActivityType, OptionType, SlashContext, SlashCommandChoice,
SlashCommandOption, Status, Embed, RoleColors, EmbedField, Timestamp)
from crypto import *
from crypto.base import CoinSymbol
from helpers.converters import LowerConverter
from helpers.shared import bot
from helpers.tasks import monitor_task
coins = {
CoinSymbol.BTC: Bitcoin(),
CoinSymbol.LTC: Litecoin(),
CoinSymbol.ETH: Ethereum()
}
@listen()
async def on_ready():
"""
Called when the bot is ready to start receiving events.
"""
print(f"Established connection with gateway! Logged in as {bot.user}.")
activity = Activity(type=ActivityType.WATCHING, name="your transactions")
await bot.change_presence(Status.IDLE, activity)
monitor_task.start()
@slash_command(
name="track",
description="Starts tracking a crypto transaction.",
options=[
SlashCommandOption(
type=OptionType.STRING,
required=True,
name="coin",
description="The cryptocurrency coin used.",
choices=[
SlashCommandChoice(name=f"{coin.name} ({coin.symbol.name})", value=coin.symbol)
for coin in coins.values()
]
),
SlashCommandOption(
type=OptionType.STRING,
required=True,
name="txid",
description="The ID of the transaction you want to track."
),
SlashCommandOption(
type=OptionType.INTEGER,
required=False,
name="confirmations",
description="The number of confirmations to notify you after.",
min_value=1
)
]
)
async def track_command(ctx: SlashContext, coin: CoinSymbol, txid: Annotated[str, LowerConverter],
confirmations=1):
"""
Called when a user uses the /track command.
:param ctx: The application command interaction context.
:param coin: The symbol of the cryptocurrency coin used.
:param txid: The ID of the transaction to track.
:param confirmations: The number of confirmations to notify after.
"""
crypto = coins.get(coin)
if confirmations > crypto.max_confirmations:
return await ctx.send(f"The maximum number of confirmations for "
f"**{crypto.name}** is **{crypto.max_confirmations}**.")
# If the user provided a link, attempt to extract the TXID from it
if txid.startswith("http"):
matches = re.findall(crypto.txid_pattern, txid)
if matches:
txid = matches[0]
# Defer the response to avoid timing out
await ctx.defer()
await crypto.track(ctx, txid, confirmations)
@slash_command(name="prices", description="Displays the prices of top cryptocurrencies.")
async def prices_command(ctx: SlashContext):
"""
Called when a user uses the /prices command.
:param ctx: The application command interaction context.
"""
await ctx.defer()
embed = Embed(title=":coin: Cryptocurrency Prices", color=RoleColors.YELLOW, timestamp=Timestamp.now())
embed.add_fields(*[
EmbedField(
name=f"{coin.name} ({coin.symbol.name})",
value=f"{coin.emoji} ${coin.get_usd_rate():,.2f} USD",
inline=True
)
for coin in coins.values()
])
await ctx.send(embed=embed)
@slash_command(name="help", description="Displays information about the bot.")
async def help_command(ctx: SlashContext):
"""
Called when a user uses the /help command.
:param ctx: The application command interaction context.
"""
embed = Embed(
title=":coin: CryptoTracker Help",
description="<:github:1133138264270831616> [This bot is open source!](https://github.com/aelew/cryptotracker)",
color=RoleColors.BLUE,
timestamp=Timestamp.now()
)
embed.add_field(
name=":tools: Commands",
value="\n".join([
f"`/{command.resolved_name}` • {command.description}"
for command in bot.application_commands
])
)
await ctx.send(embed=embed)
# Start the Discord bot
bot.start(os.getenv("DISCORD_BOT_TOKEN"))