2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
2016-07-24 04:40:52 +12:00
|
|
|
from discord.ext.commands.cooldowns import BucketType
|
2016-07-09 13:27:19 +12:00
|
|
|
from .utils import config
|
2016-07-17 04:49:14 +12:00
|
|
|
from .utils import checks
|
2016-07-09 13:42:33 +12:00
|
|
|
import discord
|
2016-07-09 13:27:19 +12:00
|
|
|
import random
|
|
|
|
|
|
|
|
|
2016-07-24 03:07:03 +12:00
|
|
|
def battlingOff(player_id):
|
2016-07-24 02:20:49 +12:00
|
|
|
battling = config.getContent('battling')
|
2016-08-07 10:20:23 +12:00
|
|
|
|
|
|
|
battling = {p1: p2 for p1, p2 in battling.items() if not p2 == player_id and not p1 == player_id}
|
|
|
|
|
|
|
|
config.saveContent('battling', battling)
|
|
|
|
|
|
|
|
|
2016-07-19 02:27:15 +12:00
|
|
|
def userBattling(ctx):
|
2016-07-19 02:26:11 +12:00
|
|
|
battling = config.getContent('battling')
|
2016-07-19 02:35:05 +12:00
|
|
|
if battling is None:
|
|
|
|
return False
|
2016-07-31 00:30:28 +12:00
|
|
|
if ctx.message.author.id in battling.values() or ctx.message.author.id in battling.keys():
|
2016-07-24 02:20:49 +12:00
|
|
|
return True
|
2016-07-19 02:47:39 +12:00
|
|
|
if str(ctx.command) == 'battle':
|
2016-07-31 00:31:55 +12:00
|
|
|
return ctx.message.mentions[0].id in battling.values() or ctx.message.mentions[0].id in battling.keys()
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-19 02:47:39 +12:00
|
|
|
return False
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
|
|
|
|
def updateBattleRecords(winner, loser):
|
2016-07-18 03:17:47 +12:00
|
|
|
battles = config.getContent('battle_records')
|
2016-07-26 02:25:17 +12:00
|
|
|
if battles is None:
|
2016-07-18 09:10:12 +12:00
|
|
|
battles = {winner.id: "1-0", loser.id: "0-1"}
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-26 02:25:17 +12:00
|
|
|
winner_stats = battles.get(winner.id) or {}
|
2016-07-26 02:32:35 +12:00
|
|
|
winner_rating = winner_stats.get('rating') or 1000
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-26 02:25:17 +12:00
|
|
|
loser_stats = battles.get(loser.id) or {}
|
2016-07-26 02:32:35 +12:00
|
|
|
loser_rating = loser_stats.get('rating') or 1000
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-26 02:25:17 +12:00
|
|
|
difference = abs(winner_rating - loser_rating)
|
|
|
|
rating_change = 0
|
|
|
|
count = 25
|
|
|
|
while count <= difference:
|
|
|
|
if count > 300:
|
|
|
|
break
|
|
|
|
rating_change += 1
|
|
|
|
count += 25
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-26 02:25:17 +12:00
|
|
|
if winner_rating > loser_rating:
|
|
|
|
winner_rating += 16 - rating_change
|
|
|
|
loser_rating -= 16 - rating_change
|
|
|
|
else:
|
|
|
|
winner_rating += 16 + rating_change
|
|
|
|
loser_rating -= 16 + rating_change
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-26 02:49:51 +12:00
|
|
|
winner_wins = winner_stats.get('wins') or 0
|
2016-07-26 02:25:17 +12:00
|
|
|
winner_losses = winner_stats.get('losses') or 0
|
|
|
|
loser_wins = loser_stats.get('wins') or 0
|
2016-07-26 02:49:51 +12:00
|
|
|
loser_losses = loser_stats.get('losses') or 0
|
|
|
|
winner_wins += 1
|
|
|
|
loser_losses += 1
|
2016-08-07 10:20:23 +12:00
|
|
|
|
|
|
|
winner_stats = {'wins': winner_wins, 'losses': winner_losses, 'rating': winner_rating}
|
|
|
|
loser_stats = {'wins': loser_wins, 'losses': loser_losses, 'rating': loser_rating}
|
2016-07-26 02:25:17 +12:00
|
|
|
battles[winner.id] = winner_stats
|
2016-07-26 02:30:20 +12:00
|
|
|
battles[loser.id] = loser_stats
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-24 02:20:49 +12:00
|
|
|
return config.saveContent('battle_records', battles)
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
|
|
|
|
class Interaction:
|
2016-07-09 13:59:10 +12:00
|
|
|
"""Commands that interact with another user"""
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
2016-08-07 10:20:23 +12:00
|
|
|
@commands.group(pass_context=True, no_pm=True, invoke_without_command=True)
|
|
|
|
@commands.cooldown(1, 180, BucketType.user)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-07-09 13:42:33 +12:00
|
|
|
async def battle(self, ctx, player2: discord.Member):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Challenges the mentioned user to a battle"""
|
2016-07-10 00:57:25 +12:00
|
|
|
if len(ctx.message.mentions) == 0:
|
|
|
|
await self.bot.say("You must mention someone in the room " + ctx.message.author.mention + "!")
|
|
|
|
return
|
|
|
|
if len(ctx.message.mentions) > 1:
|
|
|
|
await self.bot.say("You cannot battle more than one person at once!")
|
|
|
|
return
|
|
|
|
if ctx.message.author.id == player2.id:
|
|
|
|
await self.bot.say("Why would you want to battle yourself? Suicide is not the answer")
|
|
|
|
return
|
|
|
|
if self.bot.user.id == player2.id:
|
|
|
|
await self.bot.say("I always win, don't even try it.")
|
|
|
|
return
|
2016-07-19 02:26:11 +12:00
|
|
|
if userBattling(ctx):
|
|
|
|
await self.bot.say("You or the person you are trying to battle is already in a battle!")
|
|
|
|
return
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-24 02:20:49 +12:00
|
|
|
battling = config.getContent('battling') or {}
|
2016-07-19 02:36:21 +12:00
|
|
|
battling[ctx.message.author.id] = ctx.message.mentions[0].id
|
2016-08-04 13:31:38 +12:00
|
|
|
config.saveContent('battling', battling)
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-24 02:20:49 +12:00
|
|
|
fmt = "{0.mention} has challenged you to a battle {1.mention}\n!accept or !decline"
|
2016-08-08 01:55:35 +12:00
|
|
|
config.loop.call_later(180, battlingOff, ctx.message.author.id)
|
2016-07-10 00:57:25 +12:00
|
|
|
await self.bot.say(fmt.format(ctx.message.author, player2))
|
2016-07-29 04:59:12 +12:00
|
|
|
await self.bot.delete_message(ctx.message)
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-11 09:57:52 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-07-11 09:57:52 +12:00
|
|
|
async def accept(self, ctx):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Accepts the battle challenge"""
|
2016-07-19 02:47:39 +12:00
|
|
|
if not userBattling(ctx):
|
2016-07-19 03:45:40 +12:00
|
|
|
await self.bot.say("You are not currently in a battle!")
|
2016-07-10 00:57:25 +12:00
|
|
|
return
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-24 02:20:49 +12:00
|
|
|
battling = config.getContent('battling') or {}
|
2016-08-07 10:20:23 +12:00
|
|
|
p1 = [p1_id for p1_id, p2_id in battling.items() if p2_id == ctx.message.author.id]
|
2016-07-24 02:20:49 +12:00
|
|
|
if len(p1) == 0:
|
|
|
|
await self.bot.say("You are not currently being challenged to a battle!")
|
|
|
|
return
|
2016-08-07 10:20:23 +12:00
|
|
|
|
|
|
|
battleP1 = discord.utils.find(lambda m: m.id == p1[0], ctx.message.server.members)
|
2016-07-19 08:01:07 +12:00
|
|
|
battleP2 = ctx.message.author
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-10 00:57:25 +12:00
|
|
|
fmt = config.battleWins[random.randint(0, len(config.battleWins) - 1)]
|
2016-08-08 01:46:30 +12:00
|
|
|
battlingOff(ctx.message.author.id)
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-24 02:20:49 +12:00
|
|
|
if random.randint(1, 100) < 50:
|
2016-07-10 00:57:25 +12:00
|
|
|
await self.bot.say(fmt.format(battleP1.mention, battleP2.mention))
|
2016-07-24 02:20:49 +12:00
|
|
|
updateBattleRecords(battleP1, battleP2)
|
|
|
|
else:
|
2016-07-10 00:57:25 +12:00
|
|
|
await self.bot.say(fmt.format(battleP2.mention, battleP1.mention))
|
2016-07-24 02:20:49 +12:00
|
|
|
updateBattleRecords(battleP2, battleP1)
|
2016-08-08 01:46:30 +12:00
|
|
|
|
2016-07-29 04:59:12 +12:00
|
|
|
await self.bot.delete_message(ctx.message)
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-11 09:57:52 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-07-11 09:57:52 +12:00
|
|
|
async def decline(self, ctx):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Declines the battle challenge"""
|
2016-07-19 02:47:39 +12:00
|
|
|
if not userBattling(ctx):
|
2016-07-19 03:45:40 +12:00
|
|
|
await self.bot.say("You are not currently in a battle!")
|
2016-07-10 00:57:25 +12:00
|
|
|
return
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-24 02:20:49 +12:00
|
|
|
battling = config.getContent('battling') or {}
|
2016-08-07 10:20:23 +12:00
|
|
|
p1 = [p1_id for p1_id, p2_id in battling.items() if p2_id == ctx.message.author.id]
|
2016-07-24 02:20:49 +12:00
|
|
|
if len(p1) == 0:
|
|
|
|
await self.bot.say("You are not currently being challenged to a battle!")
|
|
|
|
return
|
2016-08-07 10:20:23 +12:00
|
|
|
battleP1 = discord.utils.find(lambda m: m.id == p1[0], ctx.message.server.members)
|
2016-07-24 02:20:49 +12:00
|
|
|
battleP2 = ctx.message.author
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-08-08 01:46:30 +12:00
|
|
|
battlingOff(ctx.message.author.id)
|
2016-08-05 03:20:28 +12:00
|
|
|
await self.bot.say("{0} has chickened out! What a loser~".format(battleP2.mention, battleP1.mention))
|
2016-07-29 04:59:12 +12:00
|
|
|
await self.bot.delete_message(ctx.message)
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2016-08-04 13:31:38 +12:00
|
|
|
@commands.cooldown(1, 180, BucketType.user)
|
2016-07-31 00:30:28 +12:00
|
|
|
@checks.customPermsOrRole(send_messages=True)
|
2016-07-09 13:42:33 +12:00
|
|
|
async def boop(self, ctx, boopee: discord.Member):
|
2016-07-09 13:27:19 +12:00
|
|
|
"""Boops the mentioned person"""
|
2016-07-10 00:57:25 +12:00
|
|
|
booper = ctx.message.author
|
|
|
|
if len(ctx.message.mentions) == 0:
|
|
|
|
await self.bot.say("You must mention someone in the room " + ctx.message.author.mention + "!")
|
|
|
|
return
|
|
|
|
if len(ctx.message.mentions) > 1:
|
|
|
|
await self.bot.say("You cannot boop more than one person at once!")
|
|
|
|
return
|
|
|
|
if boopee.id == booper.id:
|
|
|
|
await self.bot.say("You can't boop yourself! Silly...")
|
|
|
|
return
|
|
|
|
if boopee.id == self.bot.user.id:
|
|
|
|
await self.bot.say("Why the heck are you booping me? Get away from me >:c")
|
|
|
|
return
|
|
|
|
|
2016-07-24 02:20:49 +12:00
|
|
|
boops = config.getContent('boops') or {}
|
2016-08-07 10:20:23 +12:00
|
|
|
|
2016-07-10 00:57:25 +12:00
|
|
|
amount = 1
|
2016-07-18 03:17:47 +12:00
|
|
|
booper_boops = boops.get(ctx.message.author.id)
|
|
|
|
if booper_boops is None:
|
2016-07-18 09:10:12 +12:00
|
|
|
boops[ctx.message.author.id] = {boopee.id: 1}
|
2016-07-18 03:17:47 +12:00
|
|
|
elif booper_boops.get(boopee.id) is None:
|
|
|
|
booper_boops[boopee.id] = 1
|
|
|
|
boops[ctx.message.author.id] = booper_boops
|
2016-07-10 00:57:25 +12:00
|
|
|
else:
|
2016-07-18 03:17:47 +12:00
|
|
|
amount = booper_boops.get(boopee.id) + 1
|
|
|
|
booper_boops[boopee.id] = amount
|
|
|
|
boops[ctx.message.author.id] = booper_boops
|
2016-07-18 09:10:12 +12:00
|
|
|
|
2016-07-29 11:15:02 +12:00
|
|
|
config.saveContent('boops', boops)
|
2016-07-29 04:59:12 +12:00
|
|
|
fmt = "{0.mention} has just booped you {1.mention}! That's {2} times now!"
|
|
|
|
await self.bot.say(fmt.format(booper, boopee, amount))
|
|
|
|
await self.bot.delete_message(ctx.message)
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
|
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Interaction(bot))
|