-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbedbotControl.py
More file actions
146 lines (122 loc) · 5.65 KB
/
bedbotControl.py
File metadata and controls
146 lines (122 loc) · 5.65 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os
import subprocess
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
# Same optional local module as the main bot (gitignored)
try:
import private_config as _private_config # type: ignore
except ImportError:
_private_config = None
def _parse_trusted_ids_from_env():
raw = (os.getenv("BEDBOT_CONTROL_TRUSTED_IDS") or "").strip()
if not raw:
return []
out = []
for part in raw.split(","):
part = part.strip()
if part:
try:
out.append(int(part))
except ValueError:
print(f"Skipping invalid ID in BEDBOT_CONTROL_TRUSTED_IDS: {part!r}")
return out
def _resolve_control_trusted_ids():
"""User IDs for /start, /restart, /shutdown — `.env` first, then optional private_config."""
ids = _parse_trusted_ids_from_env()
if ids:
return ids
if _private_config is not None:
pc_ids = getattr(_private_config, "BEDBOT_CONTROL_TRUSTED_IDS", None)
if pc_ids:
try:
return [int(x) for x in pc_ids]
except (TypeError, ValueError):
print("private_config.BEDBOT_CONTROL_TRUSTED_IDS must be a list of integers.")
return []
CONTROL_TRUSTED_IDS = _resolve_control_trusted_ids()
intents = discord.Intents.default()
bot = commands.Bot(command_prefix="!", intents=intents)
def get_bot_token():
token = (os.getenv("DISCORD_TOKEN") or "").strip()
if not token and _private_config is not None:
raw = getattr(_private_config, "DISCORD_TOKEN", None)
if raw is not None and str(raw).strip():
token = str(raw).strip()
if not token:
print(
"Bot token not found. Set DISCORD_TOKEN in your `.env` file "
"(optional fallback: private_config.py — see private_config.example.py)."
)
return None
return token
def _is_trusted(user_id: int) -> bool:
if not CONTROL_TRUSTED_IDS:
return False
return user_id in CONTROL_TRUSTED_IDS
@bot.event
async def on_ready():
print(f"Control bot logged in as {bot.user}")
if not CONTROL_TRUSTED_IDS:
print(
"Warning: no trusted user IDs configured. Set BEDBOT_CONTROL_TRUSTED_IDS in `.env` "
"(comma-separated) or in private_config.py (see private_config.example.py)."
)
try:
await bot.tree.sync()
print("Slash commands synced successfully!")
except Exception as e:
print(f"Failed to sync slash commands: {e}")
@bot.tree.command(name="start", description="Start the main bot using PM2")
async def start(interaction: discord.Interaction):
"""Slash command to start the main bot process using PM2."""
if not _is_trusted(interaction.user.id):
await interaction.response.send_message("You do not have permission to start the bot.")
return
try:
await interaction.response.send_message("Starting the main bot...", ephemeral=False)
print("Starting the main bot process via pm2...")
subprocess.run(["pm2", "start", "disBedBot"], check=True)
print("Main bot process successfully started via pm2.")
await interaction.followup.send("Main bot started successfully!", ephemeral=False)
except subprocess.CalledProcessError as e:
print(f"Failed to start main bot via pm2: {e}")
await interaction.followup.send("Failed to start the main bot. Please check the logs.", ephemeral=False)
@bot.tree.command(name="restart", description="Restart the main bot using PM2")
async def restart(interaction: discord.Interaction):
"""Slash command to restart the main bot process using PM2."""
if not _is_trusted(interaction.user.id):
await interaction.response.send_message("You do not have permission to restart the bot.")
return
try:
await interaction.response.send_message("Restarting the main bot...", ephemeral=False)
print("Restarting the main bot process via pm2...")
subprocess.run(["pm2", "restart", "disBedBot"], check=True)
print("Main bot process successfully restarted via pm2.")
await interaction.followup.send("Main bot restarted successfully!", ephemeral=False)
except subprocess.CalledProcessError as e:
print(f"Failed to restart main bot via pm2: {e}")
await interaction.followup.send("Failed to restart the main bot. Please check the logs.", ephemeral=False)
@bot.tree.command(name="shutdown", description="Stop the main bot using PM2")
async def shutdown(interaction: discord.Interaction):
"""Slash command to stop the main bot process using PM2."""
if not _is_trusted(interaction.user.id):
await interaction.response.send_message("You do not have permission to shut down the bot.")
return
try:
await interaction.response.send_message("Shutting down the main bot...", ephemeral=False)
print("Stopping the main bot process via pm2...")
subprocess.run(["pm2", "stop", "disBedBot"], check=True)
print("Main bot process successfully stopped via pm2.")
await interaction.followup.send("Main bot shut down successfully!", ephemeral=False)
except subprocess.CalledProcessError as e:
print(f"Failed to shut down main bot via pm2: {e}")
await interaction.followup.send("Failed to shut down the main bot. Please check the logs.", ephemeral=False)
if __name__ == "__main__":
token = get_bot_token()
if token:
bot.run(token)
else:
print("Bot token not found. Exiting...")