Add extra checks on mute/ban/unmute loop. Add unmute command, closes #25

This commit is contained in:
Zeva Rose 2021-07-02 21:26:51 -06:00
parent 6e21b7bb67
commit 32084f1b30
3 changed files with 77 additions and 9 deletions

View file

@ -116,7 +116,8 @@ async def unmute():
role = guild.get_role(mute_role)
user = await guild.fetch_member(mute["user"])
if user:
await user.remove_roles(role, reason="No longer muted")
if role in user.roles:
await user.remove_roles(role, reason="Unmute")
updates.append(
pymongo.UpdateOne(
{"user": user.id, "guild": guild.id, "time": mute["time"]},

View file

@ -1,5 +1,5 @@
import jarvis
from datetime import datetime
from datetime import datetime, timedelta
from discord import User, Member
from discord.ext import commands
from discord.utils import get
@ -27,11 +27,14 @@ class AdminCog(commands.Cog):
user: User = None,
type: str = "perm",
reason: str = None,
length: int = 1,
length: int = 4,
):
if not user or user == ctx.author:
await ctx.send("You cannot ban yourself.")
return
if type == "temp" and length < 0:
await ctx.send("You cannot set a temp ban to < 0 hours.")
return
if not reason:
reason = (
"Mr. Stark is displeased with your presence. Please leave."
@ -46,8 +49,11 @@ class AdminCog(commands.Cog):
f"You have been {mtype}banned from {guild_name}."
+ " Reason:\n{reason}"
)
time = datetime.now()
expiry = None
if mtype == "temp":
user_message += f"\nDuration: {length} hours"
expiry = time + timedelta(hours=length)
await user.send(user_message)
await ctx.guild.ban(user, reason=reason)
@ -71,6 +77,7 @@ class AdminCog(commands.Cog):
"guild": ctx.guild.id,
"type": type,
"length": length,
"expiry": expiry,
"active": active,
}
)
@ -239,6 +246,10 @@ class AdminCog(commands.Cog):
)
@admin_or_permissions(mute_members=True)
async def _mute(self, ctx, user: Member, reason: str, length: int = 30):
ctx.defer()
if user == ctx.author:
await ctx.send("You cannot mute yourself.")
return
mute_setting = self.db.jarvis.settings.find_one(
{"guild": ctx.guild.id, "setting": "mute"}
)
@ -249,21 +260,75 @@ class AdminCog(commands.Cog):
return
role = get(ctx.guild.roles, id=mute_setting["value"])
await user.add_roles(role, reason=reason)
time = datetime.now()
expiry = None
if length < 0:
length = -1
if length >= 0:
expiry = time + timedelta(minutes=length)
self.db.jarvis.mutes.insert_one(
{
"user": user.id,
"reason": reason,
"admin": ctx.author.id,
"time": datetime.now(),
"time": time,
"guild": ctx.guild.id,
"length": length,
"active": True,
"expiry": expiry,
"active": True if length >= 0 else False,
}
)
self.db.jarvis.mutes.update_many(
{
"guild": ctx.guild.id,
"user": user.id,
"expiry": {"$lt": expiry},
},
{"$set": {"active": False}},
)
await ctx.send(f"{user.mention} has been muted.\nReason: {reason}")
@cog_ext.cog_slash(
name="unmute",
description="Unmute a user",
guild_ids=[418094694325813248, 578757004059738142],
options=[
create_option(
name="user",
description="User to unmute",
option_type=6,
required=True,
)
],
)
@admin_or_permissions(mute_members=True)
async def _unmute(self, ctx, user: Member):
ctx.defer()
mute_setting = self.db.jarvis.settings.find_one(
{"guild": ctx.guild.id, "setting": "mute"}
)
if not mute_setting:
await ctx.send(
"Please configure a mute role with /settings mute <role> first."
)
return
role = get(ctx.guild.roles, id=mute_setting["value"])
if role in user.roles:
await user.remove_roles(role, reason="Unmute")
else:
await ctx.send("User is not muted.")
return
self.db.jarvis.mutes.update_many(
{
"guild": ctx.guild.id,
"user": user.id,
},
{"$set": {"active": False}},
)
await ctx.send(f"{user.mention} has been unmuted.")
def setup(bot):
bot.add_cog(AdminCog(bot))

View file

@ -13,6 +13,7 @@ jarvis:
guild: Guild ID
type: String ("temp" | "soft" | "perm"), ban type, default "perm"
length: Optional(int), required for type=="temp", ban length in days, default 1
expiry: Datetime, time + length
active: boolean, if ban is active
kicks:
@ -26,19 +27,20 @@ jarvis:
user: User ID
reason: String, default "Mr. Stark is annoyed by your voice. Please be quiet."
admin: User ID, admin who muted user
time: datetime
time: Datetime
guild: Guild ID
#type: String ("voice" | "text" | "all"), mute type, default "all"
length: int, mute length in minutes, default 30, -1 for permanent
expiry: Datetime, time + length
active: boolean, if mute is active
warns:
user: User ID
reason: String, default "Mr. Stark has defined rules of conduct. Please read them."
admin: User ID, admin who warned user
time: datetime
time: Datetime
guild: Guild ID
expiry: int, hours from warning until it expires, default 24
length: int, hours from warning until it expires, default 24
expiry: Datetime, time + length
active: boolean, if warning is active or expired
jokes: