1
0

bot.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import discord
  2. from discord.ext import commands
  3. import asyncio
  4. import importlib
  5. #external files belonging to this project
  6. from output import *
  7. description = '''I'm the Sarpian bot designed for San Andreas Roleplay. Treat me like your assistant.'''
  8. bot = commands.Bot(command_prefix='!', description=description)
  9. channel = discord.Object(id='420986449932976129')
  10. serverobj = None
  11. adminRoles = ["199936764935143425", "199937309737484289", "199937425672241153", "365936281298927628", "397886565713444875"]
  12. """
  13. EVENTS
  14. """
  15. @bot.event
  16. async def on_ready():
  17. #await bot.send_message(channel, "Hi there! I'm the Sarpian bot designed for San Andreas Roleplay.")
  18. #await bot.send_message(channel, "I will be your assistant. So far though, all I can do is forward you messages from the SA:MP server.")
  19. print("------Connected...")
  20. loop = asyncio.get_event_loop()
  21. coro = asyncio.start_server(readwrite, "0.0.0.0", 5000, loop = loop)
  22. server = asyncio.run_coroutine_threadsafe(coro, loop)
  23. print('Socket is up')
  24. class AServer:
  25. def __init__(self, reader: asyncio.StreamReader, writer: asyncio.StreamWriter, bot, loop=None):
  26. self.loop = loop or asyncio.get_event_loop()
  27. self.reader = reader
  28. self.writer = writer
  29. self.alive = True
  30. #self.hostname = socket.get_hostname()
  31. self.__ready_condition = asyncio.Condition()
  32. self.ready = False
  33. self.bot = bot
  34. asyncio.run_coroutine_threadsafe(self.GetData(), self.loop)
  35. async def GetData(self):
  36. while True:
  37. #await this function till there is data available
  38. data = await self.reader.read(300)
  39. message = data.decode('ascii')
  40. print(message)
  41. embed = GetEmbededMessage(message)
  42. print(embed)
  43. if not embed:
  44. print("There was a blank message!")
  45. global bot
  46. global channel
  47. await bot.send_message(channel, embed=embed)
  48. async def SendData(self, data):
  49. #if sock not ready, AWAIT until it is
  50. """ if not self.ready:
  51. print('waiting..')
  52. await self.wait_for_ready()
  53. """
  54. #send some data
  55. self.writer.write(data.encode())
  56. print("SENDING: " + data)
  57. await self.writer.drain()
  58. async def readwrite(reader, writer):
  59. global serverobj
  60. global bot
  61. global channel
  62. serverobj = AServer(reader, writer, bot)
  63. print(serverobj)
  64. await bot.send_message(channel, "I have connected to the server.")
  65. def is_admin(user):
  66. for urole in user.roles:
  67. for role in adminRoles:
  68. if urole.id == role:
  69. return True
  70. return False
  71. @bot.command(pass_context=True)
  72. async def hi(ctx):
  73. if not is_admin(ctx.message.author):
  74. await bot.say("You're not an administrator.")
  75. return
  76. if serverobj:
  77. await bot.say("The server is connected.")
  78. else:
  79. await bot.say("Uhoh...the server is not connected.")
  80. @bot.command()
  81. async def admins():
  82. if serverobj:
  83. await serverobj.SendData("!ADMINS!")
  84. @bot.command()
  85. async def servertime():
  86. if serverobj:
  87. await serverobj.SendData("!SERVERTIME!")
  88. @bot.command(pass_context=True)
  89. async def gmx(ctx):
  90. if not is_admin(ctx.message.author):
  91. await bot.say("You're not an administrator.")
  92. return
  93. if serverobj:
  94. await serverobj.SendData("!GMX!")
  95. @bot.command(pass_context = True)
  96. async def a(ctx, *, msg):
  97. if not is_admin(ctx.message.author):
  98. await bot.say("You're not an administrator.")
  99. return
  100. if serverobj:
  101. nick = ctx.message.author
  102. await serverobj.SendData("!AMSG!&" + str(nick) + "&" + msg)
  103. """
  104. COMMANDS
  105. """
  106. """
  107. IMPORTANT STUFF...
  108. """
  109. bot.run('MzE2ODY1MDYyMTU5NjQ2NzIy.DYF_ig.cQRn1lz_HqqqNUUR72mH_Kkc9Bw')
  110. try:
  111. loop.run_forever()
  112. except KeyboardInterrupt:
  113. pass
  114. # Close the server
  115. loop.run_until_complete(server.wait_closed())
  116. loop.close()
  117. bot.close()