Separate files
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
from .bot import Bot
|
from .bot import Bot
|
||||||
|
from .discord_utils import delete_message, message_id
|
||||||
|
|||||||
+2
-37
@@ -1,4 +1,4 @@
|
|||||||
from typing import List, Callable, Coroutine, Tuple, Any
|
from typing import Callable, Coroutine, Tuple, Any
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
import traceback
|
import traceback
|
||||||
@@ -10,6 +10,7 @@ import os
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
from .utils import sanitize_input, parse_arguments
|
||||||
|
|
||||||
CommandFunction = Callable[[discord.Client, discord.Message, Tuple[str]], Coroutine[Any, Any, None]]
|
CommandFunction = Callable[[discord.Client, discord.Message, Tuple[str]], Coroutine[Any, Any, None]]
|
||||||
|
|
||||||
@@ -18,42 +19,6 @@ def debug(message: discord.Message, txt: str):
|
|||||||
logging.info(f"{message.guild} > #{message.channel}: {txt}")
|
logging.info(f"{message.guild} > #{message.channel}: {txt}")
|
||||||
|
|
||||||
|
|
||||||
async def delete_message(message: discord.Message) -> bool:
|
|
||||||
try:
|
|
||||||
await message.delete()
|
|
||||||
return True
|
|
||||||
except discord.Forbidden:
|
|
||||||
pass
|
|
||||||
except discord.NotFound:
|
|
||||||
pass
|
|
||||||
return False
|
|
||||||
|
|
||||||
|
|
||||||
def message_id(message: discord.Message) -> str:
|
|
||||||
is_direct = message.channel.type == discord.ChannelType.private
|
|
||||||
if not is_direct:
|
|
||||||
return f'{message.guild.id}/{message.channel.id}/{message.author.id}'
|
|
||||||
else:
|
|
||||||
return message.author.id
|
|
||||||
|
|
||||||
|
|
||||||
def sanitize_input(src: str) -> str:
|
|
||||||
return re.sub(r'[^A-Za-z0-9 _]', "", src.lower().strip())
|
|
||||||
|
|
||||||
|
|
||||||
args_regex = re.compile('"([^"]*)"|\'([^\']*)\'|([^ ]+)')
|
|
||||||
|
|
||||||
|
|
||||||
def parse_arguments(src: str) -> List[str]:
|
|
||||||
def get_found_match(m: list) -> str:
|
|
||||||
f = [g for g in m if len(g) > 0]
|
|
||||||
if len(f) > 0:
|
|
||||||
return f[0]
|
|
||||||
return ""
|
|
||||||
|
|
||||||
return [get_found_match(m) for m in args_regex.findall(src)]
|
|
||||||
|
|
||||||
|
|
||||||
class Command(object):
|
class Command(object):
|
||||||
def __init__(self, regex: str, compute: CommandFunction, help_short: str, help_long: str):
|
def __init__(self, regex: str, compute: CommandFunction, help_short: str, help_long: str):
|
||||||
self.regex = regex
|
self.regex = regex
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import discord
|
||||||
|
|
||||||
|
|
||||||
|
async def delete_message(message: discord.Message) -> bool:
|
||||||
|
try:
|
||||||
|
await message.delete()
|
||||||
|
return True
|
||||||
|
except discord.Forbidden:
|
||||||
|
pass
|
||||||
|
except discord.NotFound:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def message_id(message: discord.Message) -> str:
|
||||||
|
is_direct = message.channel.type == discord.ChannelType.private
|
||||||
|
if not is_direct:
|
||||||
|
return f'{message.guild.id}/{message.channel.id}/{message.author.id}'
|
||||||
|
else:
|
||||||
|
return message.author.id
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
from typing import List
|
||||||
|
import re
|
||||||
|
|
||||||
|
args_regex = re.compile('"([^"]*)"|\'([^\']*)\'|([^ ]+)')
|
||||||
|
|
||||||
|
|
||||||
|
def sanitize_input(src: str) -> str:
|
||||||
|
return re.sub(r'[^A-Za-z0-9 _]', "", src.lower().strip())
|
||||||
|
|
||||||
|
|
||||||
|
def parse_arguments(src: str) -> List[str]:
|
||||||
|
def get_found_match(m: list) -> str:
|
||||||
|
f = [g for g in m if len(g) > 0]
|
||||||
|
if len(f) > 0:
|
||||||
|
return f[0]
|
||||||
|
return ""
|
||||||
|
|
||||||
|
return [get_found_match(m) for m in args_regex.findall(src)]
|
||||||
Reference in New Issue
Block a user