commit d5fcc4060cc1c9d0a339801a2aa176c3ac682bf5 Author: Birthday Smoothie Date: Thu Feb 24 15:50:00 2022 +0000 initial push diff --git a/.env-example b/.env-example new file mode 100644 index 0000000..ba89dae --- /dev/null +++ b/.env-example @@ -0,0 +1,2 @@ +BOT_KEY= +CHANNEL_ID_PAIRS= \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..b0dc83c --- /dev/null +++ b/.gitignore @@ -0,0 +1,134 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# Specific Files +daily_report_list.json +error_log.log +error_log_daily_send.log diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..c6fdb64 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,16 @@ +discord.py - https://discordpy.readthedocs.io/en/stable/ +dotenv - https://pypi.org/project/python-dotenv/ + +.env example: +The server2server message bot requires two parameters in its .env file: +1) [BOT_KEY] - this is the discord bot key that you will get after you create a discord bot +2) [CHANNEL_ID_PAIRS] - this is a python dictionary of Discord channel ids. The key of the pay is the sender channel id and the value is the receiver channel id. + +The bot only sends messages one way. So any messages that are sent in the receiver channel do not get sent to the sender channel. Only sender to receiver. + +Here is an example of what a .env file will look like: + +BOT_KEY= +CHANNEL_ID_PAIRS={"sender_channel_id_1" : receiver_channel_id_1, "sender_channel_id_2" : receiver_channel_id_2} + +The bot needs to be in both servers in order for this to work diff --git a/server2serverMessageBot.py b/server2serverMessageBot.py new file mode 100644 index 0000000..38b587c --- /dev/null +++ b/server2serverMessageBot.py @@ -0,0 +1,68 @@ +import discord +import json +import os +from dotenv import load_dotenv + +#load environment variables +load_dotenv() + +BOT_KEY = os.getenv('BOT_KEY') +CHANNEL_ID_PAIRS = json.loads(os.getenv('CHANNEL_ID_PAIRS')) + +sender_channel_ids = [] +for sender_id in CHANNEL_ID_PAIRS: + sender_channel_ids.append(int(sender_id)) + +# Format message for sending +def format_message(message, original_message): + sender_message = '**'+message.author.name+'**' + if original_message: + sender_message += ' (in reply to **'+original_message.author.name+'**)\n' + original_message_list = original_message.content.split('\n') + for line in original_message_list: + sender_message += '> '+line+'\n' + else: + sender_message += '\n' + + sender_message += message.content + + return sender_message + +client = discord.Client() + +@client.event +async def on_ready(): + print('We have logged in as {0.user}'.format(client)) + +@client.event +async def on_message(message): + if message.content == '!channel_id': + await message.channel.send(message.channel.id) + return + + # only process messages that are from the sender channel + if message.channel.id not in sender_channel_ids: + return + + # if the message is from the bot, dont do anything + if message.author == client.user: + return + + # cast message channel id to string so we can compare with channel_id_pairs + message_channel_id_str = str(message.channel.id) + + # find the receiver channel and send the message to that channel + receiver_channel = client.get_channel(CHANNEL_ID_PAIRS[message_channel_id_str]) + + if message.reference is not None: + original = await message.channel.fetch_message(id=message.reference.message_id) + + sender_message = format_message(message, original) + await receiver_channel.send(sender_message) + return + + sender_message = format_message(message, None) + await receiver_channel.send(sender_message) + return + +client.run(BOT_KEY)