<commit_before># This is the master class for the Python PS bot.
# Every general-purpose command is included in this file, with the sole exception
# being onMessage(), as derived applications may need to acces this function.
#
# As such, unless a valid onMessage() function is supplied when creating an
# instance, this will not run.
#
# To run this, the following modules are required:
# PyYAML == 3.11
# requests == 2.5.1
# simplejson == 3.6.5
# websocket-client == 0.23.0

import websocket
import requests
import json
import yaml
from time import sleep
import datetime
import re

from room import Room
from user import User
from plugins.battling.battleHandler import BattleHandler

# Module global for automatically update help with the correct symbol in plugins.
# This will be change from default on creation of every instance of PokemonShowdownBot!
guidechar = ' '

class PokemonShowdownBot:
    ''' Controls the most basic aspects of connecting to Pokemon Showdown as well as commands '''

    def __init__(self, url, onMessage = None):
        global guidechar
        try:
            with open("details.yaml", 'r') as yaml_file:
                self.details = yaml.load(yaml_file)
        except FileNotFoundError:
            self.loadDefaults()

        self.owner = self.toId(self.details['master'])
        self.apikeys = self.details['apikeys']
        self.name = self.details['user']
        self.id = self.toId(self.name)
        self.rooms = {}
        self.commandchar = self.details['command']
        self.startTime = None
        self.intro()
        self.splitMessage = onMessage if onMessage else self.onMessage
        self.url = url
        #websocket.enableTrace(True)
        self.openConnection()
        guidechar = self.commandchar

    def onError(self, ws, error):
        print('Websocket Error:', error)
    def onClose(self, message):
        self.rooms = {}
        print('Websocket closed')

    def onOpen(self, message):
        print('Websocket opened')

    def openConnection(self):
        if not self.url: return
        self.ws = websocket.WebSocketApp(self.url,
                                         on_message = self.splitMessage,
                                         on_error = self.onError,
                                         on_close = self.onClose)
        self.ws.on_open = self.onOpen
        self.bh = BattleHandler(self.ws, self.name)

    def closeConnection(self):
        self.ws.close()
        self.ws = None

    def listen(self):
        self.ws.run_forever()

    def loadDefaults(self):
        import shutil
        shutil.copy('details-example.yaml', 'details.yaml')
        with open("details.yaml", 'r') as yaml_file:
                self.details = yaml.load(yaml_file)

    def intro(self):
        print('+~~~~~~~~~~~~~~~~~~~~~~~~+')
        print('|  Pokemon Showdown Bot  |')
        print('|      Created by:       |')
        print('|      Quite Quiet       |')
        print('+~~~~~~~~~~~~~~~~~~~~~~~~+')
    def log(self, sort, msg, user):
        print('{sort}: {cmd} (user: {user})'.format(sort = sort, cmd = msg, user = user))
    def userIsSelf(self, user):
        return self.name == user

    def send(self, msg):
        self.ws.send(msg)
<commit_msg>Send pings to not time out connection<commit_after># This is the master class for the Python PS bot.
# Every general-purpose command is included in this file, with the sole exception
# being onMessage(), as derived applications may need to acces this function.
#
# As such, unless a valid onMessage() function is supplied when creating an
# instance, this will not run.
#
# To run this, the following modules are required:
# PyYAML == 3.11
# requests == 2.5.1
# simplejson == 3.6.5
# websocket-client == 0.23.0

import websocket
import requests
import json
import yaml
from time import sleep
import datetime
import re

from room import Room
from user import User
from plugins.battling.battleHandler import BattleHandler

# Module global for automatically update help with the correct symbol in plugins.
# This will be change from default on creation of every instance of PokemonShowdownBot!
guidechar = ' '

class PokemonShowdownBot:
    ''' Controls the most basic aspects of connecting to Pokemon Showdown as well as commands '''

    def __init__(self, url, onMessage = None):
        global guidechar
        try:
            with open("details.yaml", 'r') as yaml_file:
                self.details = yaml.load(yaml_file)
        except FileNotFoundError:
            self.loadDefaults()

        self.owner = self.toId(self.details['master'])
        self.apikeys = self.details['apikeys']
        self.name = self.details['user']
        self.id = self.toId(self.name)
        self.rooms = {}
        self.commandchar = self.details['command']
        self.startTime = None
        self.intro()
        self.splitMessage = onMessage if onMessage else self.onMessage
        self.url = url
        #websocket.enableTrace(True)
        self.openConnection()
        guidechar = self.commandchar

    def onError(self, ws, error):
        print('Websocket Error:', error)
    def onClose(self, message):
        self.rooms = {}
        print('Websocket closed')

    def onOpen(self, message):
        print('Websocket opened')

    def openConnection(self):
        if not self.url: return
        self.ws = websocket.WebSocketApp(self.url,
                                         on_message = self.splitMessage,
                                         on_error = self.onError,
                                         on_close = self.onClose)
        self.ws.on_open = self.onOpen
        self.bh = BattleHandler(self.ws, self.name)

    def closeConnection(self):
        self.ws.close()
        self.ws = None

    def listen(self):
        self.ws.run_forever(ping_interval = 120, ping_interval = 40)

    def loadDefaults(self):
        import shutil
        shutil.copy('details-example.yaml', 'details.yaml')
        with open("details.yaml", 'r') as yaml_file:
                self.details = yaml.load(yaml_file)

    def intro(self):
        print('+~~~~~~~~~~~~~~~~~~~~~~~~+')
        print('|  Pokemon Showdown Bot  |')
        print('|      Created by:       |')
        print('|      Quite Quiet       |')
        print('+~~~~~~~~~~~~~~~~~~~~~~~~+')
    def log(self, sort, msg, user):
        print('{sort}: {cmd} (user: {user})'.format(sort = sort, cmd = msg, user = user))
    def userIsSelf(self, user):
        return self.name == user

    def send(self, msg):
        self.ws.send(msg)

{'fraction_non_alphanumeric': 0.11534359286911473, 'fraction_numerical': 0.004723449641932043, 'mean_word_length': 2.355828220858896, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '840621', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1783, 'n_words': 563}