forked from iethree/wan-party-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
280 lines (220 loc) Β· 7.58 KB
/
commands.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
from discord.ext import commands
from discord.ext.commands import MemberConverter
import random
from giphy import *
import re
from get_error_message import (
get_error_message_for_fun_times_everyone_loves_error_messages,
)
import sqlite3
import socket
DATABASE = "wanparty.db"
bot = commands.Bot(command_prefix="/")
INSTRUCTIONS = {
"+": (lambda x, y: x + y),
"-": (lambda x, y: x - y),
"*": (lambda x, y: x * y),
"/": (lambda x, y: x / y),
"^": (lambda x, y: x ** y),
"β½": (lambda x, y: 42),
}
INS_RE = "[" + re.escape("".join(INSTRUCTIONS.keys())) + "]"
DICE_RE = r"(\d+)\s*d\s*(\d+)\s*(" + INS_RE + r"\s*\d+)?"
@bot.command()
async def bet(ctx, bet: int, guess: str):
await ctx.send(random_gif("vegas"))
message = ""
user_id = ctx.message.author.id
conn = sqlite3.connect(DATABASE)
bet_cursor = conn.cursor()
user = bet_cursor.execute(
"SELECT * FROM wanbux WHERE id = ?", (user_id,)
).fetchone()
is_naughty = (
bet_cursor.execute(
"SELECT * FROM naughty_list where id = ?", (user_id,)
).fetchone()
is not None
)
if bet <= 0 and not is_naughty:
await ctx.send("Cute")
return
if user is None:
message += f"Welcome to the WAN Casino {ctx.message.author.mention}."
message += " Have 5 Wanbux on the house.\n"
bet_cursor.execute(
"INSERT INTO wanbux(id, balance) VALUES(?, ?)",
(
user_id,
5,
),
)
balance = user[1] if user is not None else 5
if bet > balance:
if not is_naughty:
await ctx.send(
f"Your bet is too high. I'm going to assume you're betting "
f"everything you have, which is {balance} wanbux.\n"
)
bet = balance
flip = random.choice(["heads", "tails"])
message += f"I flipped {flip}."
is_win = flip == guess.strip().lower()
new_balance = balance + bet if is_win else balance - bet
message += f" You {'won' if is_win else 'lost'} {bet} wanbux!"
message += f" You have {new_balance} wanbux now."
if new_balance == 0:
message += " You're broke now! Get lost, ya bum."
bet_cursor.execute(
"UPDATE wanbux SET balance = ? WHERE id = ?", (new_balance, user_id)
)
conn.commit()
conn.close()
await ctx.send(message)
# fuck around and find out
@bot.command(name="yeet")
async def yeet_bet(ctx, guess: str):
balance = await get_balance(ctx.message.author.id)
await ctx.send("https://gph.is/g/4wMRo3n")
bet_amount = random.randrange(1, balance[0])
await bet(ctx, bet_amount, guess)
async def get_balance(user_id):
conn = sqlite3.connect(DATABASE)
bal_cursor = conn.cursor()
row = bal_cursor.execute(
"SELECT balance FROM wanbux WHERE id = ?", (user_id,)
).fetchone()
return row
async def update_balance(user_id, update):
conn = sqlite3.connect(DATABASE)
bal_cursor = conn.cursor()
updated_row = bal_cursor.execute(
"UPDATE wanbux set balance = ? WHERE id = ?", (update, user_id)
)
conn.commit()
conn.close()
return updated_row
# set balance command for testing purposes
@bot.command(name="devset")
async def dev_set(ctx, amount: int):
await update_balance(ctx.message.author.id, amount)
balance = await get_balance(ctx.message.author.id)
await ctx.send(f"Your balance has been set to {balance[0]}")
@bot.command(name="balance")
async def eval_balance(ctx):
balance = await get_balance(ctx.message.author.id)
if balance is not None:
await ctx.send(f"{ctx.message.author.mention}'s balance is {balance[0]} wanbux")
return
await ctx.send(f"{ctx.message.author.mention} doesn't have a balance")
@bot.command(name="myid")
async def my_id(ctx):
await ctx.send(ctx.message.author.id)
@bot.command()
async def beg(ctx):
row = await get_balance(ctx.message.author.id)
if row is not None and row[0] == 0:
await update_balance(ctx.message.author.id, 1)
await ctx.send(
f"Try not to spend it all in one place {ctx.message.author.mention} π"
)
elif row is not None and row[0] > 0:
await ctx.send("π")
else:
await ctx.send(get_error_message_for_fun_times_everyone_loves_error_messages())
@bot.command()
async def rob(ctx):
[balance] = await get_balance(ctx.message.author.id)
for victim in ctx.message.mentions:
[stolen] = await get_balance(victim.id)
balance += stolen
await update_balance(victim.id, 0)
await update_balance(ctx.message.author.id, balance)
# @bot.command
# async def pay(ctx, member: MemberConverter):
# # etc you get it
@bot.command()
async def rollin(ctx):
await ctx.send("Aww yeah π")
@bot.command()
async def puppet(ctx, channel_name, msg):
channels = bot.get_all_channels()
for channel in channels:
if channel.name == channel_name:
await channel.send(msg)
def nice_dice(dice):
return dice.strip().lower()
def do_the_thing(dice):
rolls = []
result = 0
for (dice_count, face_count, math) in re.findall(DICE_RE, dice):
sub_result = 0
for _ in range(int(dice_count)):
roll = random.randint(1, int(face_count))
rolls.append(str(roll))
sub_result += roll
if math != "":
sub_result = INSTRUCTIONS[math[0]](sub_result, int(math[1:]))
result += sub_result
return rolls, result
@bot.command()
async def roll(ctx, *, arg=None):
"""XdY+Z AdB+C etc"""
if arg is None:
await ctx.send(str(random.randint(1, 100)))
return
instruction = ""
rolls, result = [], 0
try:
nice_arg = nice_dice(arg)
if not re.search("(" + DICE_RE + r"\s*)*", nice_arg):
raise Exception("haha")
rolls, result = do_the_thing(nice_arg)
except Exception as e: # yolo
await ctx.send(get_error_message_for_fun_times_everyone_loves_error_messages())
print(e)
return
await ctx.send("I rolled: " + " ".join(rolls) + ", result: " + str(result))
@bot.command()
async def haiku(ctx, arg=None):
"""
Call as /haiku @targetuser.
Will generate a markov chain haiku using the
channel's history from that user's comments.
"""
from markov_haiku_discord import gen_haiku
# trust no one
if (arg==None or len(arg) > 1):
await ctx.send("I don't understand.")
# identify which user to search for
this_guild = ctx.message.guild
this_channel = ctx.message.channel
target_user = ctx.message.raw_mentions[0]
# pull users comments
haiku_list = []
async for m in ctx.message.channel.history(limit=2000):
if m.author.id == target_user:
content = m.content.split(' ')
if len(content) > 2:
haiku_list.append(' '.join(content[:]))
haiku_string = ' '.join(haiku_list)
# train haiku_bot on comments & generate a haiku
gen_haiku(haiku_string)
await ctx.send("Sorry, hauiku_bot is new at this and currently under construction. Please be patient.")
return
@bot.command()
async def sql(ctx, *, arg=None):
if "drop table" in arg.lower():
result = "π"
else:
try:
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute(arg)
result = cursor.fetchall()
result = "\n".join(map(str, result))
conn.commit()
conn.close()
except Exception as e:
result = "Error: " + str(e)
await ctx.send("```sql\n" + str(result) + "\n```")