forked from Inahime/Discord-LINE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
discord_bot.py
73 lines (57 loc) · 1.87 KB
/
discord_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
import discord
import dotenv
import json
import os
import requests
client = discord.Client()
# Discord, LINE, Group ID
dotenv.load_dotenv()
CHANNEL_ACCESS_TOKEN = os.environ['CHANNEL_ACCESS_TOKEN']
DISCORD_ACCESS_TOKEN = os.environ['DISCORD_ACCESS_TOKEN']
LINE_GROUP_ID = os.environ['LINE_GROUP_ID']
@client.event
async def on_message(message):
# bot からのメッセージは無視する
if message.author.bot:
return
post_to_line(message)
def post_to_line(msg):
content = format_content(msg)
message = {
'to': LINE_GROUP_ID,
'messages': content
}
headers = {'Content-Type': 'application/json',
'Authorization': 'Bearer ' + CHANNEL_ACCESS_TOKEN}
requests.post('https://api.line.me/v2/bot/message/push',
data=json.dumps(message), headers=headers)
def format_content(message):
content = [
{
'type': 'text',
'text': message.author.display_name + ' sent on Discord'
}
]
if len(message.content) != 0:
content.append(
{'type': 'text',
'text': message.content},
)
if len(message.attachments) != 0:
roop = len(message.attachments)
for i in range(roop):
content_type = message.attachments[i].content_type
if 'image' in content_type:
content.append(
{'type': 'image',
'originalContentUrl': message.attachments[i].url,
'previewImageUrl': message.attachments[i].url}
)
elif 'video' in content_type:
content.append(
{'type': 'video',
'originalContentUrl': message.attachments[i].url,
'previewImageUrl': message.attachments[i].url}
)
return content
client.run(DISCORD_ACCESS_TOKEN)