Beispiel #1
0
    def add_nickname(self, u, dusr):
        self._logger.trace("add_nickname")
        usr = cassandra.get_user(u)

        if dusr.nick is None:
            return False  # Has no nick

        if dusr.nick in usr.nicknames:
            # Dispatch event
            cassandra.dispatch("nickname-change", {
                "user": usr,
                "nick": dusr.nick,
                "known": True
            })
            return False  # Known nick

        # Dispatch event
        cassandra.dispatch("nickname-change", {
            "user": usr,
            "nick": dusr.nick,
            "known": False
        })

        usr.nicknames.append(dusr.nick)

        return True  # New nick
Beispiel #2
0
async def unload(ctx, name):
    global help_pages

    cog = None
    for c in active_cogs:
        if c.name == name:
            cog = c
            break

    if cog is None:
        await ctx.send("No active cog with name %s found!" % name)
        return

    # Dispatch preload event
    response = cassandra.dispatch(
        "cog-unloading",
        type('', (object, ), {
            "name": name,
            "cancel": False
        })())

    if response.cancel == True:
        await ctx.send("Cog %s has been stoped form unloading!" % name)
        return

    # TODO: Unregister all event handlers for this cog

    active_cogs.remove(cog)
    bot.unload_extension("cogs.%s" % name)
    help_pages = None  # Clear help page cache

    # Dispatch event
    cassandra.dispatch("cog-unloaded", type('', (object, ), {"name": name})())

    await ctx.send("Cog %s unloaded." % name)
Beispiel #3
0
    def add_name(self, u, dusr):
        self._logger.trace("add_name")
        usr = cassandra.get_user(u)

        name = dusr.name + "#" + dusr.discriminator

        if name in usr.names:
            # Dispatch event
            cassandra.dispatch("name-change", {
                "user": usr,
                "name": name,
                "known": True
            })
            return False  # Known name

        # Dispatch event
        cassandra.dispatch("name-change", {
            "user": usr,
            "name": dusr.nick,
            "known": False
        })

        usr.names.append(name)

        return True  # New name
Beispiel #4
0
    async def warn(self, ctx, member: discord.Member, *, reason=None):
        self._logger.trace("warn")
        # Audit log
        caudit = self.client.get_cog('Audit')
        if caudit is not None:
            if reason is None:
                await caudit.print_audit(
                    ctx.author, 'warning', "Warned %s#%s for no reason." %
                    (member.name, member.discriminator))
            else:
                await caudit.print_audit(
                    ctx.author, 'warning', "Warned %s#%s for **\"%s\"**." %
                    (member.name, member.discriminator, reason))

        reason_str = reason
        if reason is None:
            reason_str = "No reason given"

        embed = discord.Embed(title="%s %s: You have been warned!" %
                              (EMOJI_WARN, ctx.guild.name),
                              description="Reason: %s" % reason_str,
                              colour=discord.Colour.gold())

        await member.send(embed=embed)

        usr = cassandra.get_user((guild.id, member.id))

        # Dispatch event
        cassandra.dispatch("mod-warning", {
            "dauthor": ctx.author,
            "reason": reason,
            "duser": member,
            "user": usr
        })
Beispiel #5
0
    async def clear(self, ctx, amt=10):
        self._logger.trace("clear")
        if not type(amt) is int:
            try:
                amt = int(amt, 10)
            except ValueError:
                await ctx.send("Invalid number of messages given")
                return

        if amt < 1:
            await ctx.send("Can not remove %s messages" % amt)
            return
        elif amt > 100:
            amt = 100

        await ctx.channel.purge(limit=amt + 1)

        # Audit log
        caudit = self.client.get_cog('Audit')
        if caudit is not None:
            await caudit.print_audit(
                ctx.author, 'clear', "Cleared %s messages from channel <#%s>" %
                (amt, ctx.channel.id))

        # Dispatch event
        cassandra.dispatch("mod-clear", {
            "dauthor": ctx.author,
            "amount": amt,
            "channel": ctx.channel
        })
