Change raffles so they're included in one table, per server
This commit is contained in:
parent
7f261a81fa
commit
c33b16136f
|
@ -8,7 +8,6 @@ import pendulum
|
||||||
import re
|
import re
|
||||||
import asyncio
|
import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
import rethinkdb as r
|
|
||||||
|
|
||||||
|
|
||||||
class Raffle:
|
class Raffle:
|
||||||
|
@ -36,9 +35,10 @@ class Raffle:
|
||||||
|
|
||||||
for raffle in raffles:
|
for raffle in raffles:
|
||||||
server = self.bot.get_guild(int(raffle['server_id']))
|
server = self.bot.get_guild(int(raffle['server_id']))
|
||||||
title = raffle['title']
|
for r in raffle['raffles']:
|
||||||
entrants = raffle['entrants']
|
title = r['title']
|
||||||
raffle_id = raffle['id']
|
entrants = r['entrants']
|
||||||
|
raffle_id = r['id']
|
||||||
|
|
||||||
# Check to see if this cog can find the server in question
|
# Check to see if this cog can find the server in question
|
||||||
if server is None:
|
if server is None:
|
||||||
|
@ -46,7 +46,7 @@ class Raffle:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
now = pendulum.utcnow()
|
now = pendulum.utcnow()
|
||||||
expires = pendulum.parse(raffle['expires'])
|
expires = pendulum.parse(r['expires'])
|
||||||
|
|
||||||
# Now lets compare and see if this raffle has ended, if not just continue
|
# Now lets compare and see if this raffle has ended, if not just continue
|
||||||
if expires > now:
|
if expires > now:
|
||||||
|
@ -69,7 +69,8 @@ class Raffle:
|
||||||
break
|
break
|
||||||
|
|
||||||
if winner is None:
|
if winner is None:
|
||||||
fmt = 'I couldn\'t find an entrant that is still in this server, for the raffle `{}`!'.format(title)
|
fmt = 'I couldn\'t find an entrant that is still in this server, for the raffle `{}`!'.format(
|
||||||
|
title)
|
||||||
else:
|
else:
|
||||||
fmt = 'The raffle `{}` has just ended! The winner is {}!'.format(title, winner.display_name)
|
fmt = 'The raffle `{}` has just ended! The winner is {}!'.format(title, winner.display_name)
|
||||||
|
|
||||||
|
@ -88,11 +89,12 @@ class Raffle:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# No matter which one of these matches were met, the raffle has ended and we want to remove it
|
# No matter which one of these matches were met, the raffle has ended and we want to remove it
|
||||||
await self.bot.db.query(r.table('raffles').get(raffle_id).delete())
|
raffle['raffles'].remove(r)
|
||||||
# Now...this is an ugly idea yes, but due to the way raffles are setup currently (they'll be changed in
|
entry = {
|
||||||
# the future) The cache does not update, and leaves behind this deletion....so we need to manually update
|
'server_id': raffle['server_id'],
|
||||||
# the cache here
|
'raffles': raffle['raffles']
|
||||||
await self.bot.db.cache.get('raffles').refresh()
|
}
|
||||||
|
self.bot.db.save('raffles', entry)
|
||||||
|
|
||||||
@commands.command()
|
@commands.command()
|
||||||
@commands.guild_only()
|
@commands.guild_only()
|
||||||
|
@ -103,8 +105,7 @@ class Raffle:
|
||||||
|
|
||||||
EXAMPLE: !raffles
|
EXAMPLE: !raffles
|
||||||
RESULT: A list of the raffles setup on this server"""
|
RESULT: A list of the raffles setup on this server"""
|
||||||
r_filter = {'server_id': str(ctx.message.guild.id)}
|
raffles = self.bot.db.load('raffles', key=ctx.message.guild.id, pluck='raffles')
|
||||||
raffles = self.bot.db.load('raffles', table_filter=r_filter)
|
|
||||||
if not raffles:
|
if not raffles:
|
||||||
await ctx.send("There are currently no raffles setup on this server!")
|
await ctx.send("There are currently no raffles setup on this server!")
|
||||||
return
|
return
|
||||||
|
@ -133,19 +134,15 @@ class Raffle:
|
||||||
RESULT: You've entered the first raffle!"""
|
RESULT: You've entered the first raffle!"""
|
||||||
# Lets let people use 1 - (length of raffles) and handle 0 base ourselves
|
# Lets let people use 1 - (length of raffles) and handle 0 base ourselves
|
||||||
raffle_id -= 1
|
raffle_id -= 1
|
||||||
r_filter = {'server_id': str(ctx.message.guild.id)}
|
|
||||||
author = ctx.message.author
|
author = ctx.message.author
|
||||||
|
key = str(ctx.message.guild.id)
|
||||||
|
|
||||||
raffles = self.bot.db.load('raffles', table_filter=r_filter)
|
raffles = self.bot.db.load('raffles', key=key, pluck='raffles')
|
||||||
if raffles is None:
|
if raffles is None:
|
||||||
await ctx.send("There are currently no raffles setup on this server!")
|
await ctx.send("There are currently no raffles setup on this server!")
|
||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(raffles, list):
|
|
||||||
raffle_count = len(raffles)
|
raffle_count = len(raffles)
|
||||||
else:
|
|
||||||
raffles = [raffles]
|
|
||||||
raffle_count = 1
|
|
||||||
|
|
||||||
# There is only one raffle, so use the first's info
|
# There is only one raffle, so use the first's info
|
||||||
if raffle_count == 1:
|
if raffle_count == 1:
|
||||||
|
@ -157,8 +154,8 @@ class Raffle:
|
||||||
entrants.append(str(author.id))
|
entrants.append(str(author.id))
|
||||||
|
|
||||||
update = {
|
update = {
|
||||||
'entrants': entrants,
|
'raffles': raffles,
|
||||||
'id': raffles[0]['id']
|
'server_id': key
|
||||||
}
|
}
|
||||||
self.bot.db.save('raffles', update)
|
self.bot.db.save('raffles', update)
|
||||||
await ctx.send("{} you have just entered the raffle!".format(author.mention))
|
await ctx.send("{} you have just entered the raffle!".format(author.mention))
|
||||||
|
@ -175,8 +172,8 @@ class Raffle:
|
||||||
# Since we have no good thing to filter things off of, lets use the internal rethinkdb id
|
# Since we have no good thing to filter things off of, lets use the internal rethinkdb id
|
||||||
|
|
||||||
update = {
|
update = {
|
||||||
'entrants': entrants,
|
'raffles': raffles,
|
||||||
'id': raffles[raffle_id]['id']
|
'server_id': key
|
||||||
}
|
}
|
||||||
self.bot.db.save('raffles', update)
|
self.bot.db.save('raffles', update)
|
||||||
await ctx.send("{} you have just entered the raffle!".format(author.mention))
|
await ctx.send("{} you have just entered the raffle!".format(author.mention))
|
||||||
|
@ -270,11 +267,15 @@ class Raffle:
|
||||||
'expires': expires.to_datetime_string(),
|
'expires': expires.to_datetime_string(),
|
||||||
'entrants': [],
|
'entrants': [],
|
||||||
'author': str(author.id),
|
'author': str(author.id),
|
||||||
'server_id': str(server.id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# We don't want to pass a filter to this, because we can have multiple raffles per server
|
raffles = self.bot.db.load('raffles', key=server.id, pluck='raffles') or []
|
||||||
self.bot.db.save('raffles', entry)
|
raffles.append(entry)
|
||||||
|
update = {
|
||||||
|
'server_id': str(server.id),
|
||||||
|
'raffles': raffles
|
||||||
|
}
|
||||||
|
self.bot.db.save('raffles', update)
|
||||||
await ctx.send("I have just saved your new raffle!")
|
await ctx.send("I have just saved your new raffle!")
|
||||||
|
|
||||||
@raffle.command(name='alerts')
|
@raffle.command(name='alerts')
|
||||||
|
|
Loading…
Reference in a new issue