-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransBot_Bata2.py
137 lines (119 loc) · 4.84 KB
/
TransBot_Bata2.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#-*- coding:utf-8 -*-
# 번역기봇 제작:권오준 http://www.kwon5jun.com
import discord
import asyncio
import os
import urllib.request
client = discord.Client()
client_id = os.environ["Naver_API_ID"]
client_secret = os.environ["Naver_API_Secret"]
@client.event
async def on_ready():
print("="*20)
print(client.user.name)
print('너굴너굴 번역기 봇 작동!!!')
print("="*20)
@client.event
async def on_message(message):
await client.change_presence(game=discord.Game(name='!!help 번역기봇 베타버전입니다.@@ 오류문의는 꼬기빙수#5197 ')) # ~~~ playing
if message.author != client.user: # 봇이 봇에게 명령하지 못합니다.
if message.content.startswith('!!help'): # !commands
print("도움말을 요청")
msg = await client.send_message(message.channel, """
모든 명령어 앞에는 항상 !!를 붙입니다. Bata V2.2
!!en2ko [...] English -> Korean
!!ko2en [...] Korean -> English
!!ja2ko [...] Japanese-> Korean
!!ko2ja [...] Korean -> Japanese
ex) !!en2ko Hi """) # bot's answer
#--영어>한글
elif message.content.startswith('!!en2ko'):
m = message.content
if(len(m)>8):
txtname = message.author.display_name
print("'영어>한글 번역")
sourcetxt = 'en'
targettxt = 'ko'
await client.send_message(message.channel,trans(sourcetxt,targettxt,m,txtname))
else:
await client.send_message(message.channel,"""번역할 문자를 입력하세요. (Please enter what you want to translate.)""")
#--한글>영어
elif message.content.startswith('!!ko2en'):
m = message.content
if(len(m)>8):
txtname = message.author.display_name
print("'한글>영어 번역")
sourcetxt = 'ko'
targettxt = 'en'
await client.send_message(message.channel,trans(sourcetxt,targettxt,m,txtname))
else:
await client.send_message(message.channel,"""번역할 문자를 입력하세요. (Please enter what you want to translate.)""")
#--일어>한글
elif message.content.startswith('!!ja2ko'):
m = message.content
if(len(m)>8):
txtname = message.author.display_name
print("'일어>한글 번역")
sourcetxt = 'ja'
targettxt = 'ko'
await client.send_message(message.channel,trans(sourcetxt,targettxt,m,txtname))
else:
await client.send_message(message.channel,"""번역할 문자를 입력하세요. (Please enter what you want to translate.)""")
#--한글>일어
elif message.content.startswith('!!ko2ja'):
m = message.content
if(len(m)>8):
txtname = message.author.display_name
print("'한글>일어' 번역")
sourcetxt = 'ko'
targettxt = 'ja'
await client.send_message(message.channel,trans(sourcetxt,targettxt,m,txtname))
else:
await client.send_message(message.channel,"""번역할 문자를 입력하세요. (Please enter what you want to translate.)""")
#--영어>일어
elif message.content.startswith('!!en2ja'):
m = message.content
if(len(m)>8):
txtname = message.author.display_name
print("'영어>일어' 번역")
sourcetxt = 'en'
targettxt = 'ja'
await client.send_message(message.channel,trans(sourcetxt,targettxt,m,txtname))
else:
await client.send_message(message.channel,"""번역할 문자를 입력하세요. (Please enter what you want to translate.)""")
#--일어>영어
elif message.content.startswith('!!ja2en'):
m = message.content
if(len(m)>8):
txtname = message.author.display_name
print("'일어>영어' 번역")
sourcetxt = 'en'
targettxt = 'ja'
await client.send_message(message.channel,trans(sourcetxt,targettxt,m,txtname))
else:
await client.send_message(message.channel,"""번역할 문자를 입력하세요. (Please enter what you want to translate.)""")
def trans(sourcetxt,targettxt,m,txtname):
print(txtname+"님이 번역요청")
encText = urllib.parse.quote(m[7:])
data = "source="+sourcetxt+"&target="+targettxt+"&text=" + encText
url = "https://openapi.naver.com/v1/papago/n2mt"
request = urllib.request.Request(url)
request.add_header("X-Naver-Client-Id",client_id)
request.add_header("X-Naver-Client-Secret",client_secret)
response = urllib.request.urlopen(request, data=data.encode("utf-8"))
rescode = response.getcode()
if(rescode==200):
response_body = response.read()
msg = sourcetxt+"""->"""+targettxt+"""번역하였습니다. (translated it.)
["""+ txtname +"""] """+response_body.decode('utf-8')[152:-4]
elif(rescode==400):
msg = """HTTP 400 오류."""
elif(rescode==429):
msg = """한도초과입니다 내일 이용해주세요..."""
elif(rescode==500):
msg = """HTTP 500 오류."""
else:
msg = """알수없는 오류"""
return msg
access_token = os.environ["BOT_TOKEN"]
client.run('access_token') #디스코드 사이트 > 개발자 포털 > 내 봇 > Bot token