Beispiel #6
0
    async def quarantine(self, ctx, member: discord.Member, *, reason=None):
        self._logger.trace("quarantine")

        usr = cassandra.get_user((ctx.guild.id, member.id))
        if usr.quarantine_status == True:
            await ctx.send("This user is already in quarantine!")
            return

        # Audit log
        caudit = self.client.get_cog('Audit')
        if caudit is not None:
            if reason is None:
                await caudit.print_audit(
                    ctx.author, 'quarantine',
                    "Quarantined %s#%s for no reason." %
                    (member.name, member.discriminator))
            else:
                await caudit.print_audit(
                    ctx.author, 'quarantine',
                    "Quarantined %s#%s for **\"%s\"**." %
                    (member.name, member.discriminator, reason))

        old_roles = []
        for role in member.roles:
            if role == ctx.guild.default_role:
                continue
            old_roles.append(role.id)
            try:
                await member.remove_roles(role, reason="Quarantine")
            except Exception as ex:
                self._logger.warn(
                    "Failed to remove role %s from member %s: %s" %
                    (role.name, member.name, ex))

        if usr.server.quarantine_role is not None:
            qrole = get(ctx.guild.roles, id=usr.server.quarantine_role)
            if qrole is not None:
                try:
                    await member.add_roles(qrole, reason="Quarantine")
                except Exception as ex:
                    self._logger.warn(
                        "Failed to add role %s to member %s: %s" %
                        (qrole.name, member.name, ex))

        # Dispatch event
        cassandra.dispatch("mod-quarantine", {
            "dauthor": ctx.author,
            "reason": reason,
            "duser": member,
            "user": usr
        })

        usr.quarantine_status = True
        usr.quarantine_roles = old_roles
        cassandra.save_user(usr)

        await ctx.send("User %s#%s has been quarantined." %
                       (member.name, member.discriminator))
Beispiel #7
0
    async def unquarantine(self, ctx, member: discord.Member):
        self._logger.trace("unquarantine")

        usr = cassandra.get_user((ctx.guild.id, member.id))
        if usr.quarantine_status == False:
            await ctx.send("This user is not in quarantine!")
            return


# Audit log
        caudit = self.client.get_cog('Audit')
        if caudit is not None:
            if reason is None:
                await caudit.print_audit(
                    ctx.author, 'unquarantine', "Unquarantined %s#%s." %
                    (member.name, member.discriminator))

        if usr.server.quarantine_role is not None:
            qrole = get(ctx.guild.roles, id=usr.server.quarantine_role)
            if qrole is not None:
                try:
                    await member.remove_roles(qrole, reason="Unquarantine")
                except Exception as ex:
                    self._logger.warn(
                        "Failed to remove role %s from member %s: %s" %
                        (qrole.name, member.name, ex))

        for roleid in usr.quarantine_roles:
            role = get(ctx.guild.roles, id=roleid)
            if role is None:
                continue
            if role == ctx.guild.default_role:
                continue
            try:
                await member.add_roles(role, reason="Unquarantine")
            except Exception as ex:
                self._logger.warn("Failed to add role %s to member %s: %s" %
                                  (role.name, member.name, ex))

        # Dispatch event
        cassandra.dispatch("mod-unquarantine", {
            "dauthor": ctx.author,
            "duser": member,
            "user": usr
        })

        usr.quarantine_status = False
        usr.quarantine_roles = []
        cassandra.save_user(usr)

        await ctx.send("User %s#%s has been released form quarantine." %
                       (member.name, member.discriminator))
Beispiel #8
0
async def load(ctx, name):
    global help_pages

    cog = None

    for cogname in cfg.cogs:
        if cogname == name:
            cog = cfg.cogs[cogname]
            break

    if cog is None:
        await ctx.send("No cog with name %s found!" % name)
        return

    if cog in active_cogs:
        await ctx.send(
            "Cog %s is already loaded! You may want to use Reload!" % name)
        return

    # Dispatch preload event
    response = cassandra.dispatch(
        "cog-loading",
        type('', (object, ), {
            "name": name,
            "cancel": False
        })())

    if response.cancel == True:
        await ctx.send("Cog %s has been stoped form loading!" % name)
        return

    bot.load_extension("cogs.%s" % name)

    active_cogs.append(cog)
    help_pages = None  # Clear help page cache

    # Dispatch event
    cassandra.dispatch("cog-loaded", type('', (object, ), {"name": name})())

    await ctx.send("Cog %s loaded." % name)
