2022-06-01 15:44:48 +00:00
|
|
|
import logging
|
|
|
|
|
2022-09-18 11:31:52 +00:00
|
|
|
from freqtrade.constants import Config
|
2022-09-09 18:31:30 +00:00
|
|
|
from freqtrade.enums import RPCMessageType
|
2022-06-11 10:02:41 +00:00
|
|
|
from freqtrade.rpc import RPC
|
|
|
|
from freqtrade.rpc.webhook import Webhook
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2022-06-01 15:44:48 +00:00
|
|
|
|
|
|
|
|
2022-06-11 10:02:41 +00:00
|
|
|
class Discord(Webhook):
|
2024-05-12 14:51:11 +00:00
|
|
|
def __init__(self, rpc: "RPC", config: Config):
|
2022-10-07 18:59:49 +00:00
|
|
|
self._config = config
|
2022-06-11 10:02:41 +00:00
|
|
|
self.rpc = rpc
|
2024-05-12 14:51:11 +00:00
|
|
|
self.strategy = config.get("strategy", "")
|
|
|
|
self.timeframe = config.get("timeframe", "")
|
|
|
|
self.bot_name = config.get("bot_name", "")
|
2022-06-01 15:44:48 +00:00
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
self._url = config["discord"]["webhook_url"]
|
|
|
|
self._format = "json"
|
2022-06-11 10:02:41 +00:00
|
|
|
self._retries = 1
|
|
|
|
self._retry_delay = 0.1
|
2024-05-12 14:51:11 +00:00
|
|
|
self._timeout = self._config["discord"].get("timeout", 10)
|
2022-06-01 15:44:48 +00:00
|
|
|
|
2022-06-11 10:02:41 +00:00
|
|
|
def cleanup(self) -> None:
|
2022-06-01 15:44:48 +00:00
|
|
|
"""
|
2022-06-11 10:02:41 +00:00
|
|
|
Cleanup pending module resources.
|
|
|
|
This will do nothing for webhooks, they will simply not be called anymore
|
2022-06-01 15:44:48 +00:00
|
|
|
"""
|
2022-06-11 10:02:41 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def send_msg(self, msg) -> None:
|
2024-05-12 14:51:11 +00:00
|
|
|
if fields := self._config["discord"].get(msg["type"].value):
|
2022-10-01 07:35:21 +00:00
|
|
|
logger.info(f"Sending discord message: {msg}")
|
2022-06-11 15:30:56 +00:00
|
|
|
|
2024-05-12 14:51:11 +00:00
|
|
|
msg["strategy"] = self.strategy
|
|
|
|
msg["timeframe"] = self.timeframe
|
|
|
|
msg["bot_name"] = self.bot_name
|
2022-06-11 15:30:56 +00:00
|
|
|
color = 0x0000FF
|
2024-05-12 14:51:11 +00:00
|
|
|
if msg["type"] in (RPCMessageType.EXIT, RPCMessageType.EXIT_FILL):
|
|
|
|
profit_ratio = msg.get("profit_ratio")
|
|
|
|
color = 0x00FF00 if profit_ratio > 0 else 0xFF0000
|
|
|
|
title = msg["type"].value
|
|
|
|
if "pair" in msg:
|
2022-10-07 18:59:49 +00:00
|
|
|
title = f"Trade: {msg['pair']} {msg['type'].value}"
|
2024-05-12 14:51:11 +00:00
|
|
|
embeds = [
|
|
|
|
{
|
|
|
|
"title": title,
|
|
|
|
"color": color,
|
|
|
|
"fields": [],
|
|
|
|
}
|
|
|
|
]
|
2022-06-11 15:30:56 +00:00
|
|
|
for f in fields:
|
|
|
|
for k, v in f.items():
|
|
|
|
v = v.format(**msg)
|
2024-05-12 14:51:11 +00:00
|
|
|
embeds[0]["fields"].append({"name": k, "value": v, "inline": True})
|
2022-06-01 15:44:48 +00:00
|
|
|
|
|
|
|
# Send the message to discord channel
|
2024-05-12 14:51:11 +00:00
|
|
|
payload = {"embeds": embeds}
|
2022-06-11 10:02:41 +00:00
|
|
|
self._send_msg(payload)
|