Skip to content

Instantly share code, notes, and snippets.

@LewisMCYoutube
Last active October 17, 2019 03:19
Show Gist options
  • Select an option

  • Save LewisMCYoutube/8e2bddab6ac12643bceb0644163cdd9a to your computer and use it in GitHub Desktop.

Select an option

Save LewisMCYoutube/8e2bddab6ac12643bceb0644163cdd9a to your computer and use it in GitHub Desktop.
PresentForYou script

PresentForYou Python script

This is a version of the Python 3 script used on the PresentForYou account. This script is not robust or well-tested.

This script uses the Python libkol library. You cannot use the account with KoLmafia or in your browser while it is being used with this script.

Instructions

First, you will need to install libkol. Assuming you have Git and Python 3 installed (you'll already have Python 3 in most Linux distros), run these commands in the terminal or command prompt.

git clone --branch 0.5.40 https://github.com/python-kol/libkol.git
pip3 install libkol

Once that's done, you can prepare the script. Open the script in a text editor and look at these lines at the top:

GIFTER_ACCOUNT_USERNAME = "ENTER YOUR USERNAME HERE"
GIFTER_ACCOUNT_PASSWORD = "ENTER YOUR PASSWORD HERE"
ID_OF_ITEM_TO_SEND = 2306

Replace the USERNAME and PASSWORD fields with your account's username and password, making sure not to remove the quotation marks on each side. If your password has a " in it, you must preface the " with a \, so it becomes \".

You should also replace the ID_OF_ITEM_TO_SEND with the ID of the item you want to send. Ideally the script should have a large amount of this item, since there is no automatic refilling/switching function in this script because it is quite basic. You can find the ID of an item on its page in the upper-right box in its page on the KoL wiki.

Before running the script, you should make sure it has at least one item in its inventory besides the item you want to send, and that the account has access to chat (of course). You should probably also baleet your main account on this account.

To run the script, simply run python3 PresentForYouPublic.py, provided you are in the terminal and in the folder that the script is in. In Windows, you can probably just double-click the script to run it. Make sure not to close the script window until you want to quit, though.

If you're using Linux, you can run python3 PresentForYouPublic.py | nohup & and the script will continue even when the terminal is closed.

#!/usr/bin/env python3
GIFTER_ACCOUNT_USERNAME = "ENTER YOUR USERNAME HERE"
GIFTER_ACCOUNT_PASSWORD = "ENTER YOUR PASSWORD HERE"
ID_OF_ITEM_TO_SEND = 2306
MEAT_TO_SEND = 0
from libkol import run, Session, Item
from libkol.request import chat_channel, kmail_send, inventory
async def main():
async with Session() as kol:
try:
await kol.login(GIFTER_ACCOUNT_USERNAME, GIFTER_ACCOUNT_PASSWORD)
itemsSent = 0
usersWhoItemsHaveBeenSentTo = {GIFTER_ACCOUNT_USERNAME}
except:
raise SystemExit
async for messages in kol.chat.messages():
if len(messages) is not 0:
for item in messages:
try:
if item["notnew"] is 1:
print(item)
except KeyError:
try:
if item["type"] is "event":
print(item)
elif item["who"]["name"].lower() not in usersWhoItemsHaveBeenSentTo:
await kol.request("sendmessage.php", params={"told": ""}, data={"action": "send", "howmany1": "1", "whichitem1": str(ID_OF_ITEM_TO_SEND), "towho": item["who"]["id"], "sendmeat": str(MEAT_TO_SEND)}, pwd=True)
itemsSent = itemsSent + 1
print(itemsSent)
usersWhoItemsHaveBeenSentTo.add(item["who"]["name"].lower())
print(usersWhoItemsHaveBeenSentTo)
except Exception:
pass
if __name__ == "__main__":
run(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment