-
Notifications
You must be signed in to change notification settings - Fork 11
/
letter_game.py
378 lines (356 loc) · 16.2 KB
/
letter_game.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
import discord
from discord.ext import commands
import asyncio
from discord import Embed
import aiosqlite
rules = """
A set of rules - No going twice
You must go in the order of the alphabet
You cannot repeat a word
Start with A, example Apple / Bear / cottage cheese
Keep repeating this order until you get to Z then start over again
If you repeat you will lose the ability to keep playing.
A staff member may reset the game
"""
class letter_game(commands.Cog):
"""The Word games"""
def __init__(self, client):
self.client = client
@commands.command(name="how_to_letter_game")
async def letter_game_rules(self, ctx):
await ctx.respond(rules)
@commands.is_owner()
@commands.command(name="letter_game_db")
async def letter_game_db(self, ctx):
conn = await aiosqlite.connect("databases/letter_game.db")
c = await conn.cursor()
await c.execute(
"CREATE TABLE IF NOT EXISTS letter_game_server (guild_id Integer, channel_id Integer, word text, last_user_id Integer, longest_game Integer, current_game Integer,total_games Integer,total_failed_games Integer)"
)
await conn.commit()
await conn.close()
await ctx.send("Done")
conn = await aiosqlite.connect("databases/letter_game.db")
c = await conn.cursor()
await c.execute(
"CREATE TABLE IF NOT EXISTS words_used (guild_id Integer, word text)"
)
await conn.commit()
await conn.close()
await ctx.send("words Done")
conn = await aiosqlite.connect("databases/letter_game.db")
c = await conn.cursor()
await c.execute(
"CREATE TABLE IF NOT EXISTS user_stats (guild_id Integer, user_id Integer, total_words Integer, Total_fails Integer)"
)
await conn.commit()
await conn.close()
await ctx.send("user stats Done")
@commands.command(name="letter_game")
@commands.has_permissions(manage_channels=True)
async def letter_game_commands(self, ctx, channel: discord.TextChannel = None):
# if bot is not at server permission highest
if ctx.guild.me.top_role.position < ctx.author.top_role.position:
await ctx.send(
"I am not at the highest role position due to this I may not be able to remove users from the channel when they fail. Please consider moving my role to the top."
)
if channel is None:
channel = ctx.channel
async with aiosqlite.connect("databases/letter_game.db") as conn:
async with conn.cursor() as c:
await c.execute(
"SELECT * FROM letter_game_server WHERE guild_id = ?",
(ctx.guild.id,),
)
data = await c.fetchone()
if data is None:
await c.execute(
"INSERT INTO letter_game_server VALUES (?,?,?,?,?,?,?,?)",
(ctx.guild.id, channel.id, None, 0, 0, 0, 0, 0),
)
await conn.commit()
await conn.close()
await ctx.send(
"Game has been started in {}".format(channel.mention)
)
else:
await ctx.send(
"Game is already running in {} would you like to restart here?".format(
channel.mention
)
)
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
try:
msg = await self.client.wait_for(
"message", timeout=30.0, check=check
)
except asyncio.TimeoutError:
await ctx.send(
"You took to long to respond Nothing has been changed"
)
else:
if msg.content.lower() == "yes":
await c.execute(
"UPDATE letter_game_server SET channel_id = ?, word = ?, last_user_id = ?, longest_game = ?, current_game = ?,total_games = ?,total_failed_games = ? WHERE guild_id = ?",
(channel.id, None, 0, 0, 0, 0, 0, ctx.guild.id),
)
await conn.commit()
await conn.close()
await ctx.send(
"Game has been started in {}".format(channel.mention)
)
else:
await ctx.send("Nothing has been changed")
@commands.slash_command(
name="letter_game", description="Starts a letter game in the channel you are in"
)
@commands.has_permissions(manage_channels=True)
async def letter_game_slash(self, ctx, channel: discord.TextChannel = None):
# if bot is not at server permission highest
if ctx.guild.me.top_role.position < ctx.author.top_role.position:
await ctx.respond(
"I am not at the highest role position due to this I may not be able to remove users from the channel when they fail. Please consider moving my role to the top."
)
if channel is None:
channel = ctx.channel
async with aiosqlite.connect("databases/letter_game.db") as conn:
async with conn.cursor() as c:
await c.execute(
"SELECT * FROM letter_game_server WHERE guild_id = ?",
(ctx.guild.id,),
)
data = await c.fetchone()
if data is None:
await c.execute(
"INSERT INTO letter_game_server VALUES (?,?,?,?,?,?,?,?)",
(ctx.guild.id, channel.id, None, 0, 0, 0, 0, 0),
)
await conn.commit()
await conn.close()
await ctx.respond(
"Game has been started in {}".format(channel.mention)
)
else:
await ctx.respond(
"Game is already running in {} would you like to restart here?".format(
channel.mention
)
)
def check(m):
return m.author == ctx.author and m.channel == ctx.channel
try:
msg = await self.client.wait_for(
"message", timeout=30.0, check=check
)
except asyncio.TimeoutError:
await ctx.respond(
"You took to long to respond Nothing has been changed"
)
else:
if msg.content.lower() == "yes":
await c.execute(
"UPDATE letter_game_server SET channel_id = ?, word = ?, last_user_id = ?, longest_game = ?, current_game = ?,total_games = ?,total_failed_games = ? WHERE guild_id = ?",
(channel.id, None, 0, 0, 0, 0, 0, ctx.guild.id),
)
await conn.commit()
await conn.close()
await ctx.respond(
"Game has been started in {}".format(channel.mention)
)
else:
await ctx.respond("Nothing has been changed")
@commands.Cog.listener()
async def on_message(self, message):
async with aiosqlite.connect("databases/letter_game.db") as conn:
data = await conn.execute(
"SELECT * FROM letter_game_server WHERE guild_id = ?",
(message.guild.id,),
)
data = await data.fetchone()
if data is None:
return
channel_id = data[1]
if channel_id != message.channel.id:
print("not in channel")
print(channel_id)
print(message.channel.id)
return
print("in channel")
print(channel_id)
print(message.channel.id)
if message.author.bot:
return
# if the message is more than 1 word
if len(message.content.split()) > 1:
return
lastword = data[2]
lastuser = data[3]
longestgame = data[4]
currentgame = data[5]
totalgames = data[6]
totalfailedgames = data[7]
if lastuser == message.author.id:
await message.channel.send(
"You can't go twice in a row. You have been removed from the game"
)
# remove user from channel
await conn.execute(
"UPDATE letter_game_server SET total_failed_games = ? WHERE guild_id = ?",
(totalfailedgames + 1, message.guild.id),
)
await conn.commit()
userdata = await conn.execute(
"SELECT * FROM user_stats WHERE user_id = ?", (message.author.id,)
)
userdata = await userdata.fetchone()
if userdata is None:
await conn.execute(
"INSERT INTO user_stats VALUES (?,?,?,?,?)",
(message.author.id, 0, 0, 0, 0),
)
await conn.commit()
await conn.execute(
"UPDATE user_stats SET letter_game_failed = ? WHERE user_id = ?",
(userdata[4] + 1, message.author.id),
)
await conn.commit()
try:
await message.channel.set_permissions(
message.author, send_messages=False
)
except discord.Forbidden:
await message.channel.send(
"I do not have permission to remove users from the channel"
)
return
if lastword is None:
# if message does not start with a
if message.content[0].lower() != "a":
await message.channel.send("The word must start with a")
return
# set first current_game total_games
await conn.execute(
"UPDATE letter_game_server SET word = ?, last_user_id = ?, current_game = ?, total_games = ? WHERE guild_id = ?",
(
message.content.lower(),
message.author.id,
currentgame + 1,
totalgames + 1,
message.guild.id,
),
)
await conn.commit()
await conn.execute(
"insert into wordbank VALUES (?,?)",
(message.guild.id, message.content.lower()),
)
await conn.commit()
await message.add_reaction("✅")
return
elif lastword.content[0] == "z":
if message.content[0].lower() == "a":
# set first current_game total_games
await conn.execute(
"UPDATE letter_game_server SET word = ?, last_user_id = ?, current_game = ?, total_games = ? WHERE guild_id = ?",
(
message.content.lower(),
message.author.id,
currentgame + 1,
totalgames + 1,
message.guild.id,
),
)
await conn.commit()
await conn.execute(
"insert into wordbank VALUES (?,?)",
(message.guild.id, message.content.lower()),
)
await conn.commit()
await message.add_reaction("✅")
return
else:
await message.channel.send("The word must start with a")
# remove user from channel
await conn.execute(
"UPDATE letter_game_server SET total_failed_games = ? WHERE guild_id = ?",
(totalfailedgames + 1, message.guild.id),
)
await conn.commit()
userdata = await conn.execute(
"SELECT * FROM user_stats WHERE user_id = ?",
(message.author.id,),
)
userdata = await userdata.fetchone()
if userdata is None:
await conn.execute(
"INSERT INTO user_stats VALUES (?,?,?,?,?)",
(message.author.id, 0, 0, 0, 0),
)
await conn.commit()
await conn.execute(
"UPDATE user_stats SET letter_game_failed = ? WHERE user_id = ?",
(userdata[4] + 1, message.author.id),
)
await conn.commit()
try:
await message.channel.set_permissions(
message.author, send_messages=False
)
except discord.Forbidden:
await message.channel.send(
"I do not have permission to remove users from the channel"
)
return
# if this word first letter is one bigger than first letter of last word
elif ord(message.content[0].lower()) == ord(lastword.content[-1]) + 1:
await conn.execute(
"insert into wordbank VALUES (?,?)",
(message.guild.id, message.content.lower()),
)
await conn.commit()
await conn.execute(
"UPDATE letter_game_server SET word = ?, last_user_id = ? WHERE guild_id = ?",
(message.content.lower(), message.author.id, message.guild.id),
)
await conn.commit()
await message.add_reaction("✅")
return
else:
await message.channel.send(
"the next word must start {}".format(
chr(ord(lastword.content[-1]) + 1)
)
)
# remove user from channel
await conn.execute(
"UPDATE letter_game_server SET total_failed_games = ? WHERE guild_id = ?",
(totalfailedgames + 1, message.guild.id),
)
await conn.commit()
userdata = await conn.execute(
"SELECT * FROM user_stats WHERE user_id = ?", (message.author.id,)
)
userdata = await userdata.fetchone()
if userdata is None:
await conn.execute(
"INSERT INTO user_stats VALUES (?,?,?,?,?)",
(message.author.id, 0, 0, 0, 0),
)
await conn.commit()
await conn.execute(
"UPDATE user_stats SET letter_game_failed = ? WHERE user_id = ?",
(userdata[4] + 1, message.author.id),
)
await conn.commit()
try:
await message.channel.set_permissions(
message.author, send_messages=False
)
except discord.Forbidden:
await message.channel.send(
"I do not have permission to remove users from the channel"
)
return
def setup(client):
client.add_cog(letter_game(client))