-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.py
More file actions
87 lines (73 loc) · 3.04 KB
/
flake.py
File metadata and controls
87 lines (73 loc) · 3.04 KB
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
# ScottBot by github.com/scottn12
# quotes.py
# Contains all commands related to ScottBot's flake recording functionality.
from discord.ext import commands
from bot import db, cursor
class Flake(commands.Cog, name='Flake'):
'''Commands for Flakers.'''
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True)
async def flakeReset(self, ctx):
'''Resets the flakeRank (ADMIN).'''
await ctx.send("Are you sure you want to permanently reset the flakeRank? Type 'Y' to confirm.")
if not await self.confirmAction(ctx):
await ctx.send('Reset aborted.')
return
cursor.execute('DROP TABLE IF EXISTS flake' + ctx.message.guild.id)
db.commit()
@commands.command(pass_context=True)
async def flake(self, ctx):
'''Increments the flake count for all flakers mentioned.'''
serverID = str(ctx.message.guild.id)
self.createTable('Flake'+serverID)
flakers = ctx.message.mentions
for flaker in flakers:
count = self.flakeIncrement(flaker.id, serverID)
await ctx.send(str(flaker.name) + ' has now flaked ' + count + ' times!')
@commands.command(pass_context=True)
async def flakeRank(self, ctx):
'''Displays the flake standings.'''
try:
ids, counts = self.flakeRead(ctx.message.guild.id)
except:
await ctx.send('There are no flakers!')
return
msg = f'```{"Flaker:":15s}\tCount:\n'
for i in range(len(ids)):
user = self.bot.get_user(int(ids[i]))
msg += f'{str(user.name):15s}\t'
msg += f'{counts[i]}\n'
msg += '```'
await ctx.send(msg)
# Prompts the user to confirm an action and returns true/false
async def confirmAction(self, ctx):
msg = await self.bot.wait_for_message(timeout=10, author=ctx.message.author)
if not msg or msg.content.lower() != 'y':
return False
return True
# Setup Database
def createTable(self, name):
cursor.execute('CREATE TABLE IF NOT EXISTS {}(ID TEXT PRIMARY KEY, Count INTEGER)'.format(name))
db.commit()
# Increment flaker count if exists, if not create
def flakeIncrement(self, ID, serverID):
serverID = str(serverID)
cursor.execute('UPDATE Flake'+serverID+' SET Count = Count + 1 WHERE ID = ?', (ID,))
cursor.execute('INSERT OR IGNORE INTO flake'+serverID+' (ID, Count) VALUES (?, 1)', (ID,))
cursor.execute('SELECT Count FROM Flake'+serverID+' WHERE ID = ?', (ID,))
rtn = str(cursor.fetchone()[0])
db.commit()
return rtn
# Read from DB - output is a string formatted for discord text
def flakeRead(self, serverID):
ids = []
counts = []
cursor.execute('SELECT * FROM Flake' + str(serverID) + ' ORDER BY Count DESC')
rows = cursor.fetchall()
for row in rows:
ids.append(row[0])
counts.append(row[1])
return ids, counts
def setup(bot):
bot.add_cog(Flake(bot))