From 8e264a465814bde398776184ce02ce0e64a9c8a9 Mon Sep 17 00:00:00 2001 From: Prograstinator <99142422+prograstinator@users.noreply.github.com> Date: Sun, 27 Feb 2022 00:39:15 +0100 Subject: [PATCH] Add Discord like Telegram Integrations --- README.md | 25 ++++++++++++++++++- .../classes/DiscordWebhook.py | 24 ++++++++++++++++++ TwitchChannelPointsMiner/classes/Telegram.py | 5 ++-- TwitchChannelPointsMiner/classes/Twitch.py | 7 ++++++ TwitchChannelPointsMiner/logger.py | 18 +++++++++++++ example.py | 8 +++++- 6 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 TwitchChannelPointsMiner/classes/DiscordWebhook.py diff --git a/README.md b/README.md index f5644ac..0a42f70 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/TwitchChannelPointsMiner/classes/DiscordWebhook.py b/TwitchChannelPointsMiner/classes/DiscordWebhook.py new file mode 100644 index 0000000..61582ce --- /dev/null +++ b/TwitchChannelPointsMiner/classes/DiscordWebhook.py @@ -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", + }, + ) diff --git a/TwitchChannelPointsMiner/classes/Telegram.py b/TwitchChannelPointsMiner/classes/Telegram.py index c6d9055..6b75f15 100644 --- a/TwitchChannelPointsMiner/classes/Telegram.py +++ b/TwitchChannelPointsMiner/classes/Telegram.py @@ -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] diff --git a/TwitchChannelPointsMiner/classes/Twitch.py b/TwitchChannelPointsMiner/classes/Twitch.py index c27d883..8b831d1 100644 --- a/TwitchChannelPointsMiner/classes/Twitch.py +++ b/TwitchChannelPointsMiner/classes/Twitch.py @@ -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) diff --git a/TwitchChannelPointsMiner/logger.py b/TwitchChannelPointsMiner/logger.py index ecb730d..e756793 100644 --- a/TwitchChannelPointsMiner/logger.py +++ b/TwitchChannelPointsMiner/logger.py @@ -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) diff --git a/example.py b/example.py index feaa3a3..4a1f4d5 100644 --- a/example.py +++ b/example.py @@ -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