-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
369 lines (310 loc) · 11 KB
/
Copy pathbot.py
File metadata and controls
369 lines (310 loc) · 11 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@Date: 2022-11-02 13:08:34
@Author: 今日摸鱼
@Email: lemonsoldout@163.com
@Github: https://www.github.com/lemonsoldout
@Package: Discord-Bot
@Version: beta-v0.1
@Description: Discord 多功能机器人
'''
# import modules
# Discord Interactions
# Github https://discord-interactions.readthedocs.io/en/latest/api.html
import interactions
from interactions import autodefer
import datetime
import os
import random
import yaml
import asyncio
from function import league
from function import helpCommand
from function import chatGPT
from util import utils
# Discord Bot Token
# Read from Token.txt file and keep it safe
# token_file = open("./config/Token.txt", "r")
# token = token_file.read()
def getToken(name:str):
with open('./config/config.yaml') as file:
config = yaml.safe_load(file)
if name == "discord":
token = config['config']['discord']["今日摸鱼"]
elif name == "lol":
token = config['config']['lol']
elif name == "chatGPT":
token = config['config']['chatGPT']
elif name == "scope":
token = config['config']['scope']["Eternal"]
else:
token = "ERROR"
return token
dicord_token = getToken("discord")
lol_token = getToken("lol")
chatGPT_token = getToken("chatGPT")
scope_id = getToken("scope")
bot = interactions.Client(dicord_token)
bot_name = "今日摸鱼"
os_name = utils.checkOperatingSystem()
embed_color_list = [0,1752220,1146986,5763719,2067276,3447003,2123412,10181046,7419530,15277667,11342935,15844367,12745742,15105570,11027200,15548997,10038562,9807270,9936031,8359053,12370112,3426654,2899536,16776960]
@bot.event
async def on_ready():
current_time = datetime.datetime.now()
print(f'\n[机器人]\t{bot_name} 已上线...\n[USA BOS]\t', current_time,f'\n[操作系统]\t{os_name}')
@bot.command(
name = "help",
description = "今日摸鱼 Discord 机器人食用手册",
#scope = scope_id
)
@autodefer()
async def help_command(ctx: interactions.CommandContext):
await asyncio.sleep(1)
res = helpCommand.help()
await ctx.send(res)
@bot.command(
name = "game",
description = "闲暇之际, 不来点小游戏开心一下?",
#scope = scope_id
options = [
interactions.Option(
type = interactions.OptionType.INTEGER,
name = "choice",
description = "输入数字: 1. 成语接龙 2. 猜大小 3. ToDo",
required = True
)
]
)
@autodefer()
async def help_command(ctx: interactions.CommandContext, choice:int):
await asyncio.sleep(1)
await ctx.send("还没做出来!")
@bot.command(
name = "play",
description = "音乐播放模块",
#scope = scope_id
options = [
interactions.Option(
type = interactions.OptionType.STRING,
name = "name",
description = "网址 or BV (Todo 歌名)",
required = False
),
interactions.Option(
type = interactions.OptionType.STRING,
name = "source",
description = "渠道(暂时默认 Bilibili)",
required = False
),
]
)
@autodefer()
async def music_player(ctx: interactions.CommandContext, name:str, source="bilibili"):
# await asyncio.sleep(3)
# await ctx.send(g)
# voice_channel.play(discord.FFmpegPCMAudio(executable="ffmpeg", source="C:\\Users\\lemon\\Desktop\\lemon\\Discord-Bot\\function\\music\\download.mp3"))
random_number = random.randint(1,100)
color_value = embed_color_list[random_number % len(embed_color_list)]
# print(color_value)
#Todo bot connect to voice channel
#Todo bot play audio file
# WARNING 目前来看,这个 Discord 模块并没有 audio和 voice channel 的功能
# Github https://github.com/interactions-py/voice
url = "https://fastly.jsdelivr.net/gh/lemonsoldout/pictures@main/16708321871771.jpeg"
embeds = interactions.Embed(
title="音乐模块",
description="播放",
color = color_value,
image = interactions.EmbedImageStruct(
url = url,
height = 300,
width = 250,
),
# author = interactions.EmbedAuthor(
# name="柠檬汽水不要汽水#5149",
# ),
# footer=interactions.EmbedFooter(
# text="这是一个脚注",
# ),
fields = [interactions.EmbedField(
name ="歌曲名",
value = "歌手名-时长",
inline = False,
)],
)
await ctx.send(embeds=embeds)
@bot.command(
name = "stop",
description = "音乐模块",
)
@autodefer()
async def music_stop(ctx: interactions.CommandContext, source="bilibili"):
await asyncio.sleep(1)
random_number = random.randint(1,100)
color_value = embed_color_list[random_number % len(embed_color_list)]
# print(color_value)
#Todo audio stop
embeds = interactions.Embed(
title="音乐模块",
description="已停止",
color = color_value,
)
await ctx.send(embeds=embeds)
@bot.command(
name = "lol",
description = "英雄联盟账号相关信息查询",
#scope = scope_id,
options = [
interactions.Option(
type = interactions.OptionType.STRING,
name = "rule",
description = "输入数字: 1. 当前活跃匹配查询 2. 账户信息查询",
required = True
),
interactions.Option(
type = interactions.OptionType.STRING,
name = "username",
description = "账户名",
required = True
)
]
)
@autodefer()
async def league_command(ctx: interactions.CommandContext, rule:str, username:str):
msg = ""
if rule == "1":
await asyncio.sleep(5)
res = league.displayCurrentMatchPlayersInfo(username)
if res in "Target Account Not Found":
msg = "未找到用户!"
elif rule == "2":
await asyncio.sleep(5)
res = league.displayTargetAccRankInfo(username)
res = league.displayCurrentMatchPlayersInfo(username)
if res in "Target Account Not Found":
msg = "未找到用户!"
else:
msg = "用户名:\t{0}\n等级:\t{1}\n".format(res["用户名"], res["等级"])
if "SOLO" in res:
msg = msg + "单双:\t{0}\n赢:\t{1}\n输:\t{2}\n胜率:\t{3}".format(res["SOLO"]["单双"],res["SOLO"]["赢"],res["SOLO"]["输"],res["SOLO"]["胜率"])
else:
msg = "请输入正确的请求类型! 1. 当前活跃匹配查询 2. 账户信息查询"
await ctx.send(msg)
@bot.command(
name = "code",
description = "ChatGPT: 编程代码相关功能的封装",
#scope = scope_id,
options = [
interactions.Option(
type = interactions.OptionType.STRING,
name = "language",
description = "编程语言",
required = True
),
interactions.Option(
type = interactions.OptionType.STRING,
name = "question",
description = "问题",
required = True
),
]
)
@autodefer()
async def chatGPT_code(ctx: interactions.CommandContext, language:str, question:str):
await asyncio.sleep(5)
answer = chatGPT.AICodeCompletion(chatGPT_token, language, question)
msg = "问题:\t{0}\n语言:\t{1}\n".format(question, language) + "```{0}\n{1}\n```".format(language,answer)
random_number = random.randint(1,100)
color_value = embed_color_list[random_number % len(embed_color_list)]
# url = "https://fastly.jsdelivr.net/gh/lemonsoldout/pictures@main/16708321871771.jpeg"
embeds = interactions.Embed(
title="ChatGPT AI 模块",
# description = question,
color = color_value,
# image = interactions.EmbedImageStruct(
# url = url,
# height = 300,
# width = 250,
# ),
# author = interactions.EmbedAuthor(
# name="柠檬汽水不要汽水#5149",
# ),
# footer=interactions.EmbedFooter(
# text="这是一个脚注",
# ),
fields = [interactions.EmbedField(
name = "编程",
value = msg,
inline = False,
)],
)
await ctx.send(embeds=embeds)
@bot.command(
name = "chat",
description = "ChatGPT: AI 对话问答功能的封装",
#scope = scope_id,
options = [
interactions.Option(
type = interactions.OptionType.STRING,
name = "question",
description = "聊天问答",
required = True
)
]
)
@autodefer()
async def chatGPT_chat(ctx: interactions.CommandContext, question:str):
await asyncio.sleep(5)
answer = chatGPT.chatService(chatGPT_token, question)
msg = "{0}\n{1}".format(question, answer)
random_number = random.randint(1,100)
color_value = embed_color_list[random_number % len(embed_color_list)]
embeds = interactions.Embed(
title="ChatGPT AI 模块",
color = color_value,
fields = [interactions.EmbedField(
name = "聊天/问答",
value = msg,
inline = False,
)],
)
await ctx.send(embeds=embeds)
@bot.command(
name = "translate",
description = "ChatGPT: 翻译功能的封装",
#scope = scope_id,
options = [
interactions.Option(
type = interactions.OptionType.STRING,
name = "to",
description = "目标语言: 中, 英, 日, 韩, 法, 德",
required = True
),
interactions.Option(
type = interactions.OptionType.STRING,
name = "content",
description = "内容",
required = True
),
]
)
@autodefer()
async def chatGPT_translation(ctx: interactions.CommandContext, to:str, content:str):
await asyncio.sleep(5)
answer = chatGPT.translationService(chatGPT_token, to, content)
msg = "原文:\t{0}\nTo:\t{1}文\n{2}".format(content, to, answer)
random_number = random.randint(1,100)
color_value = embed_color_list[random_number % len(embed_color_list)]
embeds = interactions.Embed(
title="ChatGPT 翻译模块",
color = color_value,
fields = [interactions.EmbedField(
name = "翻译",
value = msg,
inline = False,
)],
)
await ctx.send(embeds=embeds)
# 启动 Discord 机器人
bot.start()