-
Notifications
You must be signed in to change notification settings - Fork 77
/
sender.py
78 lines (59 loc) · 2.23 KB
/
sender.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
import requests
import json
import time
import re
import argparse
import sys
class Sender:
def __init__(self,
params):
self.params = params
self.sender_initializer()
def sender_initializer(self):
with open(self.params, "r") as json_file:
params = json.load(json_file)
self.channelid=params['channelid']
self.authorization=params['authorization']
self.application_id = params['application_id']
self.guild_id = params['guild_id']
self.session_id = params['session_id']
self.version = params['version']
self.id = params['id']
self.flags = params['flags']
def send(self, prompt):
header = {
'authorization': self.authorization
}
prompt = prompt.replace('_', ' ')
prompt = " ".join(prompt.split())
prompt = re.sub(r'[^a-zA-Z0-9\s]+', '', prompt)
prompt = prompt.lower()
payload = {'type': 2,
'application_id': self.application_id,
'guild_id': self.guild_id,
'channel_id': self.channelid,
'session_id': self.session_id,
'data': {
'version': self.version,
'id': self.id,
'name': 'imagine',
'type': 1,
'options': [{'type': 3, 'name': 'prompt', 'value': str(prompt) + ' ' + self.flags}],
'attachments': []}
}
r = requests.post('https://discord.com/api/v9/interactions', json = payload , headers = header)
while r.status_code != 204:
r = requests.post('https://discord.com/api/v9/interactions', json = payload , headers = header)
print('prompt [{}] successfully sent!'.format(prompt))
def parse_args(args):
parser = argparse.ArgumentParser()
parser.add_argument('--params', help='Path to discord authorization and channel parameters', required=True)
parser.add_argument('--prompt', help='prompt to generate', required=True)
return parser.parse_args(args)
if __name__ == "__main__":
args = sys.argv[1:]
args = parse_args(args)
params = args.params
prompt = args.prompt
sender = Sender(params)
sender.send(prompt)