Beispiel #9
0
    def add_balance(self, u, amt):
        self._logger.trace("add_balance")
        usr = cassandra.get_user(u)
        old = usr.balance

        resp = cassandra.dispatch("balance-change", {
            "old": old,
            "new": old + amt,
            "user": usr
        })

        usr.balance = resp.new
        return usr.balance  # New balance
Beispiel #10
0
    async def update_user(self, guild, user, lv_role_cache=None):
        self._logger.trace("update_user(%s)" % user.id)
        if lv_role_cache is None:
            lv_role_cache = self._build_level_rank_cache(guild)

        # If user is a bot, ignore
        if user.bot:
            return

        level = self.client.get_cog('Xp').get_level((guild.id, user.id))

        highest_role = None
        for lv in lv_role_cache:
            role = lv_role_cache[lv]
            if level < lv:
                break
            highest_role = role

        if highest_role is None:
            return

        if highest_role in user.roles:
            return

        for urole in user.roles:
            if urole in lv_role_cache.values():
                await user.remove_roles(
                    urole, reason="Userlevel rank"
                )  # TODO: There must be a more efficient way to do this
        await user.add_roles(highest_role, reason="Userlevel change")

        # Dispatch event
        cassandra.dispatch(
            "lvrole-change", {
                "duser": user,
                "user": cassandra.get_user((guild.id, user.id)),
                "role": highest_role
            })
Beispiel #11
0
    async def ban(self, ctx, member: discord.Member, *, reason=None):
        self._logger.trace("ban")
        # TODO: Tempban

        # Audit log
        caudit = self.client.get_cog('Audit')
        if caudit is not None:
            if reason is None:
                await caudit.print_audit(
                    ctx.author, 'ban', "Baned %s#%s for no reason." %
                    (member.name, member.discriminator))
            else:
                await caudit.print_audit(
                    ctx.author, 'ban', "Baned %s#%s for **\"%s\"**." %
                    (member.name, member.discriminator, reason))

        if reason is None:
            reason = "No reason given"

        embed = discord.Embed(title="%s %s: The BAN-HAMMER has spoken!" %
                              (EMOJI_BAN, ctx.guild.name),
                              description="Reason: %s" % reason,
                              colour=discord.Colour.red())

        # We need to send the message first because discord does not let you send messages to users that have no servers in common
        await member.send(embed=embed)

        usr = cassandra.get_user((guild.id, member.id))

        # Dispatch event
        cassandra.dispatch("mod-ban", {
            "dauthor": ctx.author,
            "reason": reason,
            "duser": member,
            "user": usr
        })

        await member.ban(reason=reason)
Beispiel #12
0
    async def kick(self, ctx, member: discord.Member, *, reason=None):
        self._logger.trace("kick")

        # Audit log
        caudit = self.client.get_cog('Audit')
        if caudit is not None:
            if reason is None:
                await caudit.print_audit(
                    ctx.author, 'kick', "Kicked %s#%s for no reason." %
                    (member.name, member.discriminator))
            else:
                await caudit.print_audit(
                    ctx.author, 'kick', "Kicked %s#%s for **\"%s\"**." %
                    (member.name, member.discriminator, reason))

        if reason is None:
            reason = "No reason given"

        embed = discord.Embed(title="%s %s: You have beed kicked!" %
                              (EMOJI_INFO, ctx.guild.name),
                              description="Reason: %s" % reason,
                              colour=discord.Colour.dark_orange())

        # We need to send the message first because discord does not let you send messages to users that have no servers in common
        await member.send(embed=embed)

        usr = cassandra.get_user((guild.id, member.id))

        # Dispatch event
        cassandra.dispatch("mod-kick", {
            "dauthor": ctx.author,
            "reason": reason,
            "duser": member,
            "user": usr
        })

        await member.kick(reason=reason)