forked from BankeraJAPAN/python_test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatson_Language_Translator.py
More file actions
56 lines (44 loc) · 1.35 KB
/
watson_Language_Translator.py
File metadata and controls
56 lines (44 loc) · 1.35 KB
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
import discord
import sys
from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator
username = sys.argv[1]
password = sys.argv[2]
language_translator = LanguageTranslator(
username = username,
password = password
)
# テストtoken
token = "NDA0NjE4MDA4MjA0NTQxOTYy.DUoAtQ.DqDyvVDhSIQSMD-KNRtx86WKRgo"
client = discord.Client()
client.get_all_members()
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
@client.event
async def on_message(message):
# 送り主がBotだった場合反応したくないので
if client.user != message.author:
text = message.content # メッセージを取り出す
# 言語識別
language = language_translator.identify(text)
source = language['languages'][0]['language']
if source == 'en':
target = 'ja'
elif source == 'ja':
target = 'en'
elif source == 'de':
target = 'en'
else:
source = 'ja'
target = 'en'
# 翻訳
translation = language_translator.translate(
text=text,
source=source,
target=target)
msg = translation["translations"][0]["translation"]
await client.send_message(message.channel, msg)
client.run(token)