stable/freqtrade/rpc/discord.py

60 lines
1.9 KiB
Python
Raw Normal View History

2022-06-01 15:44:48 +00:00
import logging
2022-06-11 10:02:41 +00:00
from typing import Any, Dict
2022-06-01 15:44:48 +00:00
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):
2022-06-01 15:44:48 +00:00
def __init__(self, rpc: 'RPC', config: Dict[str, Any]):
2022-06-11 10:02:41 +00:00
# super().__init__(rpc, config)
self.rpc = rpc
self.config = config
2022-06-01 15:44:48 +00:00
self.strategy = config.get('strategy', '')
self.timeframe = config.get('timeframe', '')
2022-06-11 10:02:41 +00:00
self._url = self.config['discord']['webhook_url']
self._format = 'json'
self._retries = 1
self._retry_delay = 0.1
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:
logger.info(f"Sending discord message: {msg}")
2022-06-01 15:44:48 +00:00
if msg['type'].value in self.config['discord']:
msg['strategy'] = self.strategy
msg['timeframe'] = self.timeframe
fields = self.config['discord'].get(msg['type'].value)
color = 0x0000FF
if msg['type'] in (RPCMessageType.EXIT, RPCMessageType.EXIT_FILL):
profit_ratio = msg.get('profit_ratio')
color = (0x00FF00 if profit_ratio > 0 else 0xFF0000)
2022-06-01 15:44:48 +00:00
embeds = [{
'title': f"Trade: {msg['pair']} {msg['type'].value}",
'color': color,
'fields': [],
2022-06-01 15:44:48 +00:00
}]
for f in fields:
for k, v in f.items():
v = v.format(**msg)
embeds[0]['fields'].append( # type: ignore
{'name': k, 'value': v, 'inline': True})
2022-06-01 15:44:48 +00:00
# Send the message to discord channel
2022-06-11 10:02:41 +00:00
payload = {'embeds': embeds}
self._send_msg(payload)