-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (67 loc) · 2.28 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
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)
@bot.event
async def on_message(ctx: discord.Message):
if ctx.author.bot:
return
imgs = set()
msg = ctx.content.lower()
for key in message_mappings:
if key in msg:
value = message_mappings[key]
imgs.update(value['value'])
for name in image_map.get_all_names():
if msg in name:
imgs.add(name)
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 {name}')
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("為什麼要演奏春日影!")
bot.run(bot_token)