PoeTrade/src/sendkeys.py
2021-03-20 21:31:16 +01:00

75 lines
3.0 KiB
Python

from pywinauto.application import Application
from pywinauto.findwindows import ElementNotFoundError
from src import data
from src import config
def send_text(text: str) -> None:
app = Application()
try:
app.connect(title=config.conf['General']['poe_window_title'])
except ElementNotFoundError:
data.log.warning(f'Window Title "{config.conf["General"]["poe_window_title"]}" not found')
return
win = app.window(title_re=config.conf['General']['poe_window_title'])
text = escape_mods(text)
win.type_keys('{ENTER}^A{DELETE}' + text + '{ENTER}', with_spaces=True, pause=config.conf['General']['after_sendkeys_key_wait'])
def escape_mods(text: str) -> str:
text = text.replace('%', '{%}')
text = text.replace('+', '{+}')
text = text.replace('^', '{^}')
text = text.replace('(', '{(}')
text = text.replace(')', '{)}')
return text
def send_to(user: str, text: str) -> None:
send_text(f'@{user} {text}')
def send_to_format(type: str, message: data.Message) -> None:
"""Send a message defined in the conf dict to a user
Args:
type (str): The type of message. This is defined in conf['Chat'].
message (data.Message): Message data. Placeholders in the message type will be replaced with this data.
conf (dict): Configution dictionary where the messages are stored.
"""
if message.trade:
text = config.conf['Chat'][type].format(message=message.message,
date=message.date,
channel=message.channel.name,
user=message.user,
guild=message.guild,
to_from=message.to_from,
item=message.trade.item,
amount=message.trade.amount,
currency=message.trade.currency,
tab=message.trade.tab,
row=message.trade.row,
col=message.trade.col,
league=message.trade.league)
else:
text = config.conf['Chat'][type].format(message=message.message,
date=message.date,
channel=message.channel.name,
user=message.user,
guild=message.guild,
to_from=message.to_from)
send_to(message.user, text)
def invite(message: data.Message) -> None:
send_text(f'/invite {message.user}')
def kick(message: data.Message) -> None:
send_text(f'/kick {message.user}')
def trade(message: data.Message) -> None:
send_text(f'/tradewith {message.user}')