Skip to content

Commit

Permalink
TASK: Make websocket package optional
Browse files Browse the repository at this point in the history
That way it only needs to be imported when actually using the bot functionality
  • Loading branch information
d-Rickyy-b committed Oct 6, 2019
1 parent 7b13d75 commit 1786bf4
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion pastepwn/actions/discordaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import json
import logging
import sys
import websockets
from string import Template

from pastepwn.util import Request, DictWrapper
Expand All @@ -17,11 +16,22 @@ class DiscordAction(BasicAction):
def __init__(self, webhook=None, token=None, channel_id=None, template=None):
super().__init__()
self.logger = logging.getLogger(__name__)
self.bot_available = True

try:
import websockets
except ImportError:
self.logger.warning("Could not import 'websockets' module. So you can only use webhooks for discord.")
self.bot_available = False

self.webhook = webhook
if webhook is None:
if token is None or channel_id is None:
raise ValueError('Invalid arguments: requires either webhook or token+channel_id arguments')

if not self.bot_available:
raise NotImplementedError("You can't use bot functionality without the 'websockets' module. Please import it or use webhooks!")

self.token = token
self.channel_id = channel_id
self.identified = False
Expand Down

2 comments on commit 1786bf4

@Zeroji
Copy link
Contributor

@Zeroji Zeroji commented on 1786bf4 Oct 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is, you actually can use the bot way without websockets if you're reusing bot tokens... It might be useful to move the websocket check into DiscordAction.initialize_gateway

@d-Rickyy-b
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is true I do not want the action to fail after running for 2 weeks, but actually I want it to fail right away so that it can be handled with in the beginning when creating your pastepwn "config".

I will think about this and maybe add only a warning in the __init__ and move the exception into the DiscordAction.initialize_gateway method as you suggested.

Please sign in to comment.