Skip to content

Commit

Permalink
Add command invocation lock
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-ni committed Dec 2, 2021
1 parent 40e3da3 commit 88d0ed3
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import aiohttp
import discord
import uvloop
from aioredis_lock import RedisLock
from aioredis_lock import LockTimeoutError, RedisLock
from discord.ext import commands
from expiringdict import ExpiringDict

Expand Down Expand Up @@ -136,6 +136,20 @@ async def on_message(self, message: discord.Message):

await self.process_commands(message)

async def invoke(self, ctx):
if ctx.command.name in {"admin", "jishaku"} or (
ctx.command.root_parent and ctx.command.root_parent.name in {"admin", "jishaku"}
):
return await super().invoke(ctx)

try:
async with RedisLock(self.redis, f"command:{ctx.author.id}", 60, 1):
return await super().invoke(ctx)
except LockTimeoutError:
await ctx.reply(
"You are currently running another command. Please wait and try again later."
)

async def before_identify_hook(self, shard_id, *, initial=False):
async with RedisLock(self.redis, f"identify:{shard_id % 16}", 5, None):
await asyncio.sleep(5)
Expand Down

0 comments on commit 88d0ed3

Please sign in to comment.