=====================================================================

--- Changes ------------------------------

- improved capability of py/discord/post-message to include username
  flag, and defaults for each value in env config
This commit is contained in:
Wryn (yage) Wagner 2023-06-24 07:40:31 -06:00
parent 9783119a7d
commit 6c546ebb6f
4 changed files with 40 additions and 18 deletions

View File

@ -6,8 +6,12 @@ export AWS__EFS__LOCAL_MOUNT_POINT=
export DIRECTUS__API_TOKEN=
export DIRECTUS__BASE_URL=
export DISCORD__BOT_TOKEN=
export DISCORD__CONTENT_FOOTER=
export DISCORD__CONTENT_HEADER=
export DISCORD__DEFAULT_AVATAR_URL=
export DISCORD__DEFAULT_CHANNEL_ID=
export DISCORD__DEFAULT_USERNAME=
export DISCORD__DEFAULT_WEBHOOK=
export I3__BORDER_PIXEL_SIZE=
export I3__DMENU_FONT_SIZE=
export I3__GLOBAL_FONT_SIZE=

View File

@ -8,8 +8,12 @@ DIRECTUS__API_TOKEN | details for a directus instance
DIRECTUS__BASE_URL |
DISCORD__BOT_TOKEN | details for discord bot
DISCORD__CONTENT_HEADER |
DISCORD__CONTENT_FOOTER |
DISCORD__DEFAULT_AVATAR_URL |
DISCORD__DEFAULT_CHANNEL_ID |
DISCORD__DEFAULT_USERNAME |
DISCORD__DEFAULT_WEBHOOK |
I3__BORDER_PIXEL_SIZE | custom i3 configuration settings
I3__DMENU_FONT_SIZE |

View File

@ -13,19 +13,14 @@ if __name__ != '__main__':
#####################################################################
def main(args, stream):
if args.body is None:
if args.content is None:
print(f'reading input from {stream.input.name}', file=stderr)
args.body = ''.join(stream.readlines()).strip()
args.content = ''.join(stream.readlines()).strip()
if len(args.body) == 0:
args.body = 'PING'
if len(args.content) == 0:
args.content = 'PING'
response = discord.send_message(
content = args.body,
channel_id = args.channel_id,
webhook = args.webhook,
avatar_url = args.avatar_url,
)
response = discord.send_message(**vars(args))
stream.writeline(dumps({
**(response.json() if response.text != '' else {'message': 'OK'}),
@ -38,23 +33,28 @@ execute(main,
description = 'post a message to the indicated discord channel',
parse_args = [
( ['-b', '--body'], {
'dest' : 'body',
'dest' : 'content',
'help' : 'message body',
'required' : False,
}),
( ['-c', '--channel-id'], {
'dest' : 'channel_id',
'help' : 'target channel id',
'help' : 'override default target channel id',
'required' : False,
}),
( ['-w', '--webhook'], {
'dest' : 'webhook',
'help' : 'target webhook (takes precedence over -c)',
'help' : 'override default target webhook (takes precedence over -c)',
'required' : False,
}),
( ['--avatar-url'], {
'dest' : 'avatar_url',
'help' : 'replace default avatar_url',
'help' : 'override default avatar_url',
'required' : False,
}),
( ['--username'], {
'dest' : 'username',
'help' : 'override default username',
'required' : False,
}),
]

View File

@ -1,9 +1,9 @@
from py.lib.scwrypts import getenv
from py.lib.http.discord import request
def send_message(content, channel_id=None, webhook=None, avatar_url=None, **kwargs):
if channel_id is None:
channel_id = getenv('DISCORD__DEFAULT_CHANNEL_ID', required=False)
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)
if avatar_url is None:
avatar_url = getenv('DISCORD__DEFAULT_AVATAR_URL', required=False)
@ -12,11 +12,25 @@ def send_message(content, channel_id=None, webhook=None, avatar_url=None, **kwar
if webhook is not None:
endpoint = f'webhooks/{webhook}'
elif channel_id is not None:
endpoint = f'channels/{channel_id}/messages'
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'
else:
raise ValueError('must provide target channel_id or webhook')
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}'
return request(
method = 'POST',
@ -25,7 +39,7 @@ def send_message(content, channel_id=None, webhook=None, avatar_url=None, **kwar
key: value
for key, value in {
'content': content,
'username': 'wrobot',
'username': username,
'avatar_url': avatar_url,
**kwargs,
}.items()