Add artificial blocking to jimmy because funny
All checks were successful
Build and Publish Jimmy.2 / build_and_publish (push) Successful in 6s

This commit is contained in:
Nexus 2024-05-01 01:18:06 +01:00
parent 5ab832d51f
commit a64bd4fa12
Signed by: nex
GPG key ID: 0FA334385D0B689F
2 changed files with 36 additions and 1 deletions

View file

@ -4,7 +4,11 @@
token = "token" # the bot token
debug_guilds = [994710566612500550] # server IDs to create slash commands in. Omit for all guilds.
disabled_commands = ["i do not exist"] # A list of full command names to disable (e.g. "<group name> <subcommand", "command_name").
disable_mode = "disable" # Can be "disable" or "remove". "remove" removes the command entirely until the next reboot.
modules = ["cogs.*"] # a list of modules to load. Glob-based patterns can be used. Defaults to all cogs if ommitted
delay = { 421698654189912064 = [0.1, 2.5] } # Adds an artificial delay before running commands. Useful for a soft-ban.
# Values must range from 0.01 (minimum) and 2.8 (maximum).
[logging]
level = "DEBUG" # can be one of DEBUG, INFO, WARNING, ERROR, CRITICAL. Defaults to INFO

View file

@ -124,6 +124,8 @@ for module in CONFIG["jimmy"].get("modules", ["cogs/*.py"]):
except (discord.ExtensionNotFound, ModuleNotFoundError):
log.debug("%r does not exist - trying as glob pattern", module)
for ext in glob.glob(module, recursive=True):
if ext.endswith(("__init__.py", ".pyc")):
continue
ext = ext.replace("/", ".")[:-3] # foo/bar.py -> foo.bar
if ext.split(".")[-1].startswith("__"):
log.info("Skipping loading %s - starts with double underscore." % ext)
@ -194,7 +196,25 @@ async def delete_message(ctx: discord.ApplicationContext, message: discord.Messa
async def check_is_enabled(ctx: commands.Context | discord.ApplicationContext) -> bool:
disabled = CONFIG["jimmy"].get("disabled_commands", [])
if ctx.command.qualified_name in disabled:
raise commands.DisabledCommand("%s is disabled via this instance's configuration file.")
raise commands.DisabledCommand(
"%s is disabled via this instance's configuration file." % (
ctx.command.qualified_name
)
)
return True
@bot.before_invoke
async def add_delays(ctx: commands.Context | discord.ApplicationContext) -> bool:
blocked = CONFIG["jimmy"].get("delay", {})
if str(ctx.author.id) in blocked:
range_start, range_end = blocked[str(ctx.author.id)]
range_start = max(0.01, range_start)
range_end = min(2.89, range_end)
n = random.randint(round(range_start * 100), round(range_end * 100)) / 100
log.warning("Delaying user %s by %.2f seconds.", ctx.author, n)
await asyncio.sleep(n)
log.info("Artificial delay for %s lifted", ctx.author)
return True
@ -202,4 +222,15 @@ if not CONFIG["jimmy"].get("token"):
log.critical("No token specified in config.toml. Exiting. (hint: set jimmy.token in config.toml)")
sys.exit(1)
__disabled = CONFIG["jimmy"].get("disabled_commands", [])
__disabled_mode = CONFIG["jimmy"].get("disabled_mode", "disabled").lower()
if __disabled:
if __disabled_mode == "delete":
for __command__name in __disabled:
_cmd = bot.get_application_command(__command__name)
if not _cmd:
log.warning("Unknown command %r - cannot remove.", __command__name)
else:
bot.remove_application_command(_cmd)
bot.run(CONFIG["jimmy"]["token"])