Skip to content

Instantly share code, notes, and snippets.

@sgtlaggy
Last active October 27, 2016 22:18
Show Gist options
  • Save sgtlaggy/d5721b257fdff65c89c59610b965fe3f to your computer and use it in GitHub Desktop.
Save sgtlaggy/d5721b257fdff65c89c59610b965fe3f to your computer and use it in GitHub Desktop.
import urllib.request
import random
import json
try:
from tkinter import Tk, messagebox
except ImportError:
tk = False
else:
tk = True
root = Tk()
root.withdraw()
# Discord Markdown formatting works here!
# Keep this in the same order as the WEBHOOKS below if you want -
# webhook/channel-specific messages.
# If you want the same message across all webhooks, just use one message.
MESSAGES = [
"MESSAGE1",
]
# If you have more than one message and want a random one to be sent, -
# set this to True, else set it to False.
randomise = False
# Right click channel > Edit Channel > Webhooks
# Create webhook or edit existing, copy URL
# Add another line inside the brackets to announce to extra channels
# "WEBHOOK URL",
# note the trailing comma
WEBHOOKS = [
"WEBHOOK URL",
]
statuses = []
headers = {'Content-Type': 'application/json',
'User-Agent': "Stream Announcer"}
for ind, hook in enumerate(WEBHOOKS):
if len(MESSAGES) == 1:
message = MESSAGES[0]
elif randomise:
message = random.choice(MESSAGES)
elif len(MESSAGES) == len(WEBHOOKS):
message = MESSAGES[ind]
else:
if tk:
messagebox.showerror('Error',
'Mismatching message and webhook counts.\n'
'Fix that or set `randomise` to `True`.')
else:
print('Mismatching message and webhook counts.\n'
'Fix that or set `randomise` to `True`.')
input('Press RETURN or close the window to exit.')
raise SystemExit(1)
data = json.dumps({'content': message}).encode('utf-8')
req = urllib.request.Request(hook, data=data, headers=headers, method='POST')
with urllib.request.urlopen(req) as r:
statuses.append(r.getcode())
print(('Status code: {}\n' * len(statuses)).format(*statuses))
input('Press RETURN or close the window to exit.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment