botalan

discord bot
git clone git://git.benoneill.xyz/botalan
Log | Files | Refs | README | LICENSE

botalan.py (1311B)


      1 import discord
      2 import os
      3 import cmd
      4 
      5 TOKEN = os.getenv("TOKEN")
      6 
      7 client = discord.Client()
      8 
      9 def exec_cmd(msg):
     10     response = ''
     11     msg = msg.strip()
     12     if msg == '!tweet':
     13         response = cmd.tweet()
     14 
     15     if msg.startswith('!fortune'):
     16         if len(msg.split()) > 1:
     17             arg = msg.split()[1]
     18             print(arg)
     19             print(len(arg))
     20             response = cmd.fortune(arg)
     21         response = cmd.fortune()
     22 
     23     if msg.startswith('!cowsay ') and len(msg) > len('!cowsay '):
     24         index = msg.index(' ') + 1
     25         arg = None
     26         if msg[index] == '-':
     27             arg = msg[index + 1:].split()[0]
     28             cowmsg = msg[index + len(arg) + 1:]
     29         else:
     30             cowmsg = msg[index:]
     31         response = cmd.cowsay(arg, cowmsg)
     32 
     33     if msg == ('!cowfortune'):
     34         response = cmd.cowfortune()
     35 
     36     return response
     37 
     38 @client.event
     39 async def on_ready():
     40     print(f'{client.user} has connected to Discord!')
     41 
     42 @client.event
     43 async def on_message(message):
     44     if message.author == client.user:
     45         return
     46     response = exec_cmd(message.content)
     47     await message.channel.send(response)
     48 
     49 if TOKEN == None:
     50     print("Running in test mode")
     51     while True:
     52         msg = input("user> ")
     53         response = exec_cmd(msg)
     54         print(response)
     55 else:
     56     client.run(TOKEN)