initial push

This commit is contained in:
Birthday Smoothie 2022-02-24 15:50:00 +00:00
commit d5fcc4060c
4 changed files with 220 additions and 0 deletions

2
.env-example Normal file
View File

@ -0,0 +1,2 @@
BOT_KEY=
CHANNEL_ID_PAIRS=

134
.gitignore vendored Executable file
View File

@ -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

16
requirements.txt Normal file
View File

@ -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=<discord_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

View File

@ -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)