-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
118 lines (96 loc) · 4.24 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from typing import Final
import lichess.api
from lichess.format import SINGLE_PGN
from lichess_client import APIClient
import asyncio
import json
# pip install python-telegram-bot
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
print('Starting up bot...')
TOKEN: Final = '6264740670:AAHjJxYHFP8napVe2BNY8fb6NxQagtVIqzY' # Replace with your Token
BOT_USERNAME: Final = '@LichessChallengeBot'
# Lets us use the /start command
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Hello there! I can create Lichess Challenges for you in groups')
# Lets us use the /help command
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text('Try typing anything and I will do my best to respond!')
async def update(update: Update, context: ContextTypes.DEFAULT_TYPE):
msg: str = update.message.text
dig = str(update)
dig = dig[dig.find("first_name"):dig.find("is_bot") - 2]
dig = dig[dig.rfind("=") + 1:]
Fuser=(list)(msg[7:].split())
print(Fuser)
with open('Keyss.json') as json_file:
AllKeys = json.load(json_file)
AllKeys[dig]=Fuser[0]
print(AllKeys)
json_data = json.dumps(AllKeys)
# Write JSON string to a file
with open("keyss.json", "w") as outfile:
outfile.write(json_data)
await update.message.reply_text(f"Updated your Key")
# Lets us use the /custom command
async def create_challenge(challenger: int , username: str , color:str , time_limit:int , time_increment:int):
# AllKeys={}
# AllKeys["bosscoder"]="lip_w8yh7plyueuOgsV4IGbF"
# AllKeys["animewilldiesoon"]="lip_Xo1vvyGGIJDCQ3ylpUR9"
# AllKeys["gambit_bfold"]="lip_SD8tMjuNPKMWD1fnysJt"
# AllKeys["maggie619"]="lip_KsWKHq7GoEO8yhDJF4Be"
# AllKeys["starboy_001"]="lip_zl4fx2iNlbW5CZAuBLzc"
#Loading json file
with open('Keyss.json') as json_file:
AllKeys = json.load(json_file)
client = APIClient(token=AllKeys[(str)(challenger)]) # Replace with your Token
color=str(color)
response = await client.challenges.create(username=username, color=color , rated=True, time_limit=time_limit , time_increment=time_increment)
print((response))
print("lichess.org/"+username)
response=str(response)
challenge_url_index=response.rfind('/');
end=challenge_url_index+1
while(response[end]!="'"): end+=1
return response[challenge_url_index+1:end]
async def challenge(update: Update, context: ContextTypes.DEFAULT_TYPE):
dig=str(update)
dig=dig[dig.find("first_name"):dig.find("is_bot")-2]
dig=dig[dig.rfind("=")+1:]
print(dig)
msg: str = update.message.text
Fuser=(list)(msg[11:].split())
print(Fuser)
col=""
if len(Fuser[1])==5:
col=Fuser[1]
challenge_id = await create_challenge(challenger=dig,username=Fuser[0],color=col,time_limit=Fuser[-2],time_increment=Fuser[-1])
if challenge_id != "plain" and challenge_id !='json':
challenge_id = 'https://lichess.org/' + challenge_id
print(f"Challenge created! Challenge ID: {challenge_id}")
# await update.message.reply_text('Create your own game nub')
await update.message.reply_text(f"Challenge created! Challenge ID: {challenge_id}")
else:
if challenge_id=='plain':
await update.message.reply_text(f"Too Many Reqs")
else:
await update.message.reply_text(f"Account does not exist or closed")
# Log errors
async def error(update: Update, context: ContextTypes.DEFAULT_TYPE):
print(f'Update {update} caused error {context.error}')
# Run the program
if __name__ == '__main__':
app = Application.builder().token(TOKEN).build()
# Commands
app.add_handler(CommandHandler('start', start_command))
app.add_handler(CommandHandler('help', help_command))
app.add_handler(CommandHandler('challenge', challenge))
app.add_handler(CommandHandler('update', update))
#app.add_handler(CommandHandler('createrandom', createRand))
# Messages
#app.add_handler(MessageHandler(filters.TEXT, handle_message))
# Log all errors
app.add_error_handler(error)
print('Polling...')
# Run the bot
app.run_polling(poll_interval=5)