-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannbot.py
90 lines (70 loc) · 2.02 KB
/
annbot.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
from dotenv import load_dotenv
import discord
import os
import requests
import json
load_dotenv()
host = os.getenv("HOST")
port = os.getenv("PORT")
token = str(os.getenv("TOKEN"))
channel_id = int(os.getenv("CHANNEL_ID") or 0)
max_messages = int(os.getenv("MAX_MESSAGES") or 20)
bot = discord.Bot()
@bot.event
async def on_ready():
print(f'logged in!')
await handle(bot)
@bot.event
async def on_message(message):
await handle(bot)
@bot.event
async def on_raw_message_delete(payload):
await handle(bot)
@bot.event
async def on_raw_message_edit(payload):
await handle(bot)
@bot.event
async def on_raw_reaction_add(payload):
await handle(bot)
@bot.event
async def on_raw_reaction_remove(payload):
await handle(bot)
async def handle(client):
channel = client.get_channel(channel_id)
all_channels = client.get_all_channels()
messages = await channel.history(limit=max_messages).flatten()
parsed = {
'messages': [std_message(_) for _ in messages],
"channels": {x.id: std_channel(x) for x in all_channels}
}
response = requests.post(
f'http://{host}:{port}/announcements',
data=json.dumps(parsed)
)
print(f'{response.status_code}')
def std_message(message):
return {
'author': message.author.name,
'message': message.content,
'createdAt': message.created_at.strftime('%m/%d/%Y'),
'reactions': [std_reaction(_) for _ in message.reactions],
'attachments': [std_attachment(_) for _ in message.attachments]
}
def std_reaction(reaction):
if reaction.is_custom_emoji():
return {
'type': 'custom',
'content': f'https://cdn.discordapp.com/emojis/{reaction.emoji.id}.png',
'count': reaction.count
}
else:
return {
'type': 'emoji',
'content': reaction.emoji,
'count': reaction.count
}
def std_attachment(attachement):
return attachement.url
def std_channel(channel):
return channel.name
bot.run(token)