-
Notifications
You must be signed in to change notification settings - Fork 133
/
message_queue_bot.py
83 lines (76 loc) · 2 KB
/
message_queue_bot.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
import discord
import asyncio
import aiohttp
import jsonpickle
async def truncate(channel, msg):
if len(msg) == 0:
return
split = [msg[i:i + 1999] for i in range(0, len(msg), 1999)]
try:
for s in split:
if len(split) > 1:
if s.startswith('```'):
s = s[:1995]+'\n```'
await bot.send_message(channel, s)
except Exception as e:
await bot.send_message(channel, e)
async def get_queue():
payload = {'key': ''}
try:
with aiohttp.ClientSession() as session:
with aiohttp.Timeout(15):
async with session.post('http://ip:port/queued', data=payload) as resp:
load = await resp.json()
queue = {}
for s in load:
queue[s] = int(load[s][2])
q = {}
for key in sorted(queue, key=lambda k: queue[k], reverse=False):
q[key] = load[key]
return q
except Exception as e:
print(e)
return {}
async def delete_queue(id):
payload = {'key':'', 'id':str(id)}
try:
with aiohttp.ClientSession() as session:
with aiohttp.Timeout(15):
async with session.post('http://ip:port/queue_delete', data=payload) as resp:
text = await resp.text()
return text
except:
return []
bot = discord.Client()
async def check_queue():
await bot.wait_until_ready()
while not bot.is_closed:
queue = await get_queue()
if len(queue) == 0:
await asyncio.sleep(1)
else:
for s in queue:
message_id = int(s)
channel_id = str(queue[s][0])
message = str(queue[s][1])
embed = True if int(queue[s][3]) else False
try:
target = bot.get_channel(channel_id)
if embed:
await bot.send_message(target, embed=jsonpickle.decode(message))
else:
await truncate(target, message)
except Exception as e:
print(e)
pass
delete_request = await delete_queue(message_id)
await asyncio.sleep(0.21)
@bot.event
async def on_ready():
print('Logged in as')
print(bot.user.name + "#" + bot.user.discriminator)
print(bot.user.id)
print('------')
await bot.change_presence(game=discord.Game(name="go away"))
bot.loop.create_task(check_queue())
bot.run('')