-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
219 lines (165 loc) · 6.35 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
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
import discord
import os
import requests
import json
from keep_alive import keep_alive
import yagmail
import asyncio
from discord.ext import commands
from discord.ext.commands import Bot, when_mentioned_or
from discord.ext.commands import CommandNotFound, CommandOnCooldown, MissingPermissions, MissingRequiredArgument, BadArgument, MissingAnyRole
import smtplib
from email.message import EmailMessage
os.system('pip install Pillow')
from PIL import Image, ImageFont, ImageDraw
FONT_FILE = ImageFont.truetype(r'font.ttf', 180)
FONT_COLOR = "#FFFFFF"
template = Image.open(r'cert.png')
WIDTH, HEIGHT = template.size
client = commands.Bot(command_prefix=when_mentioned_or('&gen '),description='A bot used to generate certificates')
game="ggk"
mlid=os.environ['id']
mp=os.environ['pas']
async def send_message(ctx, message):
await ctx.send(embed=discord.Embed(description=message))
def make_certificates(name):
'''Function to save certificates as a .png file'''
image_source = Image.open(r'cert.png')
draw = ImageDraw.Draw(image_source)
name_width, name_height = draw.textsize(name, font=FONT_FILE)
draw.text(((WIDTH - name_width) / 2, (HEIGHT - name_height) / 2 - 30), name, fill=FONT_COLOR, font=FONT_FILE)
image_source.save("./certificates/"+name +".png")
@client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
# await client.change_presence(status=discord.Status.online, activity=game)
#ping
@client.command(brief='inputting')
async def create(ctx):
def check1(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
# try:
await send_message(ctx, f"{ctx.author.mention}, Please enter the winners name")
try:
msg = await client.wait_for("message", check=check1, timeout=30)
name=msg.content
except asyncio.TimeoutError:
await ctx.send("Sorry, you didn't reply in time!")
return
name=name.lower()
finalname=name[0].upper()
for i in range(1, len(name)):
if(name[i-1]==" "):
finalname=finalname+name[i].upper()
else:
finalname=finalname+name[i]
make_certificates(finalname)
await msg.channel.send(file=discord.File("./certificates/"+finalname +".png"))
def check2(msg):
return msg.author == ctx.author and msg.channel == ctx.channel and msg.content.lower() in ["y", "n"]
await send_message(ctx, f"{ctx.author.mention}, Do you want to mail it, enter y for yes and n for no")
try:
msg = await client.wait_for("message", check=check2, timeout=30)
flag=msg.content.lower()
except asyncio.TimeoutError:
await ctx.send("Sorry, you didn't reply in time!")
return
if flag=='n':
await send_message(ctx,"Mail not send")
return
# To
mymail=EmailMessage()
mymail['Subject']="Certificate of Appreciation"
mymail['from']=mlid
mymail.set_content("Congrats!, your certificate is attached herewith")
server=smtplib.SMTP_SSL('smtp.gmail.com',465)
server.login(mlid, mp)
await send_message(ctx, f"{ctx.author.mention}, Enter the reciever's mail adress")
try:
message = await client.wait_for('message', timeout=30, check=check1)
recipient = str(message.content).split(' ')
except asyncio.TimeoutError:
await ctx.send("Sorry, you didn't reply in time!")
return
#CC
await send_message(ctx, f"{ctx.author.mention}, Enter the CC of recievers address and type 'na' if none")
try:
message = await client.wait_for('message', timeout=30, check=check1)
ccrec=[]
if(message.content.lower() != "na"):
ccrec = str(message.content).split(' ')
except asyncio.TimeoutError:
await ctx.send("Sorry, you didn't reply in time!")
return
#BCC
await send_message(ctx, f"{ctx.author.mention}, Enter the BCC of recievers address and type 'na' if none")
try:
message = await client.wait_for('message', timeout=30, check=check1)
bccrec=[]
if(message.content.lower() != "na"):
bccrec = str(message.content).split(' ')
except asyncio.TimeoutError:
await ctx.send("Sorry, you didn't reply in time!")
return
mymail['To']=", ".join(recipient)
mymail['Cc']=", ".join(ccrec)
mymail['Bcc']=", ".join(bccrec)
with open("./certificates/"+finalname +".png","rb") as fl:
data=fl.read()
mymail.add_attachment(data,maintype="application",subtype="png",filename=name+".png")
try:
server.send_message(mymail)
await send_message(ctx, "mail sent successfully")
except:
await send_message(ctx, "Some error occured :( Please try again")
del mymail
server.quit()
# make_certificates(message)
# except:
# await ctx.send(f"You took too long to type{ctx.author.mention}")
# return
@client.command(brief='Measure delays')
async def ping(ctx):
await ctx.send(':ping_pong: Pong! ~' + str(round(client.latency * 1000, 2)) + " ms")
# @client.event
# async def on_message(message):
# if message.author == client.user:
# return
# if message.content.startswith('&gen'):
# name=message.content.split("&gen ",1)[1]
# name=name.lower()
# finalname=name[0].upper()
# for i in range(1, len(name)):
# if(name[i-1]==" "):
# finalname=finalname+name[i].upper()
# else:
# finalname=finalname+name[i]
# make_certificates(finalname)
# await message.channel.send(file=discord.File("./certificates/"+finalname +".png"))
# s="Do you want to send it via email"
# await message.channel.send(s)
@ client.event
async def on_command_error(ctx: commands.Context, error: Exception):
if isinstance(error, CommandNotFound):
pass
elif isinstance(error, CommandOnCooldown):
pass
elif isinstance(error, BadArgument) or isinstance(error, MissingRequiredArgument):
command = ctx.command
usage = f".{str(command)} "
params = []
for key, value in command.params.items():
if key not in ['self', 'ctx']:
params.append(f"[{key}]" if "NoneType" in str(
value) else f"<{key}>")
usage += ' '.join(params)
await ctx.send(f"Usage: **{usage}**")
elif isinstance(error, MissingPermissions) or isinstance(error, MissingAnyRole):
await ctx.send(f"{str(error)}")
else:
print(f"{ctx.author.id} {ctx.guild.id} {ctx.message.content}")
print(error)
await ctx.send(error)
tok = os.environ['TOKEN']
keep_alive()
client.run(tok)