Advertisement
willbedie

Ban System [MySQL R41-4]

Sep 2nd, 2018
1,774
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.74 KB | None | 0 0
  1. /*
  2. _ _
  3. ( ) ( )_
  4. | |_ _ _ ___ ___ _ _ ___ | ,_) __ ___ ___
  5. | '_`\ /'_` )/' _ `\ /',__)( ) ( )/',__)| | /'__`\/' _ ` _ `\
  6. | |_) )( (_| || ( ) | \__, \| (_) |\__, \| |_ ( ___/| ( ) ( ) |
  7. (_,__/'`\__,_)(_) (_) (____/`\__, |(____/`\__)`\____)(_) (_) (_)
  8. ( )_| |
  9. `\___/'
  10.  
  11. Advanced Ban / Unban system by willbedie.
  12. Commands: /ban, /unban, /baninfo, /oban
  13. Version: V0.2
  14. Credits: Y_Less, SA-MP, Zeex, maddinat0r, BlueG, Emmet, willbedie
  15. Last Updated: 9/4/2018 - 2:21 PM
  16. MySQL Version: R41-4*/
  17.  
  18. #define FILTERSCRIPT
  19.  
  20. #include <a_samp>
  21. #include <a_mysql>
  22. #include <zcmd>
  23. #include <sscanf2>
  24. #include <easyDialog>
  25.  
  26. #if defined FILTERSCRIPT
  27.  
  28. #define MYSQL_HOST "hostname"
  29. #define MYSQL_USER "username"
  30. #define MYSQL_DATABASE "database"
  31. #define MYSQL_PASS "password"
  32.  
  33. new MySQL: Database;
  34.  
  35. public OnFilterScriptInit()
  36. {
  37. new MySQLOpt: option_id = mysql_init_options();
  38. mysql_set_option(option_id, AUTO_RECONNECT, true);
  39. Database = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASS, MYSQL_DATABASE, option_id);
  40. if(Database == MYSQL_INVALID_HANDLE || mysql_errno(Database) != 0)
  41. {
  42. print("The server couldn't connect to the MySQL Database");
  43. SendRconCommand("exit");
  44. return 1;
  45. }
  46. else
  47. print("Connection to MySQL database was successful.");
  48.  
  49. mysql_query(Database, "CREATE TABLE IF NOT EXISTS bans(`BanID` int(10) AUTO_INCREMENT PRIMARY KEY, `Username` VARCHAR(24) NOT NULL, `BannedBy` VARCHAR(24) NOT NULL, `BanReason` VARCHAR(128) NOT NULL, `IpAddress` VARCHAR(17) NOT NULL, `Date` VARCHAR(30) NOT NULL);");
  50.  
  51. print("\n--------------------------------------");
  52. print("Ban / Unban system by willbedie (MySQL)");
  53. print("--------------------------------------\n");
  54. return 1;
  55. }
  56.  
  57. public OnFilterScriptExit()
  58. {
  59. return 1;
  60. }
  61.  
  62. public OnPlayerConnect(playerid)
  63. {
  64. new query[100];
  65. mysql_format(Database, query, sizeof(query), "SELECT * FROM `bans` WHERE `Username` = '%e';", GetName(playerid));
  66. mysql_tquery(Database, query, "CheckPlayer", "d", playerid); // Check if the player is banned
  67. return 1;
  68. }
  69.  
  70. forward CheckPlayer(playerid); // We are going to check the player who is logging in
  71. public CheckPlayer(playerid)
  72. {
  73. if(cache_num_rows() != 0) // If the player is currently banned.
  74. {
  75. new Username[24], BannedBy[24], BanReason[128], Date[20];
  76. cache_get_value_name(0, "Username", Username); // Retreive the username from the mysql database
  77. cache_get_value_name(0, "BannedBy", BannedBy); // Retreive the admin's name from the mysql database
  78. cache_get_value_name(0, "BanReason", BanReason); // Retreive the ban reason from the mysql database
  79. cache_get_value_name(0, "Date", Date);
  80.  
  81. SendClientMessage(playerid, -1, "{D93D3D}You are banned from this server."); // Send a message to the player to tell him he's banned
  82. new string[500];
  83. format(string, sizeof(string), "{FFFFFF}You are banned from this server\n{D93D3D}Username: {FFFFFF}%s\n{D93D3D}Banned by: {FFFFFF}%s\n{D93D3D}Ban Reason: {FFFFFF}%s\n{D93D3D}Date: {FFFFFF}%s", Username, BannedBy, BanReason, Date);
  84. Dialog_Show(playerid, DIALOG_BANNED, DIALOG_STYLE_MSGBOX, "Ban Info", string, "Close", ""); // Show this dialog to the player.
  85. SetTimerEx("SendToKick", 400, false, "i", playerid); // Kick the player in 400 miliseconds.
  86. }
  87. else
  88. {
  89. //Log the player in here
  90. }
  91. return 1;
  92. }
  93.  
  94. public OnPlayerDisconnect(playerid, reason)
  95. {
  96. return 1;
  97. }
  98.  
  99. public OnPlayerSpawn(playerid)
  100. {
  101. return 1;
  102. }
  103.  
  104.  
  105. forward SendToKick(playerid);
  106. public SendToKick(playerid)
  107. {
  108. Kick(playerid);
  109. return 1;
  110. }
  111.  
  112. GetName(playerid)
  113. {
  114. new Name[MAX_PLAYER_NAME];
  115. GetPlayerName(playerid, Name, sizeof(Name));
  116. return Name;
  117. }
  118.  
  119. stock ReturnDate()
  120. {
  121. new sendString[90], MonthStr[40], month, day, year;
  122. new hour, minute, second;
  123.  
  124. gettime(hour, minute, second);
  125. getdate(year, month, day);
  126. switch(month)
  127. {
  128. case 1: MonthStr = "January";
  129. case 2: MonthStr = "February";
  130. case 3: MonthStr = "March";
  131. case 4: MonthStr = "April";
  132. case 5: MonthStr = "May";
  133. case 6: MonthStr = "June";
  134. case 7: MonthStr = "July";
  135. case 8: MonthStr = "August";
  136. case 9: MonthStr = "September";
  137. case 10: MonthStr = "October";
  138. case 11: MonthStr = "November";
  139. case 12: MonthStr = "December";
  140. }
  141.  
  142. format(sendString, 90, "%s %d, %d %02d:%02d:%02d", MonthStr, day, year, hour, minute, second);
  143. return sendString;
  144. }
  145.  
  146. CMD:ban(playerid, params[])
  147. {
  148. if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "SERVER: You are not authorized to use that command."); // If the player is not logged into rcon
  149.  
  150. new PlayerIP[17];
  151. new giveplayerid, reason[128], string[150], query[150];
  152. GetPlayerIp(giveplayerid, PlayerIP, sizeof(PlayerIP)); // We are going to get the target's IP with this.
  153.  
  154. if(sscanf(params, "us[128]", giveplayerid, reason)) return SendClientMessage(playerid, -1, "USAGE: /ban [playerid] [reason]"); // This will show the usage of the command after the player types /ban
  155. if(!IsPlayerConnected(giveplayerid)) return SendClientMessage(playerid, -1, "That player is not connected"); // If the target is not connected.
  156.  
  157. mysql_format(Database, query, sizeof(query), "INSERT INTO `bans` (`Username`, `BannedBy`, `BanReason`, `IpAddress`, `Date`) VALUES ('%e', '%e', '%e', '%e', '%e')", GetName(giveplayerid), GetName(playerid), reason, PlayerIP, ReturnDate());
  158. mysql_tquery(Database, query, "", ""); // This will insert the information into the bans table.
  159.  
  160. format(string, sizeof(string), "SERVER: %s[%d] was banned by %s, Reason: %s", GetName(giveplayerid), giveplayerid, GetName(playerid), reason); // This message will be sent to every player online.
  161. SendClientMessageToAll(-1, string);
  162. SetTimerEx("SendToKick", 500, false, "d", giveplayerid); // Kicks the player in 500 miliseconds.
  163. return 1;
  164. }
  165.  
  166. CMD:unban(playerid, params[])
  167. {
  168. if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "SERVER: You are not authorized to use that command.");
  169.  
  170. new name[MAX_PLAYER_NAME], query[150], string[150], rows;
  171. if(sscanf(params, "s[128]", name)) return SendClientMessage(playerid, -1, "USAGE: /unban [name]"); // This will show the usage of the command if the player types only /unban.
  172. mysql_format(Database, query, sizeof(query), "SELECT * FROM `bans` WHERE `Username` = '%e' LIMIT 0, 1", name);
  173. new Cache:result = mysql_query(Database, query);
  174. cache_get_row_count(rows);
  175.  
  176. if(!rows)
  177. {
  178. SendClientMessage(playerid, -1, "SERVER: That name does not exist or there is no ban under that name.");
  179. }
  180.  
  181. for (new i = 0; i < rows; i ++)
  182. {
  183. mysql_format(Database, query, sizeof(query), "DELETE FROM `bans` WHERE Username = '%e'", name);
  184. mysql_tquery(Database, query);
  185. for(new x; x < MAX_PLAYERS; x++)
  186. {
  187. if(IsPlayerAdmin(x))
  188. {
  189. format(string, sizeof(string), "AdminWarn: %s(%d) has unbanned %s", GetName(playerid), name);
  190. SendClientMessage(x, -1, string);
  191. }
  192. }
  193. }
  194. cache_delete(result);
  195. return 1;
  196. }
  197. CMD:oban(playerid, params[])
  198. {
  199. if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "SERVER: You are not authorized to use that command.");
  200. new name[MAX_PLAYER_NAME], reason[128], query[300], string[100], rows;
  201. if(sscanf(params, "s[24]s[128]", name, reason)) return SendClientMessage(playerid, -1, "USAGE: /oban [username] [reason]");
  202. mysql_format(Database, query, sizeof(query), "SELECT `Username` FROM `users` WHERE `Username` = '%e' LIMIT 0,1", name);
  203. new Cache:result = mysql_query(Database, query);
  204. cache_get_row_count(rows);
  205.  
  206. if(!rows)
  207. {
  208. SendClientMessage(playerid, -1, "SERVER: That name does not exist.");
  209. }
  210.  
  211. for (new i = 0; i < rows; i ++)
  212. {
  213. mysql_format(Database, query, sizeof(query), "INSERT INTO `bans` (`Username`, `BannedBy`, `BanReason`, `Date`) VALUES ('%e', '%e', '%e', '%e')", name, GetName(playerid), reason, ReturnDate());
  214. mysql_tquery(Database, query);
  215. format(string, sizeof(string), "AdmCmd: {FF0000}%s has been offline-banned by %s, Reason: %s", name, GetName(playerid), reason);
  216. SendClientMessageToAll(-1, string);
  217. }
  218. cache_delete(result);
  219. return 1;
  220. }
  221. CMD:baninfo(playerid, params[])
  222. {
  223. if(!IsPlayerAdmin(playerid)) return SendClientMessage(playerid, -1, "SERVER: You are not authorized to use that command.");
  224. new name[MAX_PLAYER_NAME], query[300], rows;
  225. if(sscanf(params, "s[24]", name)) return SendClientMessage(playerid, -1, "USAGE: /baninfo [username]");
  226. mysql_format(Database, query, sizeof(query), "SELECT * FROM `bans` where `Username` = '%e' LIMIT 0, 1", name);
  227. new Cache:result = mysql_query(Database, query);
  228. cache_get_row_count(rows);
  229.  
  230. if(!rows)
  231. {
  232. SendClientMessage(playerid, -1, "SERVER: That name does not exist or there is no ban under that name.");
  233. }
  234.  
  235. for (new i = 0; i < rows; i ++)
  236. {
  237. new Username[24], BannedBy[24], BanReason[24], BanID, Date[30];
  238. cache_get_value_name(0, "Username", Username);
  239. cache_get_value_name(0, "BannedBy", BannedBy);
  240. cache_get_value_name(0, "BanReason", BanReason);
  241. cache_get_value_name_int(0, "BanID", BanID);
  242. cache_get_value_name(0, "Date", Date);
  243.  
  244. new string[500];
  245. format(string, sizeof(string), "{FFFFFF}Checking ban information on user: {9D00AB}%s\n\n{FFFFFF}Username: {9D00AB}%s\n{FFFFFF}Banned By: {9D00AB}%s\n{FFFFFF}Ban Reason: {9D00AB}%s\n{FFFFFF}Ban ID: {9D00AB}%i\n{FFFFFF}Date: {9D00AB}%s\n\n{FFFFFF}Type /unban [name] if you want to unban this user.", name, Username, BannedBy, BanReason, BanID, Date);
  246. Dialog_Show(playerid, DIALOG_BANCHECK, DIALOG_STYLE_MSGBOX, "{FFFFFF}Ban Information", string, "Close", "");
  247. }
  248. cache_delete(result);
  249. return 1;
  250. }
  251. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement