better command routing

This commit is contained in:
klemek
2019-11-14 20:40:19 +01:00
parent 067fef344b
commit 98481e7b1d
3 changed files with 29 additions and 10 deletions
+28 -9
View File
@@ -8,7 +8,7 @@ import emotes
import help
from utils import debug
VERSION = "1.0"
VERSION = "1.1-dev"
t0 = datetime.now()
# Loading token
@@ -18,6 +18,25 @@ token = os.getenv('DISCORD_TOKEN')
client = discord.Client()
async def info(message, args):
"""
Computes the %info command
:param message: message sent
:type message: :class:`discord.Message`
:param args: arguments of the command
:type args: list[:class:`str`]
"""
await message.channel.send(f"```Discord Analyst v{VERSION} started at {t0:%Y-%m-%d %H:%M}```")
COMMANDS = {
"%help": help.compute,
"%emotes": emotes.compute,
"%info": info
}
@client.event
async def on_ready():
"""
@@ -47,6 +66,13 @@ async def on_message(message):
if message.author == client.user:
return
args = message.content.split(" ")
if len(args) < 1 or args[0] not in COMMANDS:
return
debug(message, f"command '{message.content}'")
# Check if bot can respond on current channel or DM user
permissions = message.channel.permissions_for(message.guild.me)
if not permissions.send_messages:
@@ -58,14 +84,7 @@ async def on_message(message):
return
# Redirect to the correct command
args = message.content.split(" ")
if args[0] == "%info":
debug(message, f"command '{message.content}'")
await message.channel.send(f"Discord Analyst v{VERSION} started at {t0.isoformat()}")
if args[0] == "%help":
await help.compute(message, args)
if args[0] == "%emotes":
await emotes.compute(message, args)
await COMMANDS[args[0]](message, args)
# Launch client
-1
View File
@@ -10,7 +10,6 @@ async def compute(message, args):
:param args: arguments of the command
:type args: list[str]
"""
debug(message, f"command '{message.content}'")
# Select correct response to send
+1
View File
@@ -1,5 +1,6 @@
# DISCORD API
def debug(message, txt):
"""
Print a log with the context of the current event