Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
PointlessUser committed Oct 17, 2023
1 parent a50d5df commit e61a698
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
22 changes: 19 additions & 3 deletions Connect4Bot/Bumps/BumpInputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ def process_bump(bumps, chat_message: IncomingGroupChatMessage, bumpResponse):

message = chat_message.body.lower()

if chat_message.group_jid in bumps and (bumps[chat_message.group_jid]["bump"] is True) and message != "toggle bump":
if chat_message.group_jid in bumps and (bumps[chat_message.group_jid]["bump"] is True):
bump = bumps[chat_message.group_jid]
bump["timer"].reset()
bump["bumpCount"] = 0
return ''


if message == "toggle bump":
if chat_message.group_jid in bumps:
bumps[chat_message.group_jid]["bump"] = not bumps[chat_message.group_jid]["bump"]
Expand All @@ -23,3 +21,21 @@ def process_bump(bumps, chat_message: IncomingGroupChatMessage, bumpResponse):
else:
bumps[chat_message.group_jid] = {"timer":Timer(60*60, bumpResponse), "bumpCount":0, "bump":True,}
return "Bump will trigger every 60 mins"

elif message.startswith("set bump "):
print("set bump")
time = message.split("set bump")[1].strip()
try:
time = int(time)
except ValueError:
return "Invalid time"

if chat_message.group_jid in bumps and bumps[chat_message.group_jid]["bump"] and time >= 1:
bumps[chat_message.group_jid]["timer"] = Timer(time*60, bumpResponse)
if time >= 1:
bumps[chat_message.group_jid]["timer"] = Timer(time*60, bumpResponse)
bumps[chat_message.group_jid]["timer"].reset()
return f"Bump will trigger every {time} min"+("s" if time > 1 else "")
return "Cannot set bump to less than 1 min"
else:
return "Bump is currently disabled"
31 changes: 19 additions & 12 deletions Connect4Bot/Connect4/CheckInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,21 @@ def take_action(games: dict[Connect4Game], action: int, chat_message: IncomingGr
if action == 2:
return message[5:]

# How to use the bot
# How to use the bot (help)
if action == 3:
return """Commands:
connect or C to join game
(connect #) or (C#) to make a move
(example: connect 1 or C2)
reset to reset game
ping to check if server is running
echo to echo message
Type C to start/join a game
Type the number to make a move (1-7)
Type reset to reset game
Type 'lb' or 'leaderboard' to see the leaderboard
Type help to see this message
Type 'start #' to start a game with # in a row (e.g. 'start 5' starts connect 5)
Type 'Toggle Bump' to bump the group every hour
Type 'Set Bump #' to set the bump interval to # minutes
Type ping to check if server is running
Type echo to echo a message
Type 'gif (search term)' to send a gif (e.g. 'gif cat' sends a gif of a cat)
"""

# leaderboard
Expand Down Expand Up @@ -152,11 +158,11 @@ def play_move(move, games: dict[Connect4Game], chat_message: IncomingGroupChatMe

group_jid = chat_message.group_jid
player_jid = chat_message.from_jid

# check if game exists
if group_jid in games:
game: Connect4Game = games[group_jid]
else:
elif not chat_message.body.lower().startswith("connect"):
return "No game in progress"

# play move
Expand All @@ -171,7 +177,8 @@ def play_move(move, games: dict[Connect4Game], chat_message: IncomingGroupChatMe

# no game in progress
elif response == 1:
return "No game in progress"
if not chat_message.body.lower().startswith("connect"):
return "No game in progress"

# invalid move (out of bounds)
elif response == 2:
Expand All @@ -198,13 +205,13 @@ def play_move(move, games: dict[Connect4Game], chat_message: IncomingGroupChatMe
except Exception:
leaderboard = {}
dump(leaderboard, open(leaderboard_file, 'wb'))

if game.get_winner() in leaderboard:
leaderboard[game.get_winner()][0] = game.get_winner_name()
leaderboard[game.get_winner()][1] += 1
else:
leaderboard[game.get_winner()] = [game.get_winner_name(), 1]

dump(leaderboard, open(leaderboard_file, 'wb'))
return game.__str__(), f"{game.get_winner_name()} won!"

Expand Down
27 changes: 15 additions & 12 deletions Connect4Bot/start.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import yaml
import cProfile

from functools import partial

Expand All @@ -14,7 +15,6 @@
from Bumps.bumpQuotes import getQuote
from Connect4.CheckInput import *


bumpTime = 60 * 60 # 1 hour

games = {} # Map of JID to Connect4Game
Expand Down Expand Up @@ -46,20 +46,19 @@ def __init__(self, creds: dict):
device_id = creds.get("device_id")
android_id = creds.get("android_id")
self.tenor_key = creds.get("tenor_key")

self.senderJID = None
self.senderName = None
self.groupJID = None

# start bot
self.client = KikClient(self, username, password, kik_node, device_id, android_id, logging=True)
self.client = KikClient(self, username, password, kik_node, device_id, android_id, logging=True, debug=True)
# self.client = KikClient(self, username, password, kik_node, logging=True)
self.client.wait_for_messages()

# Initialization and Authentication
def on_authenticated(self):
print("Now I'm Authenticated, let's request roster")
# self.client.request_roster()
self.client.request_roster()


def bump(self, bumpJID):
Expand All @@ -73,7 +72,7 @@ def bump(self, bumpJID):
def on_group_message_received(self, chat_message: IncomingGroupChatMessage):
"""Called when a group chat message is received"""
# reset timer if message is from bump group

print("---------------------------------------------------------------")
self.senderJID = chat_message.from_jid
self.senderName = chat_message.from_jid.split('@')[0]
self.groupJID = chat_message.group_jid
Expand All @@ -88,11 +87,13 @@ def on_group_message_received(self, chat_message: IncomingGroupChatMessage):
# if display name is needed, get it
if output == 'Display name needed':
self.chat_message = chat_message
if self.senderName == '3ajrtbkk3ybdun2wucyhedbwpapsxtxupbnlhnghlbxaugdblerq_a':
self.play_with_custom_display_name("Yvaine")
else:
self.client.xiphias_get_users_by_alias(self.senderJID)

# if self.senderName == '3ajrtbkk3ybdun2wucyhedbwpapsxtxupbnlhnghlbxaugdblerq_a':
# self.play_with_custom_display_name("Yvaine")
# elif self.senderName == 'vbalbn4l5embfll4sfd3b2tnfih2hvmsgm7s7t3z6emnh53wvhxq_a':
# self.play_with_custom_display_name("Ahri")
# else:
# self.client.xiphias_get_users_by_alias(self.senderJID)
self.client.xiphias_get_users_by_alias(self.senderJID)
# if message isnt invalid action or display name needed, send message
elif output != '':
if type(output) is tuple:
Expand All @@ -116,7 +117,6 @@ def on_register_error(self, response: SignUpError):
print(f"[-] Register error: {response.message}")

def on_xiphias_get_users_response(self, response):
print(response)
for user in response.users:
name = user.display_name

Expand All @@ -138,6 +138,7 @@ def on_xiphias_get_users_response(self, response):
def play_with_custom_display_name(self, name = None):
if name is None:
name = self.senderName
print(self.senderName)

response = start_game(games, name, self.chat_message)
if type(response) is tuple:
Expand All @@ -153,4 +154,6 @@ def on_disconnected(self):


if __name__ == "__main__":
main()
def my_function():
main()
cProfile.run('my_function()')

0 comments on commit e61a698

Please sign in to comment.