Esempio n. 1
0
 def allow_calc_history(self, channel):
     if not core.parameters.get('calculator.persistent'):
         return False
     if channel.is_private:
         return patrons.tier(channel.user.id) >= patrons.TIER_QUADRATIC
     else:
         return patrons.tier(
             channel.server.owner.id) >= patrons.TIER_QUADRATIC
Esempio n. 2
0
def test_simple_rankings(patrons):
    core.parameters.add_source({'patrons': {
        '1': 'none',
        '2': 'constant',
        '3': 'quadratic',
        '4': 'exponential',
        '5': 'special'    
    }})
    assert patrons.tier('1') == patrons.TIER_NONE
    assert patrons.tier('2') == patrons.TIER_CONSTANT
    assert patrons.tier('3') == patrons.TIER_QUADRATIC
    assert patrons.tier('4') == patrons.TIER_EXPONENTIAL
    assert patrons.tier('5') == patrons.TIER_SPECIAL
Esempio n. 3
0
def test_simple_rankings():
    p = core.parameters.load_parameters([{
        'patrons': {
            '2': 'constant',
            '3': 'quadratic',
            '4': 'exponential',
            '5': 'special'
        }
    }])
    assert patrons.tier(p, '1') == patrons.TIER_NONE
    assert patrons.tier(p, '2') == patrons.TIER_CONSTANT
    assert patrons.tier(p, '3') == patrons.TIER_QUADRATIC
    assert patrons.tier(p, '4') == patrons.TIER_EXPONENTIAL
    assert patrons.tier(p, '5') == patrons.TIER_SPECIAL
Esempio n. 4
0
	async def perform_calculation(self, arg, message, should_sort = False):
		if arg == '':
			# If no equation was given, spit out the help.
			if not message.content.startswith('=='):
				await self.send_message(message.channel, 'Type `=help calc` for information on how to use this command.', blame = message.author)
		else:
			safe.sprint('Doing calculation:', arg)
			if arg.count(':') > 1:
				await self.send_message(message.channel, 'There are too many `:` characters in that equation.', blame = message.author)
			else:
				error = None
				scope = SCOPES[message.channel.id]
				# Determine the stack size and time limit depending on whether
				# the person has the sufficient patreon reward tier
				limits = {'stack_size': 200, 'warnings': True}
				time_limit = 10
				if patrons.tier(message.author.id) >= patrons.TIER_QUADRATIC:
					limits['stack_size'] = 500
					time_limit = 20
				# Actually run the command, and handles the errors
				try:
					future = process_command(arg, scope, limits)
					warnings, values = await asyncio.wait_for(future, timeout = time_limit)
				except asyncio.TimeoutError:
					result = 'Calculation took too long'
				except calculator.EvaluationError as e:
					# traceback.print_exc()
					result = 'Error: ' + str(e)
					if len(result) > 2000:
						result = 'An error occurred, but it was too large to display.'
				except calculator.attempt6.ImbalancedBraces as e:
					result = 'Invalid syntax: Imbalanced braces'
				except calculator.attempt6.TokenizationFailed as e:
					result = format_parse_error('Invalid token', arg, e.position)
				except calculator.attempt6.ParseFailed as e:
					result = format_parse_error('Invalid syntax', arg, e.position)
				else:
					if should_sort:
						values.sort()
					if len(values) == 0:
						result = 'There were no results :thinking:'
					else:
						result = '\n'.join(warnings + ['']) + ' '.join(map(format_result, values))
					if len(result) > 2000:
						result = 'Result was too big :('
					if len(result) < 1000 and await advertising.should_advertise_to(message.author, message.channel):
						result += '\nSupport the bot on Patreon: <https://www.patreon.com/dxsmiley>'
				safe.sprint(result)
				await self.send_message(message.channel, result, blame = message.author)
Esempio n. 5
0
async def should_advertise_to(user, channel):
	result = False
	if core.parameters.get('advertising enable'):
		if patrons.tier(user.id) == patrons.TIER_NONE:
			chan_id = channel.id if channel.is_private else channel.server.id
			ad_count = (await core.keystore.get('advert_counter', chan_id)) \
			        or core.parameters.get('advertising starting-amount')
			if ad_count > core.parameters.get('advertising interval'):
				print('Did advertise!')
				ad_count = 0
				result = True
			else:
				ad_count += random.choice([1, 2])
			await core.keystore.set('advert_counter', chan_id, ad_count)
	return result
Esempio n. 6
0
def test_complex_rankings(patrons):
    core.parameters.add_source({'patrons': {
        '56347856': 'none - Something extra',
        '68362367': 'constantkjdsgh',
        '27456542': 'quadraticsdkfjghdfks',
        '57235548': 'exponentialhfgjd',
        '58563757': 'special---98w475\'\'ekhjf'
    }})
    assert patrons.tier('75635675') == patrons.TIER_NONE
    assert patrons.tier('26374267') == patrons.TIER_NONE
    assert patrons.tier('56347856') == patrons.TIER_NONE
    assert patrons.tier('68362367') == patrons.TIER_CONSTANT
    assert patrons.tier('27456542') == patrons.TIER_QUADRATIC
    assert patrons.tier('57235548') == patrons.TIER_EXPONENTIAL
    assert patrons.tier('58563757') == patrons.TIER_SPECIAL
Esempio n. 7
0
	async def handle_load_module(self, message, argument):
		if core.parameters('calculator loadmodule'):
			if patrons.tier(message.author.id) == patrons.TIER_NONE:
				await self.send_message(message.channel,
					'This command is user development and currently only avaiable to patrons.\nSupport the bot here: https://www.patreon.com/dxsmiley',
					blame = message.author
				)
			else:
				code = None
				async with aiohttp.ClientSession() as session:
					url = 'https://dxbot.herokuapp.com'
					if self.is_dev:
						url = 'http://localhost:5000'
					async with session.get(url + '/api/get/' + argument) as response:
						jdata = await response.json()
						code = jdata['content']
				await self.perform_calculation(code.strip(), message)
Esempio n. 8
0
def test_complex_rankings():
    p = core.parameters.load_parameters([{
        'patrons': {
            '68362367': 'constantkjdsgh',
            '27456542': 'quadraticsdkfjghdfks',
            '57235548': 'exponentialhfgjd',
            '58563757': 'special---98w475\'\'ekhjf'
        }
    }])
    assert patrons.tier(p, '75635675') == patrons.TIER_NONE
    assert patrons.tier(p, '26374267') == patrons.TIER_NONE
    assert patrons.tier(p, '56347856') == patrons.TIER_NONE
    assert patrons.tier(p, '68362367') == patrons.TIER_CONSTANT
    assert patrons.tier(p, '27456542') == patrons.TIER_QUADRATIC
    assert patrons.tier(p, '57235548') == patrons.TIER_EXPONENTIAL
    assert patrons.tier(p, '58563757') == patrons.TIER_SPECIAL
Esempio n. 9
0
def test_invalid_ranking(patrons):
    with pytest.raises(ValueError):
        core.parameters.add_source({'patrons': {
            '1': 'something'
        }})
        assert patrons.tier('1') == patrons.TIER_NONE
Esempio n. 10
0
def test_invalid_ranking():
    with pytest.raises(patrons.InvalidPatronRankError):
        p = core.parameters.load_parameters([{'patrons': {'1': 'something'}}])
        patrons.tier(p, '1')