11# bot.py
2- import os , glob , shlex
2+ import os
3+ import glob
4+ import shlex
5+ import time
36try :
47 import discord
58except ImportError :
912
1013def Parsecommand (message , permission ):
1114 command = shlex .split (message .content , posix = False )
12- #remove slash from command
15+ # remove slash from command
1316 command [0 ] = command [0 ][1 :]
14- #important this command is not common use as the return value is important
17+ # important this command is not common use as the return value is important
1518 if command [0 ] == "list" :
1619 global PATH
1720 minecraft_inject ("list^M" )
21+ time .sleep (2 )
1822 log = get_latest_file (str (PATH ) + "/logs/*" )
1923 try :
2024 with open (log , "r" ) as f :
@@ -31,42 +35,43 @@ def Parsecommand(message, permission):
3135 return "log file not found"
3236
3337 elif command [0 ] == "help" :
34- with open ("Commands.txt" , "r" ) as f :
35- text = f .read ().splitlines ()
36- text .pop (0 )
37- return text
38-
39- else :
4038 global COMMANDS
4139 global PERMISSIONS
40+ text = ""
41+ for i in range (len (COMMANDS )):
42+ text = text + COMMANDS [i ] + " - " + PERMISSIONS [i ] + "\n "
43+ return text
44+
45+ else :
4246 global RETURNS
4347 Permission_Ranking = ["Guest" , "User" , "Trustee" , "Admin" ]
44-
45- if not command [0 ] in COMMANDS : # checks for valid command
48+
49+ if not command [0 ] in COMMANDS : # checks for valid command
4650 return "Command not found. Try /help"
4751 index = COMMANDS .index (command [0 ])
4852
49- #check for acceptable permissions
50- if Permission_Ranking .index (PERMISSIONS [index ]) > Permission_Ranking .index (permission ):
53+ # check for acceptable permissions
54+ if Permission_Ranking .index (PERMISSIONS [index ]) > Permission_Ranking .index (permission ):
5155 return "You do not have permission to use this command"
5256
53- #actuate command
57+ # actuate command
5458 minecraft_inject (" " .join (command ) + "^M" )
5559
56- #return value
60+ # return value
5761 returnval = RETURNS [index ]
5862
5963 returnval = returnval .split (" " )
6064 for i in range (len (returnval )):
6165 if "*par" in returnval [i ]:
62- if (len (command ) - 1 >= int (returnval [i ][4 :])):
66+ if (len (command ) - 1 >= int (returnval [i ][4 :])):
6367 returnval [i ] = command [int (returnval [i ][4 :])]
6468 else :
6569 returnval [i ] = "default"
6670
6771 returnval = " " .join (returnval )
6872 return returnval
6973
74+
7075def minecraft_inject (message ):
7176 os .system ("screen -S Bedrock -p 0 -X stuff \" " + str (message ) + "\" " )
7277
@@ -76,7 +81,8 @@ def get_latest_file(path):
7681 latest_file = max (list_of_files , key = os .path .getctime )
7782 return latest_file
7883
79- try : #pull oauth token from file and other constants data
84+
85+ try : # pull oauth token from file and other constants data
8086 with open ("Creds.txt" , "r" ) as f :
8187 data = f .read ().splitlines ()
8288 TOKEN = data [0 ].split ("=" )[1 ]
@@ -86,7 +92,7 @@ def get_latest_file(path):
8692except FileNotFoundError :
8793 TOKEN = input ("Enter token: " )
8894
89- try : # import commands and permissions
95+ try : # import commands and permissions
9096 with open ("Commands.txt" , "r" ) as f :
9197 commands = f .read ().splitlines ()
9298 commands .pop (0 )
@@ -106,11 +112,11 @@ def get_latest_file(path):
106112 commands = []
107113 print ("Commands.txt not found" )
108114
109- #initiate discord bot
115+ # initiate discord bot
110116client = discord .Client (intents = discord .Intents .all ())
111117
112118
113- @client .event # on ready post to console
119+ @client .event # on ready post to console
114120async def on_ready ():
115121 for guild in client .guilds :
116122 if guild .name == GUILD :
@@ -125,7 +131,7 @@ async def on_ready():
125131 print (f'Guild Members:\n - { members } ' )
126132
127133
128- @client .event # on join guild DM them
134+ @client .event # on join guild DM them
129135async def on_member_join (member ):
130136 await member .create_dm ()
131137 await member .dm_channel .send (
@@ -134,18 +140,18 @@ async def on_member_join(member):
134140 await member .dm_channel .send ("Join the server at: remotehost.ddns.net" )
135141
136142
137- @client .event # on message
138- async def on_message (message ): # ignore self
143+ @client .event # on message
144+ async def on_message (message ): # ignore self
139145 if message .author == client .user :
140146 return
141147
142148 if "/" in message .content :
143149 roles = [role .name for role in message .author .roles ]
144- if ("Admin" in roles ):
150+ if ("Admin" in roles ):
145151 permission = 'Admin'
146- elif ("Trustee" in roles ):
152+ elif ("Trustee" in roles ):
147153 permission = 'Trustee'
148- elif ("User" in roles ):
154+ elif ("User" in roles ):
149155 permission = 'User'
150156 statement = Parsecommand (message , permission )
151157 if statement != None :
0 commit comments