-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
185 lines (140 loc) · 5.91 KB
/
main.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import logging
from rich import print
from discord import Interaction, InteractionResponded, Message, Intents
from discord.ext import commands
from condor import release
from condor.server_manager import (
OnlineStatus,
get_server_status,
start_server,
stop_server,
)
from condor.config import check_config, get_config
from services.agent import on_files_upload, on_list_flight_plans, on_status
from services.dialogs import (
SelectStartFlightPlan,
SelectViewFlightPlan,
handle_error,
send_response,
)
intents = Intents.default()
intents.messages = True
intents.guilds = True
intents.message_content = True
bot = commands.Bot(command_prefix="/", intents=intents)
logger = logging.getLogger("main")
prefix = get_config().command_prefix
@bot.tree.command(name=f"{prefix}help", description="Display the help")
async def condor(interaction: Interaction):
# cmd_prefix
msg = f"""
**Condor 3 bot help** - *v{release.version}*
```
Commands:
"""
for command in bot.tree.get_commands():
msg += f"{command.name:10s} {command.description}\n"
msg += """
```
Upload: just send a new Flight Plan (ex: MyFlightPlan.fpl) to this channel.
"""
await send_response(interaction, msg)
@bot.event
async def on_ready():
await bot.tree.sync()
print(f"✅ bot is logged in as {bot.user}")
@bot.tree.command(name=f"{prefix}ping", description="Simple Ping Pong Test command")
async def ping(interaction: Interaction):
await send_response(interaction, "Pong! 🏓")
@bot.tree.command(name=f"{prefix}start", description="Start condor 3 server")
async def start(interaction: Interaction):
status, _ = get_server_status()
if status.online_status != OnlineStatus.OFFLINE.value:
await handle_error(interaction, "server is already running, server should be stopped first")
return
try:
view = SelectStartFlightPlan(interaction.user)
await send_response(interaction, "📋 Select a flight plan:", view=view)
await view.wait()
if view.response:
flight_plan = view.response
start_server(flight_plan)
await send_response(
interaction,
f"✅ server started with flight plan {flight_plan} by **{interaction.user.display_name}**",
channel_message=True,
)
await interaction.delete_original_response()
else:
await send_response(interaction, "⏳ elapsed time, operation cancelled.")
except InteractionResponded as already_responded:
print(f"[red]{already_responded}[/red]")
except Exception as exc:
print(f"[red]{exc}[/red]")
await handle_error(interaction, f"an error occured, server not started: {exc}")
@bot.tree.command(name=f"{prefix}status", description="Display condor 3 server status")
async def status(interaction: Interaction):
await on_status(interaction)
@bot.tree.command(name=f"{prefix}stop", description="Stop condor 3 server")
async def stop(interaction: Interaction):
try:
status, _ = get_server_status()
if status.online_status == OnlineStatus.OFFLINE.value:
await handle_error(interaction, "server is not running, so it couldn't be stopped")
return
if status.online_status == OnlineStatus.NOT_RUNNING.value or len(status.players) == 0:
await send_response(interaction, "stopping", delete_after=1)
stop_server()
await send_response(
interaction, f"🔴 server stopped by **{interaction.user.display_name}**", channel_message=True
)
else:
await handle_error(
interaction, f"server couldn't be stopped, {len(status.players)} player(s) are connected"
)
except InteractionResponded as already_responded:
print(f"[red]{already_responded}[/red]")
except Exception as exc:
print(f"[red]{exc}[/red]")
await handle_error(interaction, f"an error occured, server not stopped: {exc}")
@bot.tree.command(name=f"{prefix}list", description="List flight plans available")
async def _list(interaction: Interaction):
await on_list_flight_plans(interaction)
@bot.tree.command(name=f"{prefix}show", description="Show informations about a flightplan")
async def show(interaction: Interaction):
try:
view = SelectViewFlightPlan(interaction.user)
await send_response(interaction, "📋 Select a flight plan:", view=view)
await view.wait()
if not view.response:
await send_response(interaction, "⏳ elapsed time, operation cancelled.")
except InteractionResponded as already_responded:
print(f"[red]{already_responded}[/red]")
except Exception as exc:
print(f"[red]{exc}[/red]")
await handle_error(interaction, f"an error occured, server not started: {exc}")
@bot.event
async def on_message(message: Message):
config = get_config()
if message.author == bot.user or message.channel.id != config.discord.admin_channel_id:
return
print(f"[yellow]message {message.author}[/yellow]@[blue]{message.channel.name}[/blue]: {message.content}")
if message.attachments:
await on_files_upload(message)
await bot.process_commands(message) # hack to propagate message to commands
def main():
print(f"Starting Condor 3 Discord Bot - v{release.version}")
try:
config = get_config()
check_config(config)
except Exception as e:
print(f"[red]error loading configuration[/red]: {e}")
return
print(f"[yellow]admin channel[/yellow]: [blue]{config.discord.admin_channel_id}[/blue]")
print(f"[yellow]command prefix[/yellow]: [blue]{config.command_prefix}[/blue]")
print("[yellow]registered commands[/yellow]:")
for command in bot.tree.get_commands():
print(f" - [blue]{command.name}[/blue] {command.description}")
bot.run(config.discord.api_token)
if __name__ == "__main__":
main()