Skip to content

Instantly share code, notes, and snippets.

@jotson
Created March 12, 2020 14:10
Show Gist options
  • Save jotson/12eb7bb315d7bf3df42aece9a792eb02 to your computer and use it in GitHub Desktop.
Save jotson/12eb7bb315d7bf3df42aece9a792eb02 to your computer and use it in GitHub Desktop.
import json
import requests
BUILDDIR = '/home/john/code/games/gravity/build'
CHANGELOG = '/home/john/code/games/gravity/changelog.md'
def get_changelog():
f = open(CHANGELOG, 'r')
n = 0
log = ''
for line in f:
if line.startswith('#') and n > 0:
break
n = n + 1
log = log + line
f.close()
log = log.lstrip().rstrip()
return log
def post_changelog():
'''
Post latest changelog to discord
'''
log = get_changelog()
print('Posting changelog to discord...\n')
color = 38377
title = log.split('\n')[0].replace('# ', '')
description = '\n'.join(log.split('\n')[1:]).lstrip()
discord_json = {
"content": "A new build has just been uploaded!",
"embeds": [
{
"title": title,
"description": description,
"color": color,
}
]
}
response = requests.post(
get_discord_webhook('discord-general'),
data=json.dumps(discord_json),
headers={ 'Content-Type': 'application/json' }
)
if response.status_code < 200 or response.status_code > 299:
raise ValueError('Discord error %s:\n%s' % (response.status_code, response.text))
def post_commit_message(title, message):
'''
Post commit message to Discord
'''
color = 38377
discord_json = {
"content": "**New commit: " + title + "**"
}
if message:
discord_json["embeds"] = [
{
"description": message,
"color": color,
}
]
response = requests.post(
get_discord_webhook('discord-git'),
data=json.dumps(discord_json),
headers={ 'Content-Type': 'application/json' }
)
if response.status_code < 200 or response.status_code > 299:
raise ValueError('Discord error %s:\n%s' % (response.status_code, response.text))
def get_discord_webhook(name):
webhook = None
with open(BUILDDIR + "/." + name, "r") as f:
webhook = f.readline().rstrip()
return webhook
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment