-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdiscord_message_sender.py
64 lines (50 loc) · 1.91 KB
/
discord_message_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
import os
import yaml
import config
import asyncio
from loguru import logger
from dotenv import load_dotenv
from aiohttp import ClientSession
from pyuseragents import random as random_useragent
from tenacity import retry, retry_if_exception, stop_after_attempt, wait_fixed
load_dotenv()
HEADERS = {
'accept': '*/*',
'accept-encoding': 'gzip, deflate, br',
'content-type': 'application/json',
'origin': 'https://discord.com',
'authorization': os.environ["TOKEN"],
'user-agent': random_useragent()
}
@retry(retry=retry_if_exception(Exception), stop=stop_after_attempt(1), wait=wait_fixed(5), reraise=True)
async def send_message(client: ClientSession):
try:
response = await client.post(f'https://discord.com/api/v9/channels/{CHANNEL}/messages',
json={
"content": MESSAGE,
"tts": False,
"flags": 0
})
data = await response.json()
response.raise_for_status()
except Exception:
raise Exception(data['message'])
async def main():
async with ClientSession(headers=HEADERS) as client:
while True:
try:
await send_message(client)
logger.success("Message sent successfully")
except Exception as error:
logger.error(error)
finally:
await asyncio.sleep(DELAY)
if __name__ == '__main__':
print('Discord Message Sender @flamingoat\n')
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
with open('config.yaml', 'r') as f:
config = yaml.safe_load(f)
CHANNEL = config["CHANNEL_ID"]
MESSAGE = config["MESSAGE"]
DELAY = config["DELAY"]
asyncio.run(main())