-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMcDiscordTerminal.py
More file actions
183 lines (137 loc) · 7 KB
/
McDiscordTerminal.py
File metadata and controls
183 lines (137 loc) · 7 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
#--------------------------------------------------------Imports----------------------------------------------------#
import os
from Lib.utils import *
from discord.ext import commands, tasks
from Lib.hachiko.hachiko import AIOWatchdog, AIOEventHandler
#--------------------------------------------------------Classes-----------------------------------------------------#
class Terminal():
def __init__(self, path_to_log, channel_id, message_id, bot):
self.path_to_log = path_to_log
self.channel_id = channel_id
self.message_id = message_id
self.bot = bot
async def send_message(self, message, bot=None, channel_id=None):
if channel_id is None: channel_id = self.channel_id
await bot.wait_until_ready()
channel = bot.get_channel(int(channel_id))
await channel.send(message)
async def edit_message(self, message, bot=None, channel_id=None, message_id=None):
if bot is None: bot = self.bot
if channel_id is None: channel_id = self.channel_id
if message_id is None: message_id = self.message_id
await bot.wait_until_ready()
channel = bot.get_channel(int(channel_id))
msg = await channel.fetch_message(int(message_id))
await msg.edit(content=message)
async def send_terminal(self, bot=None, channel_id=None, message_id=None, path_to_log=None):
if bot is None: bot = self.bot
if channel_id is None: channel_id = self.channel_id
if message_id is None: message_id = self.message_id
if path_to_log is None: path_to_log = self.path_to_log
lastest_lignes_terminal = getLastestLines(file_path=path_to_log, number_of_lines=15)
msg = f"""```\n{lastest_lignes_terminal}```"""
await self.edit_message(message=msg, bot=bot, channel_id=channel_id, message_id=message_id)
class Event_handeler_minecraft(AIOEventHandler):
async def on_modified(self, event):
if choosen_terminal.selected == 'minecraft':
await terminal_minecraft.send_terminal()
print('Modified minecraft')
class Event_handeler_shell(AIOEventHandler):
async def on_modified(self, event):
if choosen_terminal.selected == 'shell':
await terminal_shell.send_terminal()
print('Modified shell')
class Used_terminal():
selected = 'minecraft'
def change_to_minecraft(self):
self.selected = 'minecraft'
def change_to_shell(self):
self.selected = 'shell'
#--------------------------------------------Creating const and objects----------------------------------------------#
# extract the param from the param file
PATH_TO_PARAM = f'{os.path.realpath(os.path.dirname(__file__))}/param.txt'
PARAM = text2dic(file_path=PATH_TO_PARAM, separator='=')
# seting the way to trigger the bot
bot = commands.Bot(command_prefix='!')
terminal_minecraft = Terminal(path_to_log=f'{os.path.realpath(os.path.dirname(__file__))}/Lib/terminal_minecraft/terminal_minecraft.txt',
channel_id=PARAM['id_channel_terminal'],
message_id=PARAM['id_message_terminal'],
bot=bot)
terminal_shell = Terminal(path_to_log=f'{os.path.realpath(os.path.dirname(__file__))}/Lib/terminal_shell/terminal_shell.txt',
channel_id=PARAM['id_channel_terminal'],
message_id=PARAM['id_message_terminal'],
bot=bot)
# creating terminal selector
choosen_terminal = Used_terminal()
# creating handelers
event_handeler_minecraft = Event_handeler_minecraft()
event_handeler_shell = Event_handeler_shell()
# creation and starting handeler for minecraft
observer_minecraft = AIOWatchdog(event_handler=event_handeler_minecraft,
path=f'{os.path.realpath(os.path.dirname(__file__))}/Lib/terminal_minecraft/',
recursive=True)
observer_minecraft.start()
# creation and starting handeler for shell
observer_shell = AIOWatchdog(event_handler=event_handeler_shell,
path=f'{os.path.realpath(os.path.dirname(__file__))}/Lib/terminal_shell/',
recursive=True)
observer_shell.start()
#-------------------------------------------------Terminals commands-------------------------------------------------#
@bot.event
async def on_ready():
print("Terminal Discord is ready !")
commande = rf"""sudo -u {PARAM['user_name']} screen -S {PARAM['screen_shell_session_name']} -X stuff 'cd ~^M'"""
os.system(commande)
@bot.command(name='m')
async def commande_minecraft(ctx, *, commande_minecraft=''):
try:
if str(ctx.channel) == "terminal":
await ctx.message.delete()
choosen_terminal.change_to_minecraft()
# executing the command in the minecraft screen
commande = rf"""sudo -u {PARAM['user_name']} screen -S {PARAM['screen_minecraft_session_name']} -X stuff '{commande_minecraft}^M'"""
os.system(commande)
except:
print('a problem has occurred while trying to send the commend to the minecraft terminal')
@bot.command(name='s')
async def commande_shell(ctx, *, commande_shell=''):
try:
if str(ctx.channel) == "terminal":
await ctx.message.delete()
choosen_terminal.change_to_shell()
# executing the command in the shell screen
commande = rf"""sudo -u {PARAM['user_name']} screen -S {PARAM['screen_shell_session_name']} -X stuff '{commande_shell}^M'"""
os.system(commande)
except:
print('a problem has occurred while trying to send the commend to the shell terminal')
@bot.command(name='tm')
async def show_minecraft_teminal(ctx):
try:
if str(ctx.channel) == "terminal":
await ctx.message.delete()
choosen_terminal.change_to_minecraft()
await terminal_minecraft.send_terminal()
print('the minecraft terminal as been successfully display')
except:
print("a problem has occurred while trying to display the minecraft terminal")
@bot.command(name='ts')
async def show_shell_teminal(ctx):
try:
if str(ctx.channel) == "terminal":
await ctx.message.delete()
choosen_terminal.change_to_shell()
await terminal_shell.send_terminal()
print('the shell terminal as been successfully display')
except:
print("a problem has occurred while trying to display the shell terminal")
@tasks.loop(seconds=30)
async def loop_terminals():
await bot.wait_until_ready()
if bot.is_closed():
observer_minecraft.stop()
observer_minecraft.join()
observer_shell.stop()
observer_shell.join()
#--------------------------------------------------Starting loops----------------------------------------------------#
loop_terminals.start()
bot.run(PARAM['token'])