Skip to content

Instantly share code, notes, and snippets.

@Vexs
Last active June 8, 2018 05:06
Show Gist options
  • Save Vexs/aa3aa3e4374d6f4226f15576c9105858 to your computer and use it in GitHub Desktop.
Save Vexs/aa3aa3e4374d6f4226f15576c9105858 to your computer and use it in GitHub Desktop.
Basic mute
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):
args = self.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:
""""""
def __init__(self, bot):
self.bot = bot
@commands.command(pass_context=True)
@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.message.server.roles, name="Muted")
await self.bot.add_roles(member, role)
await self.bot.say(("Muted {} for {}s" if time else "Muted {}").format(member, time))
if time:
await asyncio.sleep(time)
await self.bot.remove_roles(member, 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, error, ctx):
if isinstance(error, commands.CheckFailure):
pass
if isinstance(error, commands.BadArgument):
await self.bot.send_message(ctx.message.channel, 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment