-
Notifications
You must be signed in to change notification settings - Fork 0
/
etymology.py
55 lines (42 loc) · 1.26 KB
/
etymology.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
import discord
import openai
import re
# 设置 Discord API 密钥和 ChatGPT API 密钥
discord_token = 'YOUR_DISCORD_BOT_TOKEN_HERE'
openai.api_key = 'YOUR_OPENAI_API_KEY_HERE'
intents = discord.Intents.default()
intents.message_content = True
# 创建一个 Discord 客户端
client = discord.Client(intents=intents)
async def get_all_messages(thread):
msgs = []
async for msg in thread.history(limit=None):
msgs.append(msg)
return msgs
# 当机器人已经启动时运行
@client.event
async def on_ready():
print('Bot is ready.')
# 当机器人收到新消息时运行
@client.event
async def on_message(message):
print(message.content)
if message.author.name == 'Etymology':
return
msgs = []
msgs.append({
'role': 'user',
'content': message.content + '的含义的演变史是什么,请用中文回答'
})
# 使用 ChatGPT 生成词源
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=msgs
)
content = response.choices[0].message.content.strip()
content = f"**{message.content}**\n{content}"
print(content)
# 发送总结到 Thread 频道
await message.channel.send(content=content)
# 运行机器人
client.run(discord_token)