Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Discord action (webhook or bot token) #111

Merged
merged 6 commits into from
Oct 6, 2019
Merged

Add Discord action (webhook or bot token) #111

merged 6 commits into from
Oct 6, 2019

Conversation

Zeroji
Copy link
Contributor

@Zeroji Zeroji commented Oct 6, 2019

This includes commits from #85 which hasn't been merged yet.
Fixes #83 and expands on #85 by enabling users to have a DiscordAction with a custom bot token and channel ID, instead of just a webhook.

Both ways work just fine on my machine, but I haven't tried using fresh tokens, let me know if you encounter authentication issues.

Mxrk and others added 4 commits October 3, 2019 20:39
create discordaction with webhook
Alternative to webhook-based actions, requires a bot token and
channel ID in `__init__` parameters.
@coveralls
Copy link

coveralls commented Oct 6, 2019

Pull Request Test Coverage Report for Build 160

  • 15 of 69 (21.74%) changed or added relevant lines in 2 files are covered.
  • 7 unchanged lines in 2 files lost coverage.
  • Overall coverage increased (+0.6%) to 63.339%

Changes Missing Coverage Covered Lines Changed/Added Lines %
pastepwn/actions/discordaction.py 14 68 20.59%
Files with Coverage Reduction New Missed Lines %
pastepwn/analyzers/ibananalyzer.py 1 96.43%
pastepwn/actions/telegramaction.py 6 36.36%
Totals Coverage Status
Change from base Build 147: 0.6%
Covered Lines: 2094
Relevant Lines: 3306

💛 - Coveralls

Repository owner deleted a comment Oct 6, 2019
Repository owner deleted a comment Oct 6, 2019
@dynomite567
Copy link

Looking at this, it implements a Discord bot way better and efficiently than my PR, #86, with no new dependencies at all. Good stuff!

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')
Copy link
Owner

Choose a reason for hiding this comment

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

👏 very nice

else:
paste_dict = paste.to_dict()
paste_dict["analyzer_name"] = analyzer_name
text = self.template.safe_substitute(DictWrapper(paste_dict))
Copy link
Owner

Choose a reason for hiding this comment

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

I created a much better way on how to use templates in the most recent master branch. But no worries, I will fix this later on :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I belive this is Mxrk's work, inspired from TelegramAction source code, so make the change there too :)

@d-Rickyy-b
Copy link
Owner

Tested webhook from @Mxrk and it works perfectly!

grafik

But sadly I can't manage to get the bot part working. You write that you have not used "fresh" tokens, which means that you had a bot which connected via a library first and then you used that token?

@Zeroji
Copy link
Contributor Author

Zeroji commented Oct 6, 2019

Yes, as someone in the gist you linked yesterday mentioned, some initialization might be required.

I will try to generate some tokens for myself and try again, if I get it to work I will add the gateway ping in DiscordAction.__init__, I think. There might be issues if the message is sent too long after the ping, if you think standard pastepwn usage might fall in that category I will add logic to call it from perform.

@d-Rickyy-b
Copy link
Owner

@Zeroji That would be epic. Maybe also check the status code returned from the post method and do a gateway ping again.
Looking forward to hear back from you.

@Zeroji
Copy link
Contributor Author

Zeroji commented Oct 6, 2019

@d-Rickyy-b I'll have to check status codes because apparently this is a "happens occasionally at random intervals" kind of thing. I can reproduce the issue with fresh tokens, unfortunately I can't establish a Gateway connection without adding websockets. I'll give this a small hour, and drop it if I can't put something together - the alternative will be to run the token through any "proper" discord lib at least once.

EDIT: I don't want to hurt the compat - I believe current is 3.4+?

Uses websockets to open a connection to Discord's Gateway,
then sends the proper identification payload so that further requests
are authorized. This code runs only if sending
messages fails with code 40001.
Repository owner deleted a comment Oct 6, 2019
@Zeroji
Copy link
Contributor Author

Zeroji commented Oct 6, 2019

image
It works! Took me the entire hour I allocated to it, I hope I covered edge cases because I'm going to sleep 😛

I had to add websockets as a dependency unfortunately, but I used version 7.0 (8.0 is for python 3.6+) and tried to make my code compatible with Python 3.4. I haven't been able to build it on my machine (openssl errors...) so you'll have to let me know if something is broken.

The connection to the Gateway is done only when the request fails, and only once per DiscordAction (this might need to change in the future, maybe time-based?) because spamming Identify payloads (1000 per 24h) will reset the bot tokens.

Again, let me know if it works!

@d-Rickyy-b
Copy link
Owner

Very nice!! Thanks for all the effort. ❤️

Sadly pastepwn is a multithreaded application and hence it runs into an error regarding a missing event loop.

RuntimeError: There is no current event loop in thread 'ActionHandler'.
Exception in thread ActionHandler:
Traceback (most recent call last):
  File "C:\Users\Rico\PythonProjects\pastepwn\pastepwn\core\actionhandler.py", line 63, in _start
    action.perform(paste, analyzer.identifier)
  File "C:\Users\Rico\PythonProjects\pastepwn\pastepwn\actions\discordaction.py", line 112, in perform
    self.initialize_gateway()
  File "C:\Users\Rico\PythonProjects\pastepwn\pastepwn\actions\discordaction.py", line 86, in initialize_gateway
    asyncio.get_event_loop().run_until_complete(self._identify(ws_url))
  File "C:\Users\Rico\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py", line 694, in get_event_loop
    return get_event_loop_policy().get_event_loop()
  File "C:\Users\Rico\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py", line 602, in get_event_loop
    % threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'ActionHandler'.

So I just added

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

And it seems to work just fine 🎉 . I am not sure what kind of side effects this brings in.

Thank you so much. Gonna merge this.

@d-Rickyy-b d-Rickyy-b merged commit 7f26f4d into d-Rickyy-b:master Oct 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Discord Action
5 participants