This repository was archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathslack.py
120 lines (114 loc) · 3.04 KB
/
slack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
slack
~~~~~~~~~~~~~~~~~~~~~
An extension module to help communicating with Slack
"""
import aiohttp
import discord
import json
import os
from pathlib import Path
from fire import exceptions
with open("config.json", 'r') as cfg:
config = json.load(cfg)
SIGNING_SECRET = config['signing_secret']
async def sendvanity(slug: str, user: discord.Member, guild: discord.Guild):
message = {
"token":config['slackbot'],
"channel":"CP7SH4G8K",
"attachments":[
{
"title":"Vanity URL Request",
"fields":[
{
"title":"Guild",
"value":f'{guild.name} ({guild.id})'
},
{
"title":"User",
"value":f'{user.name} ({user.id})'
},
{
"title":"Requested Slug",
"value":slug
}
],
"author_name":"Fire",
"author_icon":"https://cdn.discordapp.com/avatars/444871677176709141/4bafec4cf070f01ddf4a5428947813e6.png?size=1024"
},
{
"fallback":"Please choose an option!",
"title":"Please choose an option!",
"callback_id":f'vanity_{guild.id}',
"attachment_type":"default",
"actions":[
{
"name":"ignore",
"text":"Ignore",
"type":"button",
"value":"ignore",
"style":"primary"
},
{
"name":"delete",
"text":"Delete",
"type":"button",
"value":"delete",
"style":"danger"
}
]
}
]
}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + config['slackbot']
}
async with aiohttp.ClientSession(headers=headers) as s:
async with s.post('https://slack.com/api/chat.postMessage', json=message) as r:
response = await r.json()
if not response['ok']:
raise exceptions.PushError(f'An error occurred while posting a message to Slack\n{response["error"]}')
return response['message']
async def updatevanitymsg(ignore: bool, message: dict):
if ignore:
message['attachments'][1] == {
"title":"Ignored!",
"color": "#4CAF50"
}
data = {
"token": config['slackbot'],
"channel": "CP7SH4G8K",
"ts": message['ts'],
"attachments": message['attachments']
}
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + config['slackbot']
}
print(headers)
async with aiohttp.ClientSession(headers=headers) as s:
async with s.post('https://slack.com/api/chat.update', json=data) as r:
response = await r.json()
if response['ok']:
return True
else:
raise exceptions.PushError(f'An error occurred while updating a message on Slack\n{response["error"]}')
else:
message['attachments'][1] == {
"title":"Deleted!",
"color": "#D32F2F"
}
data = {
"token": config['slackbot'],
"channel": "CP7SH4G8K",
"ts": message['ts'],
"attachments": message['attachments']
}
async with aiohttp.ClientSession(headers={'Content-Type': 'application/json'}) as s:
async with s.post('https://slack.com/api/chat.update', json=data) as r:
response = await r.json()
if response['ok']:
return True
else:
raise exceptions.PushError(f'An error occurred while updating a message on Slack\n{response["error"]}')