/
listen_discord.py
109 lines (92 loc) · 3.33 KB
/
listen_discord.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
import discord
from read_ytchat import YTchats
from datetime import datetime
from setup import DISCORD_TOKEN, setup_parser, logger
state_file = "./state"
chat_folder = "chat"
client = discord.Client()
chats = YTchats(state=True, save=True,
state_file=state_file, chat_folder=chat_folder)
parser = setup_parser()
@client.event
async def on_ready():
logger.debug(client.guilds)
logger.info(f"{client.user} has connected to Discord!")
# Overwrite the post function after Discord client initized
for v in chats.videos:
v.send = discord_notify(int(v.chid))
await chats.main()
def discord_notify(channel):
async def send(c):
if type(c) is str:
await client.get_channel(channel).send(c)
return
logger.debug(f"Print data: {str(c.json())}")
# If new member: message = join message
if c.amountString:
text = f"[{c.amountString}]\n{c.message}"
elif c.type != "textMessage":
text = f"[{c.message}]"
else:
text = f"{c.message}"
dtime = datetime.utcfromtimestamp(c.timestamp / 1000)
# name, color(ARGB) and time
embed = discord.Embed(title=c.author.name,
colour=c.bgColor % 0x1000000,
description=text,
timestamp=dtime)
# thumbnail
embed.set_thumbnail(url=c.author.imageUrl)
# send
await client.get_channel(channel).send(embed=embed)
return send
@client.event
async def on_message(message):
# Only read command exclude bot itself
if message.author == client.user:
return
if not message.content.startswith(".synchat"):
return
# if no args
if not message.content.startswith(".synchat "):
await message.channel.send("```" + parser.format_help() + "```")
return
# read command and videoid
logger.debug(message.content)
try:
args = parser.parse_args(message.content.split()[1:])
except BaseException as e:
# Fix this in Python3.9
logger.warning(str(type(e)) + str(e))
await message.channel.send("```" + parser.format_help() + "```")
return
method, id = args.method, args.id
dc_channel = message.channel.id
# list monitor list
if method == "list":
ids = [v.ytid for v in chats.videos if v.chid == str(dc_channel)]
await message.channel.send("sync list: " + ",".join(ids))
return
# id cannot be null if user wants to start or stop the chat
if id is None:
await message.channel.send("Fail: No video ID provieded")
return
# start to monitor
if method == "start":
logger.info(f"Sync {id} to {dc_channel}")
if chats.add_video(id, dc_channel, discord_notify(dc_channel),
save=True, chat_folder=chat_folder):
await message.channel.send(f"OK {id}")
else:
await message.channel.send(f"Fail to add {id}")
# stop monitor
elif method == "stop":
ok = await chats.remove_video(id, dc_channel)
if ok:
await message.channel.send("OK")
else:
await message.channel.send(f"No {id} found")
else:
await message.channel.send(f"{method} not implemented")
if __name__ == "__main__":
client.run(DISCORD_TOKEN)