-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbot.py
61 lines (44 loc) · 1.52 KB
/
bot.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
import os
import discord
import requests
from astolphise import Astolphise
import numpy as np
import cv2
import keras.preprocessing.image
#load_dotenv()
TOKEN = 'DISCORD TOKEN'
GUILD = 'DISCORD GUILD'
a = Astolphise()
client = discord.Client()
@client.event
async def on_ready():
# for guild in client.guilds:
# if guild.name == GUILD:
# break
# guild = discord.utils.find(lambda g: g.name == GUILD, client.guilds)
guild = discord.utils.get(client.guilds, name=GUILD)
print(
'ready'
)
@client.event
async def on_message(message):
img_data = requests.get(message.attachments[0].url).content
with open('temp/image_name.jpg', 'wb') as handler:
handler.write(img_data)
SIZE = 160
img = cv2.imread('temp/image_name.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # open cv reads images in BGR format so we have to convert it to RGB
img = cv2.resize(img, (SIZE, SIZE)) #resizing image
img = img.astype('float32') / 255.0
test_img = keras.preprocessing.image.img_to_array(img)
test_img = np.array(test_img)
test_img = np.reshape(test_img,(-1,SIZE,SIZE,3))
Prediction = a.predict(test_img)
maxEle = max(Prediction[0])
maxIndex = np.where(maxEle == Prediction)
#send message using discord bot
if maxIndex[1][0] == 0:
await message.channel.send(message.author.mention + ' ' + 'Yay, astolpho :eggplant:')
else:
await message.channel.send(message.author.mention + ' ' + 'Sad')
client.run(TOKEN)