-
Notifications
You must be signed in to change notification settings - Fork 2
/
Dmemer.py
385 lines (341 loc) · 11.4 KB
/
Dmemer.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import discord
import psycopg2
import os
import random
import asyncio
import requests
from urllib.parse import unquote
from itertools import cycle
from discord.ext import commands, tasks
bot = commands.Bot(command_prefix=['n!', 'N!'], case_insensitive = True)
status = cycle(['Looking at the records', 'transferring money', 'Waiting for drama'])
@tasks.loop(seconds=2)
async def change_status():
await bot.change_presence(activity=discord.Game(next(status)))
def connectsql():
DATABASE_URL = os.environ['DATABASE_URL']
global conn
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
global cur
cur = conn.cursor()
ownerid = [708645600165625872, 419742289188093952]
@bot.event
async def on_ready():
download_questions()
change_status.start()
drop.start()
global antinsfw
antinsfw = False
print('Bot Online')
@bot.command()
async def ping(ctx):
await ctx.send(f"Pong! {round(bot.latency * 1000)} ms")
@commands.has_permissions(administrator=True)
@bot.command()
async def nonsfw(ctx, option):
global antinsfw
if option == 'yes':
antinsfw = True
await ctx.send('Beginning to cleanise this chat')
elif option == 'no':
antinsfw = False
await ctx.send('Okay, entering the dark side')
@commands.has_permissions(administrator=True)
@bot.command()
async def init(ctx):
connectsql()
cur.execute("CREATE TABLE data (id BIGINT, username TEXT, amount INTEGER)")
conn.commit()
conn.close()
connectsql()
cur.execute("CREATE TABLE trivia (id BIGINT, category TEXT, difficulty TEXT, question TEXT, correct TEXT, wrong1 TEXT, wrong2 TEXT, wrong3 TEXT)")
conn.commit()
conn.close()
connectsql()
message = ''
for guild in bot.guilds:
for member in guild.members:
if member.bot:
pass
else:
targetname = repr(member.name + "#" + member.discriminator)
cur.execute(f"INSERT INTO data (id, username, amount) VALUES ({member.id}, {targetname}, 5000)")
message = message + '\n' + (f"Member {targetname} has been added to the database")
await ctx.send(message)
conn.commit()
conn.close()
download_questions()
await ctx.send('Questions added to database')
@commands.has_permissions(administrator=True)
@bot.command()
async def wipe(ctx):
connectsql()
cur.execute("DROP TABLE IF EXISTS data")
cur.execute("DROP TABLE IF EXISTS trivia")
conn.commit()
conn.close()
await ctx.send("Table Wiped")
@bot.command()
async def rich(ctx):
connectsql()
currentdata = ''
for guild in bot.guilds:
cur.execute(f"SELECT * FROM data ORDER BY amount DESC")
rows = cur.fetchall()
for row in rows:
if row is None:
break
currentdata = currentdata + "\n" + (f"Name: {row[1]}\nBalance: {row[2]}")
await ctx.send(currentdata)
conn.close()
@bot.command()
async def bal(ctx, target : discord.Member = None):
if target is None:
target = ctx.author
connectsql()
embed=discord.Embed(color=0xffff00)
message = f"Balance of {target}"
cur.execute(f"SELECT * FROM data WHERE id = {target.id}")
data = cur.fetchone()
amount = data[2]
embed.add_field(name=message, value=amount, inline=True)
embed.set_footer(text="Bot made by Xx_THK_xX")
await ctx.send(embed=embed)
conn.close()
@bot.command()
@commands.cooldown(1, 10, commands.BucketType.user)
async def rob(ctx, target : discord.Member):
connectsql()
attackerid = ctx.author.id
user = target
if user.id == attackerid:
await ctx.send("You cannot rob yourself you dumb fuck")
return
cur.execute(f"SELECT * FROM data WHERE id = {user.id}")
row = cur.fetchone()
if row is None:
await ctx.send("Unable to find target")
targetbal = row[2]
cur.execute(f"SELECT * FROM data WHERE id = {attackerid}")
row = cur.fetchone()
if row is None:
await ctx.send("Unable to find your profile, are you sure you're enrolled?")
attackerbal = row[2]
successmin = 50
roll1 = random.randint(1,100)
if roll1 >= successmin:
stolenpercent = random.randint(3, 35)
stolen = round(targetbal * (stolenpercent / 100))
remain = targetbal - stolen
attackernewbal = attackerbal + stolen
cur.execute(f"UPDATE data SET amount = {remain} WHERE id = {user.id}")
cur.execute(f"UPDATE data SET amount = {attackernewbal} WHERE id = {attackerid}")
await ctx.send(f"Successful Steal! You've swooped {stolen}, or {stolenpercent}% from {user}")
else:
roll2 = random.randint(1,100)
death = 10
if roll2 <= death:
cur.execute(f"UPDATE data SET amount = 0 WHERE id = {attackerid}")
await ctx.send("Oh fuck, you tripped over a banana and hit your head in a pile of shit, and now you're dead")
else:
await ctx.send("You Failed the rob, noooob")
conn.commit()
conn.close()
@rob.error
async def rob_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(f"You're robbing way too much from these noobs, you can rob them in {round(error.retry_after,2)}s")
def download_questions():
print('Downloading questions from Open Trivia DB...')
api_url = 'https://opentdb.com/api.php?amount=50&type=multiple&encode=url3986'
r = requests.get(api_url)
connectsql()
cur.execute("DELETE FROM trivia")
conn.commit()
conn.close()
connectsql()
api_result = r.json()
questions = api_result['results']
id = 0
for q in questions:
id = id + 1
category = repr(unquote(q['category']))
difficulty = repr(unquote(q['difficulty']))
question = repr(unquote(q['question']))
correctans = repr(unquote(q['correct_answer']))
badans1 = repr(unquote(q['incorrect_answers'][0]))
badans2 = repr(unquote(q['incorrect_answers'][1]))
badans3 = repr(unquote(q['incorrect_answers'][2]))
cur.execute(f"INSERT INTO trivia (id, category, difficulty, question, correct, wrong1, wrong2, wrong3) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)", (id, category, difficulty, question, correctans, badans1, badans2, badans3))
conn.commit()
conn.close()
def getquestion():
connectsql()
questionid = random.randint(1,50)
cur.execute(f"SELECT * FROM trivia WHERE id = {questionid}")
data = cur.fetchone()
cat = data[1]
diff = data[2]
diff = diff.replace("'", "")
question = data[3]
correctans = data[4]
correctans = correctans.replace("'", "")
wrongans1 = data[5]
wrongans2 = data[6]
wrongans3 = data[7]
allans = [correctans, wrongans1, wrongans2, wrongans3]
random.shuffle(allans)
ans1 = "A)" + allans[0]
ans2 = "B)" + allans[1]
ans3 = "C)" + allans[2]
ans4 = "D)" + allans[3]
answers = (f"{ans1} \n {ans2} \n {ans3} \n {ans4}")
letternum = 0
for a in allans:
letternum = letternum + 1
if correctans in a:
break
if letternum == 1:
letter = "A"
elif letternum == 2:
letter = "B"
elif letternum == 3:
letter = "C"
elif letternum == 4:
letter = "D"
response = "Category:" + cat.replace("'", "") + "\n" + "Difficulty:" + diff.replace("'", "") + "\n" + "Question:" + question.replace("'", "") + "\n " + answers.replace("'", "")
return response, correctans, letter, diff
@tasks.loop(seconds=60)
async def drop():
chance = random.randint(1,100)
if chance >= 99:
gamechannel = bot.get_channel(724274805381267498)
death = random.randint(1,100)
await gamechannel.send("It's time for another trivia question!")
question, correct, letter, diff = getquestion()
global amount
if diff == 'easy':
amount = random.randint(0,2000)
elif diff == 'medium':
amount = random.randint(1000,5000)
elif diff == 'hard':
amount = random.randint(4000,10000)
global flagged
flagged = []
def check(m):
if (str(m.content).upper() == str(correct).upper() or str(m.content).upper() == letter) and (m.author.id not in flagged and m.author.id != 710363488182206465):
return True
else:
if m.author.id not in flagged:
flagged.append(m.author.id)
await gamechannel.send(question)
try:
answer = await bot.wait_for('message', check=check, timeout = 10.0)
except asyncio.TimeoutError:
await gamechannel.send(f"Oh well, look like no one's smart enough, the answer is actually {correct}")
else:
connectsql()
if death == 1:
newbal = 0
cur.execute(f"UPDATE data SET amount = {newbal} WHERE id = {answer.author.id}")
await gamechannel.send("Welp, you answered correctly, but some other person ran over you while you're going home so you died instead, suck to be you")
conn.commit()
conn.close()
else:
cur.execute(f"SELECT * FROM data WHERE id = {answer.author.id}")
data = cur.fetchone()
currentbal = data[2]
newbal = currentbal + amount
cur.execute(f"UPDATE data SET amount = {newbal} WHERE id = {answer.author.id}")
await gamechannel.send(f"Smart guy, you got {amount}")
conn.commit()
conn.close()
finally:
if len(flagged) >=1:
connectsql()
punished = []
for victim in flagged:
cur.execute(f"SELECT * FROM data WHERE id = {victim}")
data = cur.fetchone()
bal = data[2]
newbal = bal - round(amount / 4)
user = await bot.fetch_user(victim)
punished.append(user.mention)
cur.execute(f"UPDATE data SET amount = {newbal} WHERE id = {victim}")
await gamechannel.send(f"Bad luck to: \n {punished}, you all lose {round(amount/4)}, gg u suck")
conn.commit()
conn.close()
else:
pass
else:
pass
@commands.has_permissions(administrator=True)
@bot.command()
async def dropnow(ctx):
if True:
gamechannel = bot.get_channel(724274805381267498)
death = random.randint(1,100)
await gamechannel.send('Getting Question...')
question, correct, letter, diff = getquestion()
if diff == 'easy':
amount = random.randint(0,2000)
elif diff == 'medium':
amount = random.randint(1000,5000)
elif diff == 'hard':
amount = random.randint(4000,10000)
global flagged
flagged = []
def check(m):
if (str(m.content).upper() == str(correct).upper() or str(m.content).upper() == letter) and m.author.id not in flagged:
return True
else:
if m.author.id not in flagged and m.author.id != 710363488182206465:
flagged.append(m.author.id)
await gamechannel.send(question)
try:
answer = await bot.wait_for('message', check=check, timeout = 10.0)
except asyncio.TimeoutError:
await gamechannel.send(f"Oh well, look like no one's smart enough, the answer is actually {correct}")
else:
connectsql()
if death == 1:
newbal = 0
cur.execute(f"UPDATE data SET amount = {newbal} WHERE id = {answer.author.id}")
await gamechannel.send("Welp, you answered correctly, but some other person ran over you while you're going home so you died instead, suck to be you")
conn.commit()
conn.close()
else:
cur.execute(f"SELECT * FROM data WHERE id = {answer.author.id}")
data = cur.fetchone()
currentbal = data[2]
newbal = currentbal + amount
cur.execute(f"UPDATE data SET amount = {newbal} WHERE id = {answer.author.id}")
await gamechannel.send(f"Smart guy, you got {amount}")
conn.commit()
conn.close()
finally:
if len(flagged) >=1:
connectsql()
punished = []
for victim in flagged:
cur.execute(f"SELECT * FROM data WHERE id = {victim}")
data = cur.fetchone()
bal = data[2]
newbal = bal - round(amount/4)
user = await bot.fetch_user(victim)
punished.append(user.mention)
cur.execute(f"UPDATE data SET amount = {newbal} WHERE id = {victim}")
await gamechannel.send(f"Bad luck to: \n {punished}, you all lose {round(amount/4)}, gg u suck")
conn.commit()
conn.close()
else:
pass
@bot.event
async def on_message(message):
global antinsfw
if message.author.id == 285480424904327179 and antinsfw:
await message.delete()
else:
await bot.process_commands(message)
token = os.environ.get('BOT_TOKEN')
bot.run(token)