Skip to content

Instantly share code, notes, and snippets.

@XuaTheGrate
Created April 16, 2019 09:23
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save XuaTheGrate/77fa5dfa7182cfcefb8f60e5b4ebd38b to your computer and use it in GitHub Desktop.
Save XuaTheGrate/77fa5dfa7182cfcefb8f60e5b4ebd38b to your computer and use it in GitHub Desktop.
Timed Mute for Latest Discord.py
"""
Updated for discord.py 1.0.x / Rewrite
Original credit goes to Vexs
"""
import discord
import asyncio
import re
from discord.ext import commands
import sys
import traceback
time_regex = re.compile("(?:(\d{1,5})(h|s|m|d))+?")
time_dict = {"h":3600, "s":1, "m":60, "d":86400}
class TimeConverter(commands.Converter):
async def convert(self, ctx, argument):
args = argument.lower()
matches = re.findall(time_regex, args)
time = 0
for v, k in matches:
try:
time += time_dict[k]*float(v)
except KeyError:
raise commands.BadArgument("{} is an invalid time-key! h/m/s/d are valid!".format(k))
except ValueError:
raise commands.BadArgument("{} is not a number!".format(v))
return time
class MuteCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.has_permissions(manage_roles=True)
async def mute(self, ctx, member:discord.Member, *, time:TimeConverter = None):
"""Mutes a member for the specified time- time in 2d 10h 3m 2s format ex:
&mute @Someone 1d"""
role = discord.utils.get(ctx.guild.roles, name="Muted")
await member.add_roles(role)
await ctx.send(("Muted {} for {}s" if time else "Muted {}").format(member, time))
if time:
await asyncio.sleep(time)
await member.remove_roles(role)
#You really should use an external error handler- like the one here: https://gist.github.com/Vexs/daa1dcc92ff80fad7ca020d0f7bb4f75
@mute.error
async def mute_error(self, ctx, error):
if isinstance(error, commands.CheckFailure):
pass
if isinstance(error, commands.BadArgument):
await ctx.send(error)
else:
error = getattr(error, 'original', error)
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
def setup(bot):
bot.add_cog(MuteCog(bot))
@SSagun-c
Copy link

SSagun-c commented Jun 5, 2021

Thanks!

@SpaceManQQ
Copy link

Ignoring exception in command None:
discord.ext.commands.errors.CommandNotFound: Command "mute" is not found

@vyishere
Copy link

vyishere commented Oct 19, 2021

Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "mute" is not found

Line 35, try @commands.command(name='mute').
You should define what is mute first.

@SpaceManQQ
Copy link

Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "mute" is not found

Line 35, try @commands.command(name='mute'). You should define what is mute first.

I already tryed it. Didn't worked, idk why

@eterise
Copy link

eterise commented Mar 11, 2022

good code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment