1
0
Fork 0
mirror of synced 2024-06-29 19:50:25 +12:00
This commit is contained in:
Phxntxm 2016-09-11 17:34:54 -05:00
commit ca35605f97
4 changed files with 30 additions and 49 deletions

View file

@ -114,9 +114,7 @@ class Hangman:
"""Makes a guess towards the server's currently running hangman game""" """Makes a guess towards the server's currently running hangman game"""
game = self.games.get(ctx.message.server.id) game = self.games.get(ctx.message.server.id)
if not game: if not game:
bucket = ctx.command._buckets.get_bucket(ctx) ctx.command.reset_cooldown(ctx)
bucket.is_rate_limited()
bucket._token = bucket.rate
await self.bot.say("There are currently no hangman games running!") await self.bot.say("There are currently no hangman games running!")
return return
@ -126,9 +124,7 @@ class Hangman:
# And also add a message for a loss/win # And also add a message for a loss/win
if len(guess) == 1: if len(guess) == 1:
if guess in game.guessed_letters: if guess in game.guessed_letters:
bucket = ctx.command._buckets.get_bucket(ctx) ctx.command.reset_cooldown(ctx)
bucket.is_rate_limited()
bucket._tokens = bucket.rate
await self.bot.say("That letter has already been guessed!") await self.bot.say("That letter has already been guessed!")
# Return here as we don't want to count this as a failure # Return here as we don't want to count this as a failure
return return

View file

@ -93,21 +93,15 @@ class Interaction:
async def battle(self, ctx, player2: discord.Member): async def battle(self, ctx, player2: discord.Member):
"""Challenges the mentioned user to a battle""" """Challenges the mentioned user to a battle"""
if ctx.message.author.id == player2.id: if ctx.message.author.id == player2.id:
bucket = ctx.command._buckets.get_bucket(ctx) ctx.command.reset_cooldown(ctx)
bucket.is_rate_limited()
bucket._tokens = bucket.rate
await self.bot.say("Why would you want to battle yourself? Suicide is not the answer") await self.bot.say("Why would you want to battle yourself? Suicide is not the answer")
return return
if self.bot.user.id == player2.id: if self.bot.user.id == player2.id:
bucket = ctx.command._buckets.get_bucket(ctx) ctx.command.reset_cooldown(ctx)
bucket.is_rate_limited()
bucket._tokens = bucket.rate
await self.bot.say("I always win, don't even try it.") await self.bot.say("I always win, don't even try it.")
return return
if self.user_battling(ctx, player2): if self.user_battling(ctx, player2):
bucket = ctx.command._buckets.get_bucket(ctx) ctx.command.reset_cooldown(ctx)
bucket.is_rate_limited()
bucket._tokens = bucket.rate
await self.bot.say("You or the person you are trying to battle is already in a battle!") await self.bot.say("You or the person you are trying to battle is already in a battle!")
return return
@ -176,39 +170,23 @@ class Interaction:
"""Boops the mentioned person""" """Boops the mentioned person"""
booper = ctx.message.author booper = ctx.message.author
if boopee.id == booper.id: if boopee.id == booper.id:
bucket = ctx.command._buckets.get_bucket(ctx) ctx.command.reset_cooldown(ctx)
bucket.is_rate_limited()
bucket._tokens = bucket.rate
await self.bot.say("You can't boop yourself! Silly...") await self.bot.say("You can't boop yourself! Silly...")
return return
if boopee.id == self.bot.user.id: if boopee.id == self.bot.user.id:
bucket = ctx.command._buckets.get_bucket(ctx) ctx.command.reset_cooldown(ctx)
bucket.is_rate_limited()
bucket._tokens = bucket.rate
await self.bot.say("Why the heck are you booping me? Get away from me >:c") await self.bot.say("Why the heck are you booping me? Get away from me >:c")
return return
boops = await config.get_content('boops') boops = await config.get_content('boops')
# This is only used to print the amount of times they've booped someone
# Set to 1 for the first time someone was booped
amount = 1
# Get all the booped stats for the author # Get all the booped stats for the author
booper_boops = boops.get(ctx.message.author.id) # Set to default as having just booped boopee 0 times, so that we can increment that
# If the author does not exist in the dictionary, then he has never booped someone booper_boops = boops.get(ctx.message.author.id, {boopee.id: 0})
# Create a new dictionary with the amount # If the booper has never booped the member provided, assume 0 like above so we can increment like normal
if booper_boops is None: amount = booper_boops.get(boopee.id, 0) + 1
boops[ctx.message.author.id] = {boopee.id: 1} booper_boops[boopee.id] = amount
# If the booper has never booped the member provided, still add that user boops[ctx.message.author.id] = booper_boops
# To the dictionary with the amount of 1 to start it off
elif booper_boops.get(boopee.id) is None:
booper_boops[boopee.id] = 1
boops[ctx.message.author.id] = booper_boops
# Otherwise increment how many times they've booped that user
else:
amount = booper_boops.get(boopee.id) + 1
booper_boops[boopee.id] = amount
boops[ctx.message.author.id] = booper_boops
await config.save_content('boops', boops) await config.save_content('boops', boops)
fmt = "{0.mention} has just booped you {1.mention}! That's {2} times now!" fmt = "{0.mention} has just booped you {1.mention}! That's {2} times now!"

