bot.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/python
  2. import time
  3. import discord
  4. import os
  5. from mcstatus import MinecraftServer
  6. from discord.ext import commands
  7. server = MinecraftServer.lookup("YOURSERVER.COM")
  8. try:
  9. status = server.status()
  10. print("The server has {0} players and replied in {1} ms".format(status.players.online, status.latency))
  11. except:
  12. print("Server offline")
  13. client = commands.Bot(command_prefix = ".")
  14. @client.event
  15. async def on_ready():
  16. print("Ready")
  17. @client.command()
  18. async def server_start(ctx):
  19. print("Starting server")
  20. try:
  21. status = server.status()
  22. print("```css\nThe server is already running")
  23. await ctx.send("```css\nThe server is already running\n```")
  24. except:
  25. await ctx.send('Starting minecraft server. Please wait a few seconds...')
  26. os.system("cd /home/$USER/ && nohup java -Xmx2048M -Xms2048M -jar server.jar nogui &")
  27. time.sleep(7)
  28. await ctx.send("```css\nServer started\nAddress: YOURSERVER.COM```")
  29. @client.command()
  30. async def server_status(ctx):
  31. print("Server status requested.")
  32. try:
  33. status = server.status()
  34. print("The server has {0} players and replied in {1} ms".format(status.players.online, status.latency))
  35. await ctx.send("**Server status**\n```diff\n+ Players: {0} \n+ Latency: {1} ms```".format(status.players.online, status.latency))
  36. except:
  37. await ctx.send("**Server status**\n```diff\n- Server is offline\n```")
  38. @client.command()
  39. async def server_stop(ctx):
  40. print("Server shutdown requested.")
  41. try:
  42. status = server.status()
  43. if status.players.online !=0:
  44. await ctx.send("**Server shutdown requested:**\n```diff\n- Sorry. server can only be shutdown if no one is playing.\nPlayers: {0}```".format(status.players.online))
  45. else:
  46. await ctx.send("**Server shutdown requested:**\n```diff\n+ Server sucessfully shut down.```")
  47. os.system("killall java")
  48. except:
  49. await ctx.send('```diff\n- Server is not running\n```')
  50. status = server.status()
  51. @client.command()
  52. async def server_help(ctx):
  53. print("Server shutdown requested.")
  54. await ctx.send("```css\nMinecraft server controller\nBy AyyZee\nWritten in python\n```\n```arm\nCOMMAND : WHAT HAPPENS\n```\n```arm\n.server_help : Show this message.\n\n.server_start : Starts the minecraft server, playable after 5 to 10 seconds.\n\n.server_status : Shows player numbers & latency or whether the server is offline.\n\n.server_stop : Stops the server only when no one is playing.\n```\n```fix\nServer address = YOURSERVER.COM```")
  55. client.run('YOUR DISCORD API KEY')