From 05f254a47e8ca495203962d68f2feff2c067f44e Mon Sep 17 00:00:00 2001 From: Joseph Manley Date: Sun, 13 Sep 2020 05:26:44 +0000 Subject: [PATCH] Initial commit --- .github/workflows/build.yml | 32 +++++++++++++++++ .github/workflows/version.yml | 28 +++++++++++++++ .gitignore | 5 +++ discordnotifier/__init__.py | 58 ++++++++++++++++++++++++++++++ requirements.txt | 5 +++ setup.py | 66 +++++++++++++++++++++++++++++++++++ 6 files changed, 194 insertions(+) create mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/version.yml create mode 100644 discordnotifier/__init__.py create mode 100644 requirements.txt create mode 100644 setup.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..a95408a --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,32 @@ +name: Build & Publish +on: + release: + types: + - created + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - name: Set up Python 3.7 + uses: actions/setup-python@v1 + with: + python-version: 3.7 + - name: Update Version + id: semver + uses: RightBrain-Networks/semver-action@1.0.0 + with: + mode: get + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + - name: build + run: | + python setup.py sdist bdist_wheel + - name: PyPi Publish + uses: pypa/gh-action-pypi-publish@v1.0.0a0 + with: + user: __token__ + password: ${{ secrets.PYPI_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/version.yml b/.github/workflows/version.yml new file mode 100644 index 0000000..bf1d4db --- /dev/null +++ b/.github/workflows/version.yml @@ -0,0 +1,28 @@ +name: Version & Release + +on: + push: + branches: + - master + +jobs: + CheckVersion: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v1 + - name: Run Auto-Semver + id: semver + uses: RightBrain-Networks/semver-action@1.0.0 + - name: Create Release + id: create_release + uses: actions/create-release@v1 + if: steps['semver']['outputs']['RETURN_STATUS'] == '0' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ steps.semver.outputs.SEMVER_NEW_VERSION }} + release_name: ${{ steps.semver.outputs.SEMVER_NEW_VERSION }} + body: Release Version ${{ steps.semver.outputs.SEMVER_NEW_VERSION }} + draft: false + prerelease: false \ No newline at end of file diff --git a/.gitignore b/.gitignore index b6e4761..c966a82 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,8 @@ dmypy.json # Pyre type checker .pyre/ + +aws +.vscode/ +awscliv2.zip +discord_config.ini \ No newline at end of file diff --git a/discordnotifier/__init__.py b/discordnotifier/__init__.py new file mode 100644 index 0000000..bf8eca5 --- /dev/null +++ b/discordnotifier/__init__.py @@ -0,0 +1,58 @@ +import discord, boto3, configparser, os, sys + +version = "0.0.0" + +# Get token at: https://discord.com/developers/applications/ +config = configparser.ConfigParser() +config_path = "discord_config.ini" + +if not os.path.exists(config_path): + with open(config_path, "w") as file_writer: + file_writer.write("""[discord] + token = {PUT TOKEN HERE} + ignore_user = {PUT USERNAME HERE} + [aws] + topic = {PUT TOPIC HERE} + profile = default""".replace(" ","")) + print(f"Please update '{config_path}'!") + sys.exit(0) + +config.read(config_path) +session = boto3.session.Session(profile_name=config["aws"]["profile"]) +bot_token = config["discord"]["token"] +sns = session.resource('sns').Topic(config["aws"]["topic"]) +if sns == None: + print("SNS was configured poorly!") + sys.exit(0) + +class Notifier(discord.Client): + async def on_ready(self): + print(f"Logged in as {self.user}") + + guilds = "" + for guild in self.guilds: + guilds = guilds + ", " + str(guild) + print(f"Watching servers: {guilds[2:]}") + + async def on_message(self, message): + if "do you see me?" in message.content.lower(): + print(f"I see {message.author}") + await message.channel.send(":eye: You have been seen! :eye:") + + if str(message.author) in [str(self.user), "The Genuine Wonder#2859"]: + return + + # Format and print mesage + formatted_message = f"<{message.author}> \"{message.content}\" from #{message.channel} on {message.guild}" + print(formatted_message) + + # Send notification to SNS + sns.publish(Message=formatted_message) + + +def main(): + client = Notifier() + client.run(bot_token) + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3e2c67c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +# requirements.txt +# +# installs dependencies from ./setup.py, and the package itself, +# in editable mode +-e . \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..0258eb3 --- /dev/null +++ b/setup.py @@ -0,0 +1,66 @@ + +import re +from os import path + +from setuptools import setup +from codecs import open + +here = path.abspath(path.dirname(__file__)) + +# Get the long description from the README file +with open(path.join(here, 'README.md'), encoding='utf-8') as f: + long_description = f.read() + +def read(*parts): + return open(path.join(here, *parts), 'r').read() + +def find_version(*file_paths): + version_file = read(*file_paths) + version_match = re.search(r"^version = ['\"]([^'\"]*)['\"]", + version_file, re.M) + if version_match: + return version_match.group(1) + raise RuntimeError("Unable to find version string.") + +setup( + name='discord-notifier', + version=find_version('discordnotifier','__init__.py'), + description='Simple discord notification bot', + long_description=long_description, + long_description_content_type="text/markdown", + + # The project's main homepage. + url='https://github.com/josephbmanley/discord-notifier', + + # Author details + author='Joseph Manley', + author_email='j@cloudsumu.com', + + # Choose your license + license='MIT', + + # See https://pypi.org/classifiers/ + classifiers=[ + 'Development Status :: 5 - Production/Stable', + 'Intended Audience :: Developers', + 'Intended Audience :: Information Technology', + 'Intended Audience :: System Administrators', + 'License :: OSI Approved :: MIT License', + 'Programming Language :: Python :: 3', + 'Programming Language :: Python :: 3.3', + 'Programming Language :: Python :: 3.4', + 'Programming Language :: Python :: 3.5', + 'Programming Language :: Python :: 3.6', + 'Programming Language :: Python :: 3.7', + 'Programming Language :: Python :: 3.8' + ], + keywords='AWS,Discord,Cloud,Notification', + packages=["discordnotifier"], + install_requires=['boto3','discord.py'], + package_data={}, + entry_points={ + 'console_scripts' : [ + 'discordnotifier = discordnotifier:main' + ] + } +)