-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (63 loc) · 2.69 KB
/
main.py
File metadata and controls
74 lines (63 loc) · 2.69 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import logging
from os import getenv
from aiohttp import ClientSession
from discord import Intents, Message, Client, DMChannel, User, Reaction, Activity, ActivityType, Webhook
from dotenv import load_dotenv
from model import get_translation
load_dotenv()
TOKEN = getenv("TOKEN")
GENERAL = int(getenv("GENERAL"))
GENERAL_CN = int(getenv("GENERAL_CN"))
GENERAL_URL = getenv("GENERAL_URL")
GENERAL_CN_URL = getenv("GENERAL_CN_URL")
client = Client(intents=Intents.all())
logging.basicConfig(level=logging.INFO, format="%(levelname)s | [%(asctime)s] %(message)s")
@client.event
async def on_ready():
global general
global general_cn
general = client.get_channel(GENERAL)
general_cn = client.get_channel(GENERAL_CN)
activity = Activity(name="Just a Translator", type=ActivityType.playing)
await client.change_presence(activity=activity)
@client.event
async def on_message(message: Message):
if message.author == client.user or message.author.display_name.find("[Translated]") > -1:
return
if isinstance(message.channel, DMChannel):
return
if not message.content:
return
logging.info(f"({message.channel.name}) {message.author.display_name}: {message.content}")
if message.channel == general:
response = get_translation(message.content, 1)
logging.info(f"Translated \"{message.content}\" to \"{response}\"")
if response == "None":
await message.channel.send("Someone tell XiaoYuan151 there is a problem with my AI.")
return
async with ClientSession() as session:
webhook = Webhook.from_url(GENERAL_CN_URL, session=session)
await webhook.send(content=response, username=f"[Translated] {message.author.display_name}",
avatar_url=message.author.avatar.url)
return
if message.channel == general_cn:
response = get_translation(message.content, 0)
logging.info(f"Translated \"{message.content}\" to \"{response}\"")
if response == "None":
await message.channel.send("Someone tell XiaoYuan151 there is a problem with my AI.")
return
async with ClientSession() as session:
webhook = Webhook.from_url(GENERAL_URL, session=session)
await webhook.send(content=response, username=f"[Translated] {message.author.display_name}",
avatar_url=message.author.avatar.url)
return
@client.event
async def on_reaction_add(reaction: Reaction, user: User):
if user == client.user:
return
if isinstance(reaction.message.channel, DMChannel):
return
if not reaction.emoji:
return
if __name__ == "__main__":
client.run(TOKEN)