Advertisement
michalmonday

discord_samp block workaround

Aug 22nd, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.72 KB | None | 0 0
  1.  
  2. from bottle import route, run # server to receive data
  3.  
  4.  
  5. import discord
  6. import asyncio
  7.  
  8. from queue import Queue
  9. import threading
  10. import re
  11.  
  12. from discord.ext import commands
  13.  
  14.  
  15.  
  16.  
  17.  
  18. q = Queue()
  19.  
  20.  
  21. @route('<mypath:path>')
  22. def server_callback(mypath):
  23.     q.put(mypath)
  24.     return "OK"
  25.  
  26. def server_thread():
  27.     run(host='localhost', port=7891)
  28.  
  29.  
  30.  
  31.  
  32. def FilterMessage(msg): # function that intakes received request/message and returns what will be posted on discord
  33.     msg = msg[1:] # get rid of "/"
  34.     if msg.startswith("[CMSG]"):
  35.         return msg
  36.         if "Your group is in a war." in msg[6:]: # if that phrase is within message (safe in cases where space is added after/before), there could be check if the message is equal to that phrase though (using if msg[6:] == "Your group is in a war.")
  37.             return "@everyone prepare your toys! (Your group is in a war.)"
  38.     elif msg.startswith("[CHAT]"):
  39.         pass # do nothing with chat messages
  40.  
  41.     return        
  42.  
  43.  
  44. client = discord.Client()
  45.  
  46.  
  47. async def my_background_task():
  48.     await client.wait_until_ready()
  49.     channel = discord.Object(id='480410810397622274')
  50.     while not client.is_closed:
  51.         try:
  52.             msg = FilterMessage(q.get(False))
  53.         except:
  54.             pass
  55.         else:
  56.             if msg:
  57.                 await client.send_message(channel, msg)
  58.         finally:
  59.             await asyncio.sleep(0.1)
  60.        
  61.  
  62.  
  63. @client.event
  64. async def on_ready():
  65.     print('Logged in as')
  66.     print(client.user.name)
  67.     print(client.user.id)
  68.     print('------')
  69.  
  70.  
  71. @client.event
  72. async def on_message(message):
  73.     import random
  74.     # we do not want the bot to reply to itself
  75.     if message.author == client.user:
  76.         return
  77.  
  78.     if message.content.startswith('$guess'):
  79.         await client.send_message(message.channel, 'Guess a number between 1 to 10')
  80.  
  81.         def guess_check(m):
  82.             return m.content.isdigit()
  83.  
  84.         guess = await client.wait_for_message(timeout=5.0, author=message.author, check=guess_check)
  85.         answer = random.randint(1, 10)
  86.         if guess is None:
  87.             fmt = 'Sorry, you took too long. It was {}.'
  88.             await client.send_message(message.channel, fmt.format(answer))
  89.             return
  90.         if int(guess.content) == answer:
  91.             await client.send_message(message.channel, 'You are right!')
  92.         else:
  93.             await client.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer))
  94.  
  95.  
  96. if __name__ == "__main__":
  97.     server_thread = threading.Thread(target=server_thread)
  98.     server_thread.daemon = True
  99.     server_thread.start()
  100.  
  101.     client.loop.create_task(my_background_task())
  102.     client.run('NDgw')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement