forked from JonnyPtn/zomboi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zomboi.py
83 lines (72 loc) · 2.62 KB
/
zomboi.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
# The main file for zomboi bot. Sets up and runs the discord client
from chat import ChatHandler
import discord
from discord.ext import commands
from dotenv import load_dotenv
import logging
from maps import MapHandler
import os
from pathlib import Path
from perks import PerkHandler
from users import UserHandler
from admin import AdminLogHandler
from rcon_adapter import RCONAdapter
load_dotenv(override=True)
# Verify the log path
logPath = os.getenv("LOGS_PATH")
if logPath is None or len(logPath) == 0:
path = Path.home().joinpath("Zomboid/Logs")
if path.exists():
logPath = str(path)
else:
logging.error("Zomboid log path not set and unable to find default")
exit()
# Our main bot object
intents = discord.Intents.default()
intents.members = True
intents.guilds = True
intents.message_content = True
zomboi = commands.bot.Bot("!", intents=intents)
# Redirect the discord log to a file
logFormat = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s: %(message)s")
discordLogger = logging.getLogger("discord")
discordLogger.setLevel(logging.DEBUG)
handler = logging.FileHandler(filename="discord.log", encoding="utf-8", mode="w")
handler.setFormatter(logFormat)
discordLogger.addHandler(handler)
# set up our logging
zomboi.log = logging.getLogger("zomboi")
handler = logging.StreamHandler()
handler.setFormatter(logFormat)
handler.setLevel(logging.INFO)
zomboi.log.addHandler(handler)
handler = logging.FileHandler(filename="zomboi.log")
handler.setFormatter(logFormat)
handler.setLevel(logging.DEBUG)
zomboi.log.addHandler(handler)
zomboi.log.setLevel(logging.DEBUG)
@zomboi.event
async def on_ready():
zomboi.log.info(f"We have logged in as {zomboi.user}")
channel = os.getenv("CHANNEL")
zomboi.channel = zomboi.get_channel(int(channel)) if channel.isdigit() else None # Find by id
if zomboi.channel is None:
zomboi.channel = discord.utils.get(
zomboi.get_all_channels(), name=channel
) # find by name
if zomboi.channel is None:
zomboi.log.warning("Unable to get channel, will not be enabled")
else:
zomboi.log.info("channel connected")
await zomboi.add_cog(UserHandler(zomboi, logPath))
await zomboi.add_cog(ChatHandler(zomboi, logPath))
await zomboi.add_cog(PerkHandler(zomboi, logPath))
await zomboi.add_cog(RCONAdapter(zomboi))
await zomboi.add_cog(MapHandler(zomboi))
await zomboi.add_cog(AdminLogHandler(zomboi, logPath))
# Always finally run the bot
token = os.getenv("DISCORD_TOKEN")
if token is None:
zomboi.log.error("DISCORD_TOKEN environment variable not found")
exit()
zomboi.run(os.getenv("DISCORD_TOKEN"))