This repository was archived by the owner on Apr 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
214 lines (192 loc) · 7.02 KB
/
bot.py
File metadata and controls
214 lines (192 loc) · 7.02 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import os
import logging
from discord.ext import commands
from utils.config import Config
from utils import amp
from utils import checks
config = Config()
desc = "Server control bot for {}".format(config.name)
bot = commands.Bot(command_prefix=config.command_prefix, description=desc, pm_help=False)
def init_console_logger():
logger = logging.getLogger("consolelogger")
format = logging.Formatter("%(asctime)s %(message)s")
fileHandler = logging.FileHandler("commands.log")
fileHandler.setFormatter(format)
streamHandler = logging.StreamHandler()
streamHandler.setFormatter(format)
logger.setLevel(logging.INFO)
logger.addHandler(fileHandler)
logger.addHandler(streamHandler)
init_console_logger()
console_logger = logging.getLogger("consolelogger")
class Server():
def __init__(self, bot):
self.bot = bot
@commands.command()
async def state(self):
"""Gets the server's current state"""
try:
state = amp.get_server_state()
except KeyError:
amp.get_session_id()
state = amp.get_server_state()
await self.bot.say("The server is **{}**".format(state))
@checks.is_senior_admin()
@commands.command()
async def start(self):
"""Starts the server"""
try:
state = amp.get_server_state()
except KeyError:
amp.get_session_id()
state = amp.get_server_state()
if state == "online":
await self.bot.say("The server is already running")
return
elif state == "starting":
await self.bot.say("The server is already starting")
return
else:
amp.control_power(amp.Power.START)
await self.bot.say("Starting the server...")
@checks.is_senior_admin()
@commands.command()
async def restart(self):
"""Restarts the server"""
try:
state = amp.get_server_state()
except KeyError:
amp.get_session_id()
state = amp.get_server_state()
if state == "starting":
await self.bot.say("The server is already starting")
return
elif state == "shutting down":
await self.bot.say("The server is already shutting down")
return
else:
amp.control_power(amp.Power.RESTART)
await self.bot.say("Restarting the server...")
@checks.is_senior_admin()
@commands.command()
async def stop(self):
"""Stops the server"""
try:
state = amp.get_server_state()
except KeyError:
amp.get_session_id()
state = amp.get_server_state()
if state == "offline":
await self.bot.say("The server is already stopped")
return
elif state == "shutting down":
await self.bot.say("The server is already shutting down")
return
else:
amp.control_power(amp.Power.STOP)
await self.bot.say("Stopping the server...")
@checks.is_senior_admin()
@commands.command()
async def kill(self):
"""Kills the server (useful if it isn't responding)"""
try:
state = amp.get_server_state()
except KeyError:
amp.get_session_id()
state = amp.get_server_state()
if state == "offline":
await self.bot.say("The server is already stopped")
return
else:
amp.control_power(amp.Power.KILL)
await self.bot.say("Killing the server...")
@commands.command()
async def list(self):
"""Gets the list of online players"""
try:
state = amp.get_server_state()
except KeyError:
amp.get_session_id()
state = amp.get_server_state()
if state == "offline":
await self.bot.say("The server is offline")
return
await self.bot.say(amp.get_player_list())
@checks.is_senior_admin()
@commands.command(pass_context=True)
async def sendcommand(self, ctx, *, command:str):
"""Send a console command"""
try:
state = amp.get_server_state()
except KeyError:
amp.get_session_id()
state = amp.get_server_state()
if state == "offline":
await self.bot.say("The server is offline")
return
amp.send_console_command(command)
console_logger.info("[Console Command] {}: {}".format(ctx.message.author, ctx.message.content.replace("{}{} ".format(config.command_prefix, ctx.command), "")))
await self.bot.say("Command sent!")
bot.add_cog(Server(bot))
@bot.event
async def on_command_error(error, ctx):
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.DisabledCommand):
await bot.send_message(ctx.message.channel, "This command has been disabled")
return
if isinstance(error, checks.dev_only):
await bot.send_message(ctx.message.channel, "This command can only be ran by the server developers")
return
if isinstance(error, checks.admin_only):
await bot.send_message(ctx.message.channel, "This command can only be ran by the discord admins")
return
if isinstance(error, checks.senior_admin_only):
await bot.send_message(ctx.message.channel, "This command can only be ran by the server senior admins")
return
# In case the bot failed to send a message to the channel, the try except pass statement is to prevent another error
try:
await bot.send_message(ctx.message.channel, error)
except:
pass
print("An error occured while executing the command named {}: {}".format(ctx.command.qualified_name, error))
@bot.event
async def on_ready():
print("Connected! Logged in as {}/{}".format(bot.user, bot.user.id))
amp.get_session_id()
@checks.is_dev()
@bot.command(hidden=True, pass_context=True)
async def debug(ctx, *, shit:str):
import asyncio
import requests
import random
py = "```py\n{}```"
"""This is the part where I make 20,000 typos before I get it right"""
# "what the fuck is with your variable naming" - EJH2
# seth seriously what the fuck - Robin
try:
rebug = eval(shit)
if asyncio.iscoroutine(rebug):
rebug = await rebug
await bot.say(py.format(rebug))
except Exception as damnit:
await bot.say(py.format("{}: {}".format(type(damnit).__name__, damnit)))
@checks.is_dev()
@bot.command(hidden=True, pass_context=True)
async def terminal(ctx, *, command:str):
"""Runs terminal commands and shows the output via a message. Oooh spoopy!"""
xl = "```xl\n{}```"
try:
await bot.send_typing(ctx.message.channel)
await bot.say(xl.format(os.popen(command).read()))
except:
await bot.say("Error, couldn't send command")
@checks.is_dev()
@bot.command(hidden=True)
async def shutdown():
"""Shuts down the bot"""
await bot.say("Shutting down...")
amp.logout()
await bot.logout()
print("Connecting...")
bot.run(config._token)