View file

@ -109,7 +109,7 @@ class Strawpoll:
# Save this strawpoll in the list of running strawpolls for a server # Save this strawpoll in the list of running strawpolls for a server
all_polls = await config.get_content('strawpolls') all_polls = await config.get_content('strawpolls')
server_polls = all_polls.get(ctx.message.server.id) or {} server_polls = all_polls.get(ctx.message.server.id) or {}
server_polls[data['id']] = {'author': ctx.message.author.id, 'date': str(pendulum.utcnow()), 'title': title} server_polls[str(data['id'])] = {'author': ctx.message.author.id, 'date': str(pendulum.utcnow()), 'title': title}
all_polls[ctx.message.server.id] = server_polls all_polls[ctx.message.server.id] = server_polls
await config.save_content('strawpolls', all_polls) await config.save_content('strawpolls', all_polls)

View file

@ -82,7 +82,8 @@ db_opts = {'host': db_host, 'db': db_name, 'port': db_port, 'user': db_user, 'pa
possible_keys = ['prefixes', 'battling', 'battle_records', 'boops', 'server_alerts', 'user_notifications', possible_keys = ['prefixes', 'battling', 'battle_records', 'boops', 'server_alerts', 'user_notifications',
'nsfw_channels', 'custom_permissions', 'rules', 'overwatch', 'picarto', 'twitch', 'strawpolls', 'tags', 'nsfw_channels', 'custom_permissions', 'rules', 'overwatch', 'picarto', 'twitch', 'strawpolls', 'tags',
'tictactoe', 'bot_data'] 'tictactoe', 'bot_data', 'command_manage']
# This will be a dictionary that holds the cache object, based on the key that is saved # This will be a dictionary that holds the cache object, based on the key that is saved
cache = {} cache = {}
@ -124,22 +125,28 @@ async def save_content(table: str, content):
# Now that we've saved the new content, we should update our cache # Now that we've saved the new content, we should update our cache
cached = cache.get(table) cached = cache.get(table)
# While this should theoretically never happen, we just want to make sure # While this should theoretically never happen, we just want to make sure
if cached is None or isinstance(cached, dict) or len(cached.values) == 0: if cached is None:
cache[table] = Cache(table) cache[table] = Cache(table)
else: else:
await cached.update() loop.create_task(cached.update())
async def get_content(key: str): async def get_content(key: str):
cached = cache.get(key) cached = cache.get(key)
# We want to check here if the key exists in cache, and it was not created more than an hour ago # We want to check here if the key exists in cache, and it was not created more than an hour ago
# We also want to make sure that if what we're getting in cache has content # We also want to make sure that if what we're getting in cache has content
# if not, lets make sure something didn't go awry, by getting from the database instead # If not, lets make sure something didn't go awry, by getting from the database instead
if cached is None or isinstance(cached, dict) or len(cached.values) == 0 or (
pendulum.utcnow() - cached.refreshed).hours >= 1: # If we found this object not cached, cache it
if cached is None:
value = await _get_content(key) value = await _get_content(key)
# If we found this object not cached, cache it cache[key] = Cache(key)
cache[key] = value # Otherwise, check our timeout and make sure values is invalid
# If either of these are the case, we want to updated our values, and get our current data from the database
elif len(cached.values) == 0 or (pendulum.utcnow() - cached.refreshed).hours >= 1:
value = await _get_content(key)
loop.create_task(cached.update())
# Otherwise, we have valid content in cache, use it
else: else:
value = cached.values value = cached.values
return value return value
@ -154,8 +161,8 @@ async def _get_content(key: str):
# We should only ever get one result, so use it if it exists, otherwise return none # We should only ever get one result, so use it if it exists, otherwise return none
try: try:
cursor = await r.table(key).run(conn) cursor = await r.table(key).run(conn)
await conn.close()
items = list(cursor.items)[0] items = list(cursor.items)[0]
await conn.close()
except (IndexError, r.ReqlOpFailedError): except (IndexError, r.ReqlOpFailedError):
await conn.close() await conn.close()
return {} return {}