-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathauto.py
147 lines (115 loc) · 4.71 KB
/
auto.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import json
import sys
import random
import time
from datetime import datetime
from http.client import HTTPSConnection
INFO_FILE = "info.txt"
MESSAGES_FILE = "messages.txt"
def get_timestamp():
"""
Returns a timestamp in the format YYYY-MM-DD HH:MM:SS
"""
return "[" + str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) + "]"
def random_sleep(duration, min_random, max_random):
sleep_duration = duration + random.randint(min_random, max_random)
print(f"{get_timestamp()} Sleeping for {sleep_duration} seconds")
time.sleep(sleep_duration)
def read_info():
try:
with open(INFO_FILE, "r") as file:
return file.read().splitlines()
except FileNotFoundError:
print(f"{get_timestamp()} Info file not found.")
return None
def write_info(user_id, token, channel_url, channel_id):
try:
with open(INFO_FILE, "w") as file:
file.write(f"{user_id}\n{token}\n{channel_url}\n{channel_id}")
except Exception as e:
print(f"{get_timestamp()} Error configuring user information: {e}")
exit()
def configure_info():
try:
user_id = input("User-ID: ")
token = input("Discord token: ")
channel_url = input("Discord channel URL: ")
channel_id = input("Discord channel ID: ")
write_info(user_id, token, channel_url, channel_id)
print(f"Written config to info.txt, please rerun to start!")
except Exception as e:
print(f"{get_timestamp()} Error configuring user information: {e}")
exit()
def set_channel():
info = read_info()
if info:
user_id, token, _, _ = info
channel_url = input("Discord channel URL: ")
channel_id = input("Discord channel ID: ")
write_info(user_id, token, channel_url, channel_id)
print(f"Written config to info.txt, please rerun to start!")
def show_help():
print("Showing help for discord-auto-messenger")
print("Usage:")
print(" 'python3 auto.py' : Runs the automessenger. Type in the wait time and take a back seat.")
print(" 'python3 auto.py --config' : Configure settings.")
print(" 'python3 auto.py --setC' : Set channel to send message to. Including Channel ID and Channel URL")
print(" 'python3 auto.py --help' : Show help")
def send_message(conn, channel_id, message_data, header_data):
try:
conn.request("POST", f"/api/v6/channels/{channel_id}/messages", message_data, header_data)
resp = conn.getresponse()
if 199 < resp.status < 300:
print(f"{get_timestamp()} Message {message_data} sent!")
except Exception as e:
print(f"{get_timestamp()} Error sending message: {e} | {message_data}")
def get_connection():
return HTTPSConnection("discordapp.com", 443)
def main():
if len(sys.argv) > 1:
if sys.argv[1] == "--config" and input("Configure? (y/n)") == "y":
configure_info()
return
elif sys.argv[1] == "--setC" and input("Set channel? (y/n)") == "y":
set_channel()
return
elif sys.argv[1] == "--help":
show_help()
return
info = read_info()
if not info or len(info) != 4:
print(
f"{get_timestamp()} An error was found inside the user information file. Please ensure the file contains "
f"the following information in order: User agent, Discord token, Discord channel URL, and Discord channel "
f"ID. Try again with python3 auto.py"
)
configure_info()
return
header_data = {
"content-type": "application/json",
"user-id": info[0],
"authorization": info[1],
"host": "discordapp.com",
"referrer": info[2]
}
print(f"{get_timestamp()} Messages will be sent to " + header_data["referrer"] + ".")
print("Please initialise your delays and sleep time, there will be some random offsets applied as well!\n")
delay_between_messages = int(input("Delay (in seconds) between messages: "))
sleep_time = int(input("Sleep time (in seconds): "))
while True:
try:
with open(MESSAGES_FILE, "r") as file:
messages = file.read().splitlines()
except FileNotFoundError:
print(f"{get_timestamp()} Messages file not found.")
return
for message in messages:
message_data = json.dumps({"content": message})
conn = get_connection()
send_message(conn, info[3], message_data, header_data)
conn.close()
random_sleep(delay_between_messages, 1, 10)
print(f"{get_timestamp()} Finished sending all messages!")
random_sleep(sleep_time, 20, 1200)
if __name__ == "__main__":
main()