diff --git a/arguments.py b/arguments.py index af63847..7d19968 100644 --- a/arguments.py +++ b/arguments.py @@ -38,9 +38,9 @@ def range_type(string, min=0, max=100): try: value = int(string) except ValueError: - raise argparse.ArgumentTypeError(f"value not a valid integer") + raise argparse.ArgumentTypeError("value is not a valid integer") if min <= value <= max: return value else: - raise argparse.ArgumentTypeError(f"value not in range {min}-{max}") + raise argparse.ArgumentTypeError(f"value is not in range {min}-{max}") diff --git a/commands/__init__.py b/commands/__init__.py index f433740..429d97f 100644 --- a/commands/__init__.py +++ b/commands/__init__.py @@ -1,5 +1,16 @@ from . import bot, tools, utils, voice -from .utils import * +from .utils import Command, match, match_token, tokenize + +__all__ = [ + "bot", + "tools", + "utils", + "voice", + "Command", + "match", + "match_token", + "tokenize", +] def __reload_module__(): diff --git a/commands/tools.py b/commands/tools.py index 4bab6f2..0a983f3 100644 --- a/commands/tools.py +++ b/commands/tools.py @@ -1,7 +1,6 @@ import re import arguments - import commands import utils @@ -67,7 +66,7 @@ async def clear(message): if args.delete_command: try: await message.delete() - except: + except Exception: pass regex = None @@ -101,5 +100,5 @@ async def clear(message): message, f"purged **{messages}/{args.count} {'message' if args.count == 1 else 'messages'}**", ) - except: + except Exception: pass diff --git a/commands/voice.py b/commands/voice.py index 19110b0..3fdc28b 100644 --- a/commands/voice.py +++ b/commands/voice.py @@ -299,9 +299,6 @@ async def volume(message): if not command_allowed(message): return - if not message.guild.voice_client: - return - tokens = commands.tokenize(message.content) parser = arguments.ArgumentParser(tokens[0], "set the current volume level") parser.add_argument( @@ -314,10 +311,7 @@ async def volume(message): return if not message.guild.voice_client.source: - await utils.reply( - message, - f"nothing is playing!", - ) + await utils.reply(message, "nothing is playing!") return if args.volume is None: diff --git a/constants.py b/constants.py index da828a4..9151c29 100644 --- a/constants.py +++ b/constants.py @@ -13,6 +13,7 @@ RELOADABLE_MODULES = [ "constants", "core", "events", + "tasks", "utils", "voice", "youtubedl", diff --git a/core.py b/core.py index 0831315..23b785a 100644 --- a/core.py +++ b/core.py @@ -7,13 +7,13 @@ import textwrap import time import traceback +import disnake import disnake_paginator import commands import constants -import core import utils -from state import client, command_locks, last_used +from state import client, command_locks, idle_tracker async def on_message(message, edited=False): @@ -27,8 +27,10 @@ async def on_message(message, edited=False): if not matched: return - global last_used - last_used = time.time() + idle_tracker["last_used"] = time.time() + if idle_tracker["is_idle"]: + idle_tracker["is_idle"] = False + await client.change_presence(status=disnake.Status.online) if len(matched) > 1: await utils.reply( @@ -85,7 +87,7 @@ async def on_message(message, edited=False): if len(output) > 2000: output = output.replace("`", "\\`") await disnake_paginator.ButtonPaginator( - prefix=f"```\n", + prefix="```\n", suffix="```", invalid_user_function=utils.invalid_user_handler, color=constants.EMBED_COLOR, @@ -129,9 +131,9 @@ async def on_message(message, edited=False): async def on_voice_state_update(_, before, after): - is_empty = lambda channel: [m.id for m in (channel.members if channel else [])] == [ - client.user.id - ] + def is_empty(channel): + return [m.id for m in (channel.members if channel else [])] == [client.user.id] + c = None if is_empty(before.channel): c = before.channel diff --git a/events.py b/events.py index 7aa87d3..de762ee 100644 --- a/events.py +++ b/events.py @@ -17,15 +17,6 @@ async def on_ready(): ), ).start() - threading.Thread( - name="check_idle", - target=asyncio.run_coroutine_threadsafe, - args=( - tasks.check_idle(), - client.loop, - ), - ).start() - async def on_message(message): await core.on_message(message) diff --git a/main.py b/main.py index 76ca42b..a7059ff 100644 --- a/main.py +++ b/main.py @@ -12,4 +12,5 @@ async def on_ready(): await events.on_ready() -client.run(constants.SECRETS["TOKEN"]) +if __name__ == "__main__": + client.run(constants.SECRETS["TOKEN"]) diff --git a/state.py b/state.py index f840584..3f2d4f2 100644 --- a/state.py +++ b/state.py @@ -20,14 +20,13 @@ class LimitedSizeDict(collections.OrderedDict): self.popitem(last=False) -command_locks = LimitedSizeDict() -message_responses = LimitedSizeDict() -players = {} - intents = disnake.Intents.default() intents.message_content = True intents.members = True client = disnake.Client(intents=intents) -last_used = time.time() +command_locks = LimitedSizeDict() +idle_tracker = {"is_idle": False, "last_used": time.time()} +message_responses = LimitedSizeDict() +players = {} start_time = time.time() diff --git a/tasks.py b/tasks.py index e3795aa..75f5991 100644 --- a/tasks.py +++ b/tasks.py @@ -3,7 +3,7 @@ import time import disnake -from state import client, last_used, players +from state import client, idle_tracker, players async def cleanup(): @@ -17,12 +17,9 @@ async def cleanup(): for target in targets: del players[target] - -async def check_idle(): - while True: - await asyncio.sleep(3600) - - if time.time() - last_used >= 3600: + if ( + not idle_tracker["is_idle"] + and time.time() - idle_tracker["last_used"] >= 3600 + ): await client.change_presence(status=disnake.Status.idle) - else: - await client.change_presence(status=disnake.Status.online) + idle_tracker["is_idle"] = True diff --git a/utils.py b/utils.py index 8b109b3..e644cb9 100644 --- a/utils.py +++ b/utils.py @@ -5,7 +5,9 @@ from state import message_responses def format_duration(duration: int): - format_plural = lambda noun, count: noun if count == 1 else noun + "s" + def format_plural(noun, count): + return noun if count == 1 else noun + "s" + segments = [] weeks, duration = divmod(duration, 604800)