-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
340 lines (293 loc) · 11.7 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
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
import os
import discord
from dotenv import load_dotenv
from discord.ext import commands
import datetime
import game
import asyncio
import random
from time import time as currtime
import urllib.request
import urllib.parse
from PIL import Image
import io
from settings import *
import os
intents = discord.Intents.default()
intents.members = True
load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
pg = game.ProblemGenerator()
bot = commands.Bot(command_prefix='$', intents = intents)
bot_channel = None #current bot vc
start_time = 0
user_time = None
timer_on = False
rns_started = False
queue = []
jacklist = ['jack.JPG','jack2.JPG','jackJPG','jack4.JPG','jack5.JPG','jack6.JPG','jack7.JPG','jack8.PNG','jack9.JPG','jack10.JPG','jack11.JPG']
#create a list of the queued timers?
@bot.command()
async def ping(ctx):
await ctx.send(bot.latency)
#use of embed
_embed = discord.Embed(name="Embed", description="Description of Embed")
_embed.add_field(name=f"When the", value=house_of_rising_sun)
await ctx.send(embed=_embed)
await ctx.send(file= discord.File('test.gif'))
@bot.command()
async def jacques(ctx):
await ctx.send(file = discord.File(random.choice(jacklist)))
@bot.command()
async def rojo(ctx, num: int):
global bot_channel
#while queue:
if bot_channel is not None: # if bot in vc
if len(queue) < 2: await bot_channel.disconnect()
else: queue.append(num)
if ctx.message.author.voice.channel is not None:
channel = ctx.message.author.voice.channel
await ctx.send(f'User {ctx.message.author.mention} is in channel: {channel.name}')
bot_channel = await channel.connect()
print(bot_channel)
await bot_channel.play(discord.FFmpegPCMAudio(ROJO_MUSIC[num-2]))
else:
await ctx.send(':x: User is not in a channel.')
@bot.command()
async def test(ctx,*args): #bot returns user message as test function
if len(args) == 0:
await ctx.send('bruh')
else:
await ctx.send(" ".join(args))
await ctx.send(file= discord.File(random.choice(['clowntoine.png','twon.png','youremad.png','tf.jpg','tf2.jpg','tf3.jpg','tf4.jpg'])))
@bot.command()
async def quit_rns(ctx):
global rns_started
if rns_started:
rns_started = False
@bot.command()
async def start_rns(ctx, type = None):
global rns_started
rns_started = True
while rns_started:
start = currtime()
if type == None:
random_method = random.choice(PG_METHODS)
question = random_method(pg)
_embed = discord.Embed(name="Prompt", description=f"Type: {random_method.__name__}")
_embed.add_field(name=f"{question.prompt} = ", value=f"Answer in the next message {ctx.message.author.mention}.")
await latex(ctx, question.prompt)
await ctx.send(embed=_embed)
# await ctx.send(f"Type: {random_method.__name__}\n{question.prompt}=")
else:
pg_methods_string = '\n-'.join(list(func.__name__.strip('pg.') for func in PG_METHODS))
try:
question = getattr(pg, type)()
await ctx.send(f"{question.prompt}=")
except:
await ctx.send(f":x: Problem type not recognized. Available problem types: \n-{pg_methods_string}")
# This will make sure that the response will only be registered if the following
# conditions are met:
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
msg = await bot.wait_for("message", check=check)
elapsed_time = round(currtime() - start, 3)
await ctx.send(f"It only took you {elapsed_time} seconds brickhead!")
try:
# temp = int(msg.content.lower())
if msg.content.lower() == f"{question.solution}":
await ctx.send("when u get it right :100:")
else:
await ctx.send(
f"Wrong! You ragamuffin! Be better next time smh! Answer is {question.solution}")
except:
await ctx.send(f"Enter a number you buffoon! :100:")
@bot.command()
async def rns(ctx, type=None):
if not rns_started:
start = currtime()
if type == None:
random_method = random.choice(PG_METHODS)
question = random_method(pg)
_embed = discord.Embed(name="Prompt", description=f"Type: {random_method.__name__}")
_embed.add_field(name=f"{question.prompt} = ", value=f"Answer in the next message {ctx.message.author.mention}.")
await latex(ctx, question.prompt)
await ctx.send(embed=_embed)
# await ctx.send(f"Type: {random_method.__name__}\n{question.prompt}=")
else:
pg_methods_string = '\n-'.join(list(func.__name__.strip('pg.') for func in PG_METHODS))
try:
question = getattr(pg,type)()
await latex(ctx, question.prompt)
await ctx.send(f"{question.prompt}=")
except: await ctx.send(f":x: Problem type not recognized. Available problem types: \n-{pg_methods_string}")
# This will make sure that the response will only be registered if the following
# conditions are met:
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel
msg = await bot.wait_for("message", check=check)
elapsed_time = round(currtime() - start,3)
await ctx.send(f"It only took you {elapsed_time} seconds brickhead!")
try:
# temp = int(msg.content.lower())
if msg.content.lower() == f"{question.solution}":
await ctx.send("when u get it right :100:")
else:
await ctx.send(f"Wrong! You ragamuffin! Be better next time smh! Answer is {question.solution}")
except:
await ctx.send(f"Enter a number you buffoon! :100:")
else:
await ctx.send(f":x: start_rns already running!")
@bot.command()
async def timer(ctx, time : int = 30, type='s'): #timer with bot alarm
global bot_channel,start_time, user_time, timer_on
if not timer_on:
timer_on = True
try:
if type in ['m','min','minutes','minute']:
user_time = time * 60
type = 'minutes'
if user_time == 60:
type = 'minute'
elif type in ['h','hour','hours']:
user_time = time * 3600
type = 'hours'
if user_time == 3600:
type = 'hour'
else:
type = 'seconds'
user_time = time
await ctx.send(f'Timer started for {time} {type}')
start_time = currtime()
while user_time - (currtime()-start_time) > 0:
await asyncio.sleep(0.001)
# print(user_time - (currtime()-start_time))
if not timer_on:
await ctx.send("Timer stopped.")
return
timer_on = False
await ctx.send(f"Timer done. {ctx.message.author.mention}")
try:
channel = ctx.message.author.voice.channel
await ctx.send('User is in channel: ' + channel.name)
if channel is not None:
bot_channel = await channel.connect()
print(bot_channel,user_time)
await bot_channel.play(discord.FFmpegPCMAudio(os.path.join('sounds',"alarm.mp3")))
else:
await ctx.send(':x: User is not in a channel.')
except:
pass
except: await ctx.send(f':x: Wrong syntax\n?start_timer [time: integer] [type: s, m, h]')
else:
await ctx.send(f':x: Timer already running.')
@bot.command()
async def stop(ctx): #force bot to leave user's vc
global bot_channel, timer_on
if timer_on:
timer_on = False
if bot_channel is not None:
await bot_channel.disconnect()
bot_channel = None
@bot.command()
async def elapsed(ctx): #force bot to leave user's vc
global start_time,user_time, timer_on
if timer_on:
formatted_time_left=round(int((user_time))-(currtime()-start_time),1)
if formatted_time_left > 60 and formatted_time_left < 3600:
unit = 'minutes'
formatted_time_left = round(formatted_time_left / 60, 1)
elif formatted_time_left > 3600:
unit = 'hours'
formatted_time_left = round(formatted_time_left / 3600,1)
elif formatted_time_left < 60:
unit = 'seconds'
await ctx.send(f'{formatted_time_left} {unit} left.')
else: await ctx.send(f':x: No timer running.')
@bot.command()
async def michelle(ctx):
await ctx.send('7 > 5')
await ctx.send(file=discord.File('youremad.png'))
@bot.command()
async def nnn(ctx):
guild = discord.utils.find(lambda g: g.name == GUILD, bot.guilds)
members = '\n - '.join([member.name for member in guild.members])
await ctx.send(f'{members}')
# for guild in bot.guilds:
# for member in guild.members:
# await ctx.send(member)
#@commands.has_permissions(administrator=True)
@bot.command()
async def clear(ctx,amount = 1):
def check(msg):
return msg.author == ctx.author and msg.channel == ctx.channel and msg.content.lower() in ['y','n']
if amount > 1000:
await ctx.send(f'Are you sure you want to clear {amount} messages? [y/n]')
msg = await bot.wait_for("message",check = check)
if msg.content.lower() == 'y':
await ctx.channel.purge(limit=amount)
else:
await ctx.send('Clear command canceled.')
else:
await ctx.channel.purge(limit = amount)
@bot.event
async def on_ready():
guild = discord.utils.find(lambda g: g.name == GUILD, bot.guilds)
print(
f'{bot.user} is connected to the following guild:\n'
f'{guild.name}(id: {guild.id})'
)
members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')
### DEBUGGING ###
# @bot.command()
# async def join(ctx): #force bot to join user's vc
# global bot_channel
# channel = ctx.message.author.voice.channel
# if bot_channel is None:
# bot_channel = await channel.connect()
# else:
# await ctx.send(':x: Already connected to a voice channel.')
#
@bot.command()
async def leave(ctx): #force bot to leave user's vc
global bot_channel, timer_on
if bot_channel is not None:
await bot_channel.disconnect()
bot_channel = None
else:
await ctx.send(':x: Bot is not in a voice channel.')
###LATEX FORMATTING
async def generate_file(dpi, tex):
MARGIN = 20
URL = 'https://latex.codecogs.com/gif.latex?{}'
query = f'\\dpi{{{dpi}}} \\bg_white {tex}'
filename = '{}.png'.format(random.randint(1, 1000))
# query = TEMPLATE.format(dpi, tex)
print(query)
url = URL.format(urllib.parse.quote(query))
bytes = urllib.request.urlopen(url).read()
img = Image.open(io.BytesIO(bytes))
old_size = img.size
new_size = (old_size[0] + MARGIN, old_size[1] + MARGIN)
new_img = Image.new("RGB", new_size, (255, 255, 255))
new_img.paste(img, (int(MARGIN / 2), int(MARGIN / 2)))
img_bytes = io.BytesIO()
new_img.save(img_bytes, 'png')
img_bytes.seek(0)
return img_bytes
@bot.command()
async def latex(ctx, *message):
await ctx.trigger_typing()
dpi = 200
tex = ''
print('{}: dpi={} tex={}'.format(ctx.author, dpi, tex))
try: bytes = await generate_file(dpi, ''.join(message))
except:
await ctx.send(':x: Invalid syntax!')
return
filename = '{}.png'.format(random.randint(1, 1000))
# print(type(bytes))
await ctx.send(file=discord.File(bytes, filename = filename))
bot.run(TOKEN)