scwrypts/py/discord/post-message.py
yage 6c546ebb6f v3.1.0
=====================================================================

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

- improved capability of py/discord/post-message to include username
  flag, and defaults for each value in env config
2023-06-24 07:40:31 -06:00

62 lines
1.9 KiB
Python
Executable File

#!/usr/bin/env python
from json import dumps
from sys import stderr
from py.lib.http import discord
from py.lib.scwrypts import execute
from py.lib.scwrypts.exceptions import ImportedExecutableError
if __name__ != '__main__':
raise ImportedExecutableError()
#####################################################################
def main(args, stream):
if args.content is None:
print(f'reading input from {stream.input.name}', file=stderr)
args.content = ''.join(stream.readlines()).strip()
if len(args.content) == 0:
args.content = 'PING'
response = discord.send_message(**vars(args))
stream.writeline(dumps({
**(response.json() if response.text != '' else {'message': 'OK'}),
'scwrypts_metadata': {},
}))
#####################################################################
execute(main,
description = 'post a message to the indicated discord channel',
parse_args = [
( ['-b', '--body'], {
'dest' : 'content',
'help' : 'message body',
'required' : False,
}),
( ['-c', '--channel-id'], {
'dest' : 'channel_id',
'help' : 'override default target channel id',
'required' : False,
}),
( ['-w', '--webhook'], {
'dest' : 'webhook',
'help' : 'override default target webhook (takes precedence over -c)',
'required' : False,
}),
( ['--avatar-url'], {
'dest' : 'avatar_url',
'help' : 'override default avatar_url',
'required' : False,
}),
( ['--username'], {
'dest' : 'username',
'help' : 'override default username',
'required' : False,
}),
]
)