-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
141 lines (108 loc) · 3.94 KB
/
main.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
138
139
140
import discord
import yaml
import json
import os
import imagegetter
import image_map
import asyncio
import random
with open('config.yml', 'r', encoding='utf8') as configfile:
SETTINGS = yaml.load(configfile, yaml.Loader)
# deal with settings
if SETTINGS['send-as-attachment'] & SETTINGS['download-files']:
if not os.path.isdir('./img'):
try:
os.mkdir('./img')
except:
print('failed to make storage dir, quitting')
exit()
if SETTINGS['download-at-startup']:
asyncio.run(imagegetter.download_all())
def reload():
global message_mappings
with open('mygo.json', 'r', encoding='utf8') as mappingfile:
message_mappings = json.load(mappingfile)
message_mappings = {}
reload()
# bot section
with open('token.txt', 'r', encoding='utf8') as tokenfile:
bot_token = tokenfile.read().strip()
intents = discord.Intents.default()
# intents.message_content = True
bot = discord.Bot(intents=intents)
def get_images(message: str) -> set[str]:
message = message.lower()
imgs = set()
if not message:
return imgs
for key in message_mappings:
if key in message:
value = message_mappings[key]
imgs.update(value['value'])
for name in image_map.get_all_names():
if message in name:
imgs.add(name)
return imgs
@bot.event
async def on_message(ctx: discord.Message):
# ignore bots
if ctx.author.bot:
return
# get images
imgs = get_images(ctx.content)
imgs = list(imgs)
if len(imgs) > 0:
img = imgs[random.randint(0, len(imgs)-1)]
if SETTINGS['send-as-attachment']:
file = await imagegetter.get_file_handle(img)
if file is None:
print(f'Could not send file {img}')
fileObject = discord.File(file, f'{img}.jpg')
try:
await ctx.channel.send(file=fileObject)
except discord.errors.Forbidden:
print("permission denied when attempting to send message")
else:
try:
await ctx.channel.send(imagegetter.get_link(img))
except discord.errors.Forbidden:
print("permission denied when attempting to send message")
if await bot.is_owner(ctx.author):
if ctx.content.strip() == "春日影":
reload()
print("為什麼要演奏春日影!")
# slash commands section
mygo = bot.create_group('mygo')
@mygo.command(description="搜尋表情包")
async def search(ctx: discord.ApplicationContext, message: discord.Option(str, name="訊息")):
# get images
imgs = get_images(message)
imgs = list(imgs)
if len(imgs) > 0:
img = imgs[random.randint(0, len(imgs)-1)]
if SETTINGS['send-as-attachment']:
file = await imagegetter.get_file_handle(img)
if file is None:
print(f'Could not send file {img}')
fileObject = discord.File(file, f'{img}.jpg')
try:
await ctx.respond(file=fileObject)
except discord.errors.Forbidden:
print("permission denied when attempting to send message")
else:
try:
await ctx.respond(imagegetter.get_link(img))
except discord.errors.Forbidden:
print("permission denied when attempting to send message")
else:
# nothing found for this message
await ctx.respond("沒有找到表情包", ephemeral=True)
# help command
@mygo.command(description='幫助')
async def help(ctx: discord.ApplicationContext):
await ctx.respond('https://github.com/felix920506/mygobot/blob/main/help.md', ephemeral=True)
# about command
@mygo.command(description='關於')
async def about(ctx: discord.ApplicationContext):
await ctx.respond('https://github.com/felix920506/mygobot', ephemeral=True)
bot.run(bot_token)