Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bottle import route, run # server to receive data
- import discord
- import asyncio
- from queue import Queue
- import threading
- import re
- from discord.ext import commands
- q = Queue()
- @route('<mypath:path>')
- def server_callback(mypath):
- q.put(mypath)
- return "OK"
- def server_thread():
- run(host='localhost', port=7891)
- def FilterMessage(msg): # function that intakes received request/message and returns what will be posted on discord
- msg = msg[1:] # get rid of "/"
- if msg.startswith("[CMSG]"):
- return msg
- 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.")
- return "@everyone prepare your toys! (Your group is in a war.)"
- elif msg.startswith("[CHAT]"):
- pass # do nothing with chat messages
- return
- client = discord.Client()
- async def my_background_task():
- await client.wait_until_ready()
- channel = discord.Object(id='480410810397622274')
- while not client.is_closed:
- try:
- msg = FilterMessage(q.get(False))
- except:
- pass
- else:
- if msg:
- await client.send_message(channel, msg)
- finally:
- await asyncio.sleep(0.1)
- @client.event
- async def on_ready():
- print('Logged in as')
- print(client.user.name)
- print(client.user.id)
- print('------')
- @client.event
- async def on_message(message):
- import random
- # we do not want the bot to reply to itself
- if message.author == client.user:
- return
- if message.content.startswith('$guess'):
- await client.send_message(message.channel, 'Guess a number between 1 to 10')
- def guess_check(m):
- return m.content.isdigit()
- guess = await client.wait_for_message(timeout=5.0, author=message.author, check=guess_check)
- answer = random.randint(1, 10)
- if guess is None:
- fmt = 'Sorry, you took too long. It was {}.'
- await client.send_message(message.channel, fmt.format(answer))
- return
- if int(guess.content) == answer:
- await client.send_message(message.channel, 'You are right!')
- else:
- await client.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer))
- if __name__ == "__main__":
- server_thread = threading.Thread(target=server_thread)
- server_thread.daemon = True
- server_thread.start()
- client.loop.create_task(my_background_task())
- client.run('NDgw')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement