More debug data
This commit is contained in:
parent
2a69b6195f
commit
307bcc0dc0
@ -16,18 +16,21 @@ from sqlite_utils.db import NotFoundError
|
||||
|
||||
from db import DiscordDB
|
||||
|
||||
APP_NAME = "discord_bot"
|
||||
|
||||
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
|
||||
load_dotenv()
|
||||
logging.basicConfig(level=logging.WARNING, filename="logging.log",
|
||||
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
|
||||
logger = logging.getLogger(APP_NAME)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
logger.propagate = False
|
||||
fh = logging.FileHandler(f"./logging2.log", "w")
|
||||
fh.setLevel(logging.DEBUG)
|
||||
logger.addHandler(fh)
|
||||
keep_fds = [fh.stream.fileno()]
|
||||
|
||||
sys.stdout = open('output.log', 'a')
|
||||
sys.stderr = open('error.log', 'a')
|
||||
os.chdir(os.path.abspath(os.path.dirname(sys.argv[0])))
|
||||
pid = str(os.getpid())
|
||||
pidfile = "pid"
|
||||
|
||||
with open(pidfile, 'w') as f:
|
||||
f.write(str(os.getpid()))
|
||||
pidfile = f"/tmp/{APP_NAME}-{os.getpid()}.pid"
|
||||
|
||||
DISCORD_TOKEN = os.getenv("DISCORD_TOKEN")
|
||||
DB_NAME = os.getenv('DB_NAME', 'discord.db')
|
||||
@ -59,7 +62,7 @@ FLAGS = {1: 'flag_ro', 9: 'flag_br', 10: 'flag_it', 11: 'flag_fr', 12: 'flag_de'
|
||||
168: 'flag_ge', 169: 'flag_am', 170: 'flag_ng', 171: 'flag_cu'}
|
||||
|
||||
|
||||
__last_battle_request = None
|
||||
__last_battle_response = None
|
||||
__last_battle_update_timestamp = 0
|
||||
|
||||
|
||||
@ -68,12 +71,16 @@ def timestamp_to_datetime(timestamp: int) -> datetime.datetime:
|
||||
|
||||
|
||||
def get_battle_page():
|
||||
global __last_battle_update_timestamp, __last_battle_request
|
||||
global __last_battle_update_timestamp, __last_battle_response
|
||||
if int(datetime.datetime.now().timestamp()) >= __last_battle_update_timestamp + 60:
|
||||
r = requests.get('https://erep.lv/battles.json').json()
|
||||
__last_battle_request = r
|
||||
__last_battle_update_timestamp = r.get('last_updated', int(datetime.datetime.now().timestamp()))
|
||||
return __last_battle_request
|
||||
r = requests.get('https://erep.lv/battles.json')
|
||||
__last_battle_response = r.json()
|
||||
__last_battle_update_timestamp = __last_battle_response.get('last_updated', int(datetime.datetime.now().timestamp()))
|
||||
d = timestamp_to_datetime(__last_battle_update_timestamp//3600*3600)
|
||||
os.makedirs(f"{d:%F/%H}/", exist_ok=True)
|
||||
with open(f"{d:%F/%H}/{__last_battle_update_timestamp}.json", 'w') as f:
|
||||
f.write(r.text)
|
||||
return __last_battle_response
|
||||
|
||||
|
||||
def check_player(player_id: int) -> bool:
|
||||
@ -164,15 +171,22 @@ class MyClient(discord.Client):
|
||||
except Exception as e:
|
||||
await self.get_channel(603527159109124096).send("<@220849530730577920> Something bad has happened with"
|
||||
" medal hunter!")
|
||||
logging.error("Discord bot's eRepublik medal hunter died!", exc_info=e)
|
||||
logger.error("Discord bot's eRepublik medal hunter died!", exc_info=e)
|
||||
try:
|
||||
with open(f"{self.timestamp}.json", 'w') as f:
|
||||
f.write(r.text)
|
||||
except NameError:
|
||||
logger.error("There was no Response object!", exc_info=e)
|
||||
await asyncio.sleep(10)
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
loop = asyncio.get_event_loop()
|
||||
client = MyClient(loop=loop)
|
||||
bot = commands.Bot(command_prefix='!')
|
||||
|
||||
|
||||
|
||||
@bot.event
|
||||
async def on_ready():
|
||||
print('Bot loaded')
|
||||
@ -296,7 +310,16 @@ if __name__ == "__main__":
|
||||
if isinstance(error, commands.BadArgument):
|
||||
await ctx.send('spēlētāja identifikators jāpadod kā skaitliska vērtība, piemēram, 1620414')
|
||||
|
||||
|
||||
def main():
|
||||
global loop
|
||||
loop.create_task(bot.start(DISCORD_TOKEN))
|
||||
loop.create_task(client.start(DISCORD_TOKEN))
|
||||
loop.run_forever()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
# daemon = daemonize.Daemonize(APP_NAME, pidfile, main)
|
||||
# daemon.start()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user