title: Control custrom script through discord.py bot
body: <p>As title says, I have a script, that is doing (some super secret) task, which is running almost non-stop.
  What I do now, is to check it every now and then, to see if its running smoothly.</p>
  <p>I have switched from python2.7 to python 3.7 recently and with that, I started to play with discord.py bot little bit. Now, what I want to do, is to make my script, that is running almost non-stop to:</p>
  <ol>
  <li>Print its log into discord server, so I can check if its running smoothly without the need to get to PC</li>
  <li>Be able to control the script through discord commands</li>
  </ol>
  <p>So, basically, there are 2 things I want to achieve:</p>
  <p>1.) When the script prints some log into console, send it also to discord.py bot which will send it to given channel</p>
  <p>2.) Make discord.py bot to read given channel (different than previous one) and if the correct command comes, take action in the script (do something, or just change some variable value)</p>
  <p>Here is my code example I have so far:</p>
  <pre><code># Discord bot, to control bot over discord
  try:
      import discord
      import disc_bot_con
      from discord.ext import commands
  except Exception as x:
      print(&quot;Unable to import discord libraries [%s]&quot; % x)
  
  
  # Global methods *******************************************************************************************************
  def print_log(msg, lvl=None):
      if lvl == 'i':
          print(&quot;[%s] INFO:       %s&quot; % (datetime.datetime.now(), msg))
      elif lvl == 'w':
          print(&quot;[%s] WARNING:    %s&quot; % (datetime.datetime.now(), msg))
      elif lvl == 'e':
          print(&quot;[%s] ERRROR:     %s&quot; % (datetime.datetime.now(), msg))
      elif lvl == 'd':
          if DEBUG:
              print(&quot;[%s] DEBUG:      %s&quot; % (datetime.datetime.now(), msg))
      else:
          print(&quot;[%s] %s&quot; % (datetime.datetime.now(), msg))
  
  
  # Classes **************************************************************************************************************
  class Script:
      def __init__(self):
          # do soemthing
  
      def run(self):
          # repeatedly doing some stuff
          while True:
              if something:
                  print_log('Soemthing')
              else:
                  print_log('Taking action')
                  self.take_action()
  
  
  class ScriptDiscordBot(discord.Client):
      async def setup_guild(self):
          await self.login(disc_bot_con.TOKEN)
          self.guild = discord.utils.get(self.guilds, name=disc_bot_con.NAME_CZ_SK_RIVALS)
          self.channel = discord.utils.get(self.guild.channels, name='bot_trash')
  
      async def my_background_task(self):
          await self.wait_until_ready()
          counter = 0
          channel = discord.Object(id='channel_id_here')
          while not self.is_closed:
              counter += 1
              await self.send_message(channel, counter)
              await asyncio.sleep(60)  # task runs every 60 seconds
  
      async def on_connect(self):
          print_log(&quot;Connecting to discord!&quot;)
  
      async def on_ready(self):
          print('Logged on as {0}!'.format(self.user))
          guild = discord.utils.get(self.guilds, name=disc_bot_con.NAME_CZ_SK_RIVALS)
          print('Logged to: {0}!'.format(self.guild))
  
      async def send_log(self, message):
          #channel = discord.utils.get(self.guild.channels, name=chanel_name)
          channel_id = self.channel.id
          await self.send_message(channel_id, message)
  
  
  # run the thing
  if __name__ == '__main__':
  
      if not TEST_MODE:
          try:
              opts, extraparams = getopt.getopt(sys.argv[1:], 'ASFWsc:t:w:h', ['help'])
          except getopt.GetoptError as err:
              print(str(err))
              Script.print_help()
              sys.exit(2)
  
          discord_bot = ScriptDiscordBot()
          discord_bot.run(disc_bot_con.TOKEN)
  
          the_bot = Script()
          the_bot.run()
  
  </code></pre>
  <p>When I run it like this, it starts bot and running it, but does not start the Script class, obviously.</p>
  <p>At first, I have been trying to encapsulate <code>ScriptDiscordBot</code> into separate thread..but it did not worked.</p>
  <pre><code># run the thing
  if __name__ == '__main__':
  
      if not TEST_MODE:
          try:
              opts, extraparams = getopt.getopt(sys.argv[1:], 'ASFWsc:t:w:h', ['help'])
          except getopt.GetoptError as err:
              print(str(err))
              Script.print_help()
              sys.exit(2)
  
          discord_bot = ScriptDiscordBot()
          t = threading.Thread(target=discord_bot.run, args=disc_bot_con.TOKEN)
          t.start()
  
          the_bot = Script()
          the_bot.run()
  </code></pre>
  <p>Than I tried to make discord bot to run in abckground, as github exampel says, but also no luck there.</p>
  <pre><code># run the thing
  if __name__ == '__main__':
  
      if not TEST_MODE:
          try:
              opts, extraparams = getopt.getopt(sys.argv[1:], 'ASFWsc:t:w:h', ['help'])
          except getopt.GetoptError as err:
              print(str(err))
              Script.print_help()
              sys.exit(2)
  
          discord_bot = ScriptDiscordBot()
          discord_bot.loop.create_task(my_background_task())
          discord_bot.login(disc_bot_con.TOKEN)
  
          the_bot = Script()
          the_bot.run()
  </code></pre>
  <p>The last thing I have tried was, to not make run discord bot at all, but call its methods from <code>Script</code> instead.</p>
  <pre><code># run the thing
  if __name__ == '__main__':
  
      if not TEST_MODE:
          try:
              opts, extraparams = getopt.getopt(sys.argv[1:], 'ASFWsc:t:w:h', ['help'])
          except getopt.GetoptError as err:
              print(str(err))
              Script.print_help()
              sys.exit(2)
  
          discord_bot = ScriptDiscordBot()
          discord_bot.setup_guild()
          discord_bot.send_log('Test')
  
          the_bot = Script()
          the_bot.run()
  </code></pre>
  <p>Also no luck there. :(</p>
  <p>I am familiar with <code>Threading</code> and <code>Multiprocessing</code> from python2.7 but <code>asyncio</code> is completely new for me. I have been trying different approaches, to make it work. I have coded many applications where multiple processes are running at once, but this seems like too much for me to handle. I think its very simple task, but the solution seems impossible for me.</p>
  <p>Also, I am pretty sure I am thinking wrong way about it, but I have no idea how should I think here. So Id use little bit of correction in the way I am thinking about it, and also some code examples. The examples in dicsord.py documentation and github are super simple, I have them working in separate script, but I am somehow unable to make those two scripts cooperate, or run at the same time.</p>