2023-02-22 01:44:27 +00:00
|
|
|
from py.lib.scwrypts import getenv
|
|
|
|
from py.lib.http.discord import request
|
|
|
|
|
2023-06-24 13:40:31 +00:00
|
|
|
def send_message(content, channel_id=None, webhook=None, username=None, avatar_url=None, **kwargs):
|
|
|
|
if username is None:
|
|
|
|
username = getenv('DISCORD__DEFAULT_USERNAME', required=False)
|
2023-02-22 01:44:27 +00:00
|
|
|
|
|
|
|
if avatar_url is None:
|
|
|
|
avatar_url = getenv('DISCORD__DEFAULT_AVATAR_URL', required=False)
|
|
|
|
|
|
|
|
endpoint = None
|
|
|
|
|
|
|
|
if webhook is not None:
|
|
|
|
endpoint = f'webhooks/{webhook}'
|
2023-06-24 13:40:31 +00:00
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
elif channel_id is not None:
|
|
|
|
endpoint = f'channels/{channel_id}/messages'
|
2023-06-24 13:40:31 +00:00
|
|
|
|
|
|
|
elif (webhook := getenv('DISCORD__DEFAULT_WEBHOOK', required=False)) is not None:
|
|
|
|
endpoint = f'webhooks/{webhook}'
|
|
|
|
|
|
|
|
elif (channel_id := getenv('DISCORD__DEFAULT_CHANNEL_ID', required=False)) is not None:
|
|
|
|
endpoint = f'channels/{channel_id}/messages'
|
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
else:
|
|
|
|
raise ValueError('must provide target channel_id or webhook')
|
|
|
|
|
2023-06-24 13:40:31 +00:00
|
|
|
if (header := getenv('DISCORD__CONTENT_HEADER', required=False)) is not None:
|
|
|
|
content = f'{header}{content}'
|
|
|
|
|
|
|
|
if (footer := getenv('DISCORD__CONTENT_FOOTER', required=False)) is not None:
|
|
|
|
content = f'{content}{footer}'
|
|
|
|
|
2023-02-22 01:44:27 +00:00
|
|
|
|
|
|
|
return request(
|
|
|
|
method = 'POST',
|
|
|
|
endpoint = endpoint,
|
|
|
|
json = {
|
|
|
|
key: value
|
|
|
|
for key, value in {
|
|
|
|
'content': content,
|
2023-06-24 13:40:31 +00:00
|
|
|
'username': username,
|
2023-02-22 01:44:27 +00:00
|
|
|
'avatar_url': avatar_url,
|
|
|
|
**kwargs,
|
|
|
|
}.items()
|
|
|
|
if value is not None
|
|
|
|
},
|
|
|
|
)
|