name window with ctypes to distiniguish multiple bot instances, remove old task cooldown code and implement timings.Cooldown objects for tasks and command cooldown, datetime.utcnow switch

This commit is contained in:
Xevion
2021-01-24 17:19:18 -06:00
parent 4ca62daea7
commit 6668e6caec

View File

@@ -1,13 +1,14 @@
import asyncio import asyncio
import ctypes
import logging import logging
import re import re
import time from datetime import datetime
from typing import Optional, Tuple from typing import Optional, Tuple
import discord import discord
from discord.ext.tasks import loop from discord.ext.tasks import loop
from bot import constants, parsers, helpers from bot import constants, parsers, timings
from bot.constants import PlayOptions from bot.constants import PlayOptions
logger = logging.getLogger(__file__) logger = logging.getLogger(__file__)
@@ -23,12 +24,13 @@ class UnbelievaClient(discord.Client):
self.channel: Optional[discord.TextChannel] = None self.channel: Optional[discord.TextChannel] = None
self.bot: Optional[discord.User] = None self.bot: Optional[discord.User] = None
self.tasks = [ self.tasks = {
('$work', 5 * 60 + 5), '$work': timings.Cooldown(5 * 60 + 2),
('$slut', 13 * 60 + 5), '$slut': timings.Cooldown(13 * 60 + 2),
('$crime', 20 * 60 + 5) '$crime': timings.Cooldown(20 * 60 + 2),
] '$dep all': timings.Cooldown(30 * 60)
self.task_times = {} }
self.command_cooldown = timings.Cooldown(5)
self.money = 0 self.money = 0
self.last_deposit = -1 self.last_deposit = -1
@@ -38,9 +40,11 @@ class UnbelievaClient(discord.Client):
async def on_ready(self): async def on_ready(self):
await self.wait_until_ready() await self.wait_until_ready()
self.channel: discord.TextChannel = self.get_channel(self.channel_id) self.channel: discord.TextChannel = self.get_channel(self.channel_id)
self.bot = self.bot_id self.bot = self.bot_id
self.check_task_available.start() self.check_task_available.start()
ctypes.windll.kernel32.SetConsoleTitleW(f"#{self.channel.name}/{self.channel.guild.name}")
logger.info(f'Connected to #{self.channel.name} in {self.channel.guild.name}') logger.info(f'Connected to #{self.channel.name} in {self.channel.guild.name}')
async def on_message(self, message: discord.Message): async def on_message(self, message: discord.Message):
@@ -58,9 +62,7 @@ class UnbelievaClient(discord.Client):
logger.debug(f'"{tcm.duration_unparsed}" => {tcm.duration}s') logger.debug(f'"{tcm.duration_unparsed}" => {tcm.duration}s')
logger.debug(f'Changed {tcm.task_type} to wait {tcm.duration + 2}s instead.') logger.debug(f'Changed {tcm.task_type} to wait {tcm.duration + 2}s instead.')
self.task_times[tcm.task_type] = tcm.available_at self.tasks[tcm.task_type].change_expiration(tcm.available_at)
# helpers.print_embed(embed)
# Handling earnings # Handling earnings
if parsers.TaskResponse.check_valid(message): if parsers.TaskResponse.check_valid(message):
@@ -74,7 +76,6 @@ class UnbelievaClient(discord.Client):
my_cards = self.parse_cards(embed.fields[0]) my_cards = self.parse_cards(embed.fields[0])
dealer_cards = self.parse_cards(embed.fields[1]) dealer_cards = self.parse_cards(embed.fields[1])
print(options, my_cards, dealer_cards) print(options, my_cards, dealer_cards)
# self.current_blackjack = message
def parse_options(self, options_str: str) -> PlayOptions: def parse_options(self, options_str: str) -> PlayOptions:
""" """
@@ -111,52 +112,30 @@ class UnbelievaClient(discord.Client):
print(options, my_cards, dealer_cards) print(options, my_cards, dealer_cards)
pass pass
def handle_task_wait(self, match: re.Match):
task_command = self.task_parsings[match.group(1)]
duration_match = re.match(
r'(\d+) (hour|minute|second)s?(?: and (\d+) (hour|minute|second)s?)?',
match.group(2))
groups = len(list(filter(lambda s: s is not None, duration_match.groups())))
duration = 0
for i in range(0, groups // 2):
x, y = (i * 2) + 1, (i * 2) + 2
duration += int(duration_match.group(x)) * self.durations[duration_match.group(y)]
epoch = time.time() + duration + 2
logger.debug(f'"{match.group(2)}" => {duration}s')
logger.debug(f'Changed {task_command} to wait {duration + 2}s instead.')
self.task_times[task_command] = epoch
@loop(seconds=1) @loop(seconds=1)
async def check_task_available(self): async def check_task_available(self):
"""
Loop to run tasks as soon as they are available.
"""
await self.wait_until_ready() await self.wait_until_ready()
# Check for available tasks for task, task_cooldown in self.tasks.items():
now = time.time() # Task is ready to be ran again
task_completed = False if task_cooldown.ready:
for task, duration in self.tasks: # Ensure the cooldown between commands has ran.
if self.task_times.get(task, 0) <= now: await self.command_cooldown.sleep()
logger.debug(f'Planning to execute {task} {duration}s from now.')
self.task_times[task] = now + duration # Ready to execute the task.
await self.command_sleep()
logger.debug(f'Executing {task} task.') logger.debug(f'Executing {task} task.')
await self.channel.send(task) await self.channel.send(task)
self.last_message = time.time()
task_completed = True
if not task_completed: # Activate the cooldowns
if self.last_deposit + (30 * 60) <= now: task_cooldown.hit()
await self.command_sleep() self.command_cooldown.hit()
await self.channel.send('$dep all')
self.last_message = time.time()
self.last_deposit = time.time()
if self.current_blackjack is not None:
self.handle_blackjack()
async def command_sleep(self): async def command_sleep(self):
"""Sleep right before sending a command.""" """Sleep right before sending a command."""
now = time.time() now = datetime.utcnow().timestamp()
time_between = now - self.last_message time_between = now - self.last_message
wait_time = 6 - time_between wait_time = 6 - time_between