Add Discord like Telegram Integrations

This commit is contained in:
Prograstinator
2022-02-27 00:39:15 +01:00
parent 8964fb9dd7
commit 8e264a4658
6 changed files with 82 additions and 5 deletions

View File

@@ -189,6 +189,7 @@ from colorama import Fore
from TwitchChannelPointsMiner import TwitchChannelPointsMiner
from TwitchChannelPointsMiner.logger import LoggerSettings, ColorPalette
from TwitchChannelPointsMiner.classes.Telegram import Telegram
from TwitchChannelPointsMiner.classes.DiscordWebhook import Discord
from TwitchChannelPointsMiner.classes.Settings import Priority, Events, FollowersOrder
from TwitchChannelPointsMiner.classes.entities.Bet import Strategy, BetSettings, Condition, OutcomeKeys, FilterCondition, DelayMode
from TwitchChannelPointsMiner.classes.entities.Streamer import Streamer, StreamerSettings
@@ -219,7 +220,12 @@ twitch_miner = TwitchChannelPointsMiner(
token="123456789:shfuihreuifheuifhiu34578347", # Telegram API token @BotFather
events=[Events.STREAMER_ONLINE, Events.STREAMER_OFFLINE, "BET_LOSE"], # Only these events will be sent to the chat
disable_notification=True, # Revoke the notification (sound/vibration)
)
),
discord=Discord(
discord_webhook_api="https://discord.com/api/webhooks/0123456789/0a1B2c3D4e5F6g7H8i9J", #Discord Webhook URL
events=[Events.STREAMER_ONLINE, Events.STREAMER_OFFLINE, Events.BET_LOSE], # Only these events will be sent to the chat
),
),
streamer_settings=StreamerSettings(
make_predictions=True, # If you want to Bet / Make prediction
@@ -434,6 +440,23 @@ Telegram(
)
```
#### Discord
If you want to reive log updates on Discord initiate a new Discord class, else leave omit this parameter or set as None
1. Go to the Server you want to recieve updates
2. Click "Edit Channel"
3. Click "Integrations"
4. Click "Webhooks"
5. Click "New Webhook"
6. Name it if you want
7. Click on "Copy Webhook URL"
```python
Discord(
discord_webhook_api="https://discord.com/api/webhooks/0123456789/0a1B2c3D4e5F6g7H8i9J",
events=[Events.STREAMER_ONLINE, Events.STREAMER_OFFLINE, Events.BET_LOSE],
)
```
#### Events
- `STREAMER_ONLINE`
- `STREAMER_OFFLINE`

View File

@@ -0,0 +1,24 @@
from textwrap import dedent
import requests
from TwitchChannelPointsMiner.classes.Settings import Events
class Discord(object):
__slots__ = ["discord_webhook_api", "events"]
def __init__(self, discord_webhook_api: str, events: list):
self.discord_webhook_api = discord_webhook_api
self.events = [str(e) for e in events]
def send(self, message: str, event: Events) -> None:
if str(event) in self.events:
requests.post(
url=self.discord_webhook_api,
data={
"content": dedent(message),
"username": "Twitch Channel Points Miner",
"avatar_url": "https://i.imgur.com/X9fEkhT.png",
},
)

View File

@@ -8,9 +8,8 @@ from TwitchChannelPointsMiner.classes.Settings import Events
class Telegram(object):
__slots__ = ["chat_id", "telegram_api", "events", "disable_notification"]
def __init__(
self, chat_id: int, token: str, events: list, disable_notification: bool = False
):
def __init__(self, chat_id: int, token: str, events: list, disable_notification: bool = False):
self.chat_id = chat_id
self.telegram_api = f"https://api.telegram.org/bot{token}/sendMessage"
self.events = [str(e) for e in events]

View File

@@ -378,6 +378,7 @@ class Twitch(object):
extra={
"event": Events.DROP_STATUS,
"skip_telegram": True,
"skip_discord": True,
},
)
@@ -387,6 +388,12 @@ class Twitch(object):
Events.DROP_STATUS,
)
if Settings.logger.discord is not None:
Settings.logger.discord.send(
"\n".join(drop_messages),
Events.DROP_STATUS,
)
except requests.exceptions.ConnectionError as e:
logger.error(f"Error while trying to send minute watched: {e}")
self.__check_connection_handler(chunk_size)

View File

@@ -10,6 +10,7 @@ from colorama import Fore, init
from TwitchChannelPointsMiner.classes.Settings import Events
from TwitchChannelPointsMiner.classes.Telegram import Telegram
from TwitchChannelPointsMiner.classes.DiscordWebhook import Discord
from TwitchChannelPointsMiner.utils import remove_emoji
@@ -66,6 +67,7 @@ class LoggerSettings:
"color_palette",
"auto_clear",
"telegram",
"discord",
]
def __init__(
@@ -79,6 +81,7 @@ class LoggerSettings:
color_palette: ColorPalette = ColorPalette(),
auto_clear: bool = True,
telegram: Telegram or None = None,
discord: Discord or None = None,
):
self.save = save
self.less = less
@@ -89,6 +92,7 @@ class LoggerSettings:
self.color_palette = color_palette
self.auto_clear = auto_clear
self.telegram = telegram
self.discord = discord
class GlobalFormatter(logging.Formatter):
@@ -133,6 +137,20 @@ class GlobalFormatter(logging.Formatter):
f"{self.settings.color_palette.get(record.event)}{record.msg}"
)
if hasattr(record, "event"):
skip_discord = (
False
if hasattr(record, "skip_discord") is False
else True
)
if self.settings.discord is not None and skip_discord is False:
self.settings.discord.send(record.msg, record.event)
if self.settings.colored is True:
record.msg = (
f"{self.settings.color_palette.get(record.event)}{record.msg}"
)
return super().format(record)

View File

@@ -5,6 +5,7 @@ from colorama import Fore
from TwitchChannelPointsMiner import TwitchChannelPointsMiner
from TwitchChannelPointsMiner.logger import LoggerSettings, ColorPalette
from TwitchChannelPointsMiner.classes.Telegram import Telegram
from TwitchChannelPointsMiner.classes.DiscordWebhook import Discord
from TwitchChannelPointsMiner.classes.Settings import Priority, Events, FollowersOrder
from TwitchChannelPointsMiner.classes.entities.Bet import Strategy, BetSettings, Condition, OutcomeKeys, FilterCondition, DelayMode
from TwitchChannelPointsMiner.classes.entities.Streamer import Streamer, StreamerSettings
@@ -35,7 +36,12 @@ twitch_miner = TwitchChannelPointsMiner(
token="123456789:shfuihreuifheuifhiu34578347", # Telegram API token @BotFather
events=[Events.STREAMER_ONLINE, Events.STREAMER_OFFLINE, "BET_LOSE"], # Only these events will be sent to the chat
disable_notification=True, # Revoke the notification (sound/vibration)
)
),
discord=Discord(
discord_webhook_api="https://discord.com/api/webhooks/0123456789/0a1B2c3D4e5F6g7H8i9J", #Discord Webhook URL
events=[Events.STREAMER_ONLINE, Events.STREAMER_OFFLINE, Events.BET_LOSE], # Only these events will be sent to the chat
),
),
streamer_settings=StreamerSettings(
make_predictions=True, # If you want to Bet / Make prediction