Example #1
0
    async def create_autochannel(self, s, dguild, duser=None):
        srv = cassandra.get_server(s)

        # Check if autochannels are configured
        if srv.autochannel_router is None:
            self._logger.debug("router channel not configured")
            return False

        # Find router channel
        router = get(dguild.channels, id=srv.autochannel_router)
        if router is None:
            self._logger.debug("router channel not found")
            return False

        # Get category
        cat = router.category
        if cat is None:
            self._logger.debug("router channel has no category")
            return False

        name = "Autochannel"
        if duser is not None:
            name = "AC %s" % (duser.display_name)
        channel = await cat.create_voice_channel(name,
                                                 reason="New AutoChannel")

        # Save changes
        srv.autochannel_channels.append(channel.id)
        cassandra.save_server(srv)

        # Move member to new channel
        if duser is not None:
            await duser.edit(voice_channel=channel)

        return True
Example #2
0
def get_prefix(client, message):
    # Do not do anything on private messages
    if message.guild == None:
        return str(random.randrange(-99999999, 99999999))

    srv = cassandra.get_server(message.guild)
    return srv.prefix_used
Example #3
0
 async def lvroles(self, ctx):
     srv = cassandra.get_server(ctx.guild.id)
     if len(srv.level_roles) > 0:
         text = "**Level Roles**\n\n"
         lvrolecache = self._build_level_rank_cache(ctx.guild, srv)
         for lv, role in lvrolecache.items():
             text = text + ("%s: <@&%s>\n" % (lv, role.id))
         await ctx.send(text)
     else:
         await ctx.send("This server has no level roles configured.")
Example #4
0
    def _build_level_rank_cache(self, guild, srv=None):
        self._logger.trace("_build_level_rank_cache")
        if srv is None:
            srv = cassandra.get_server(guild.id)

        lv_role_cache = {}
        for lv in srv.level_roles:
            role = get(guild.roles, id=srv.level_roles[lv])
            if role is None:
                continue
            lv_role_cache[int(lv)] = role
        return dict(sorted(lv_role_cache.items(), key=lambda item: item[0]))
Example #5
0
    async def on_voice_state_update(self, member, before, after):
        srv = cassandra.get_server(member.guild.id)

        if after.channel is not None:
            if after.channel.id == srv.autochannel_router:
                self._logger.debug("user join router")
                await self.create_autochannel(srv, member.guild, member)

        if before.channel is not None:
            if before.channel.id in srv.autochannel_channels:
                self._logger.debug("user left autochannel")
                await self.delete_autochannel(srv, member.guild,
                                              before.channel)
Example #6
0
    async def lvrole(self, ctx, lvstr, rolestr=None):
        lv = None
        try:
            lv = int(lvstr)
        except Exception:
            await ctx.send("<level> must be a positive number.")
            return

        if lv < 0:
            await ctx.send("<level> must be a positive number.")
            return

        srv = cassandra.get_server(ctx.guild.id)
        if rolestr is None:
            # Remove Level role
            if lv in srv.level_roles:
                srv.level_roles.pop(lv, None)
                await ctx.send("Removed level role for level %s." % lv)
            else:
                await ctx.send("No level role is set for level %s." % lv)
        else:
            if lvstr in srv.level_roles:
                await ctx.send(
                    "Level %s currently has role %s.\nPlease remove it manualy first, if you want another role at this level."
                )
                return

            if len(rolestr) == 22 and rolestr.startswith(
                    "<@&") and rolestr.endswith(">"):
                try:
                    roleid = int(rolestr[3:21])
                except:
                    await ctx.send("Role not found %s!" % rolestr)
                    return

                role = get(ctx.guild.roles, id=int(roleid))
                if role is None:
                    await ctx.send("Role not found <@&%s>!" % roleid)
                    return

                srv.level_roles[lvstr] = roleid
                cassandra.save_server(srv)
                await ctx.send("Role <@&%s> set for level %s." % (roleid, lv))
            else:
                ctx.send("%s is not a vaild role!" % rolestr)
                return

        pass
Example #7
0
    async def on_message(self, message):
        # Ignore messages from bots
        if message.author.bot == True:
            return

        # Ignore direct messages
        if message.guild is None:
            return

        self._logger.trace("on_message(%s)" % message.content)

        srv = cassandra.get_server(message.guild.id)
        # TODO: Check if server is not found

        # Ignore commands to cassandra
        if message.content.startswith(srv.prefix_used):
            return

        # Ignore commands to other bots
        for prefix in srv.prefix_blocked:
            if message.content.startswith(prefix):
                return

        usr = cassandra.get_user((srv, message.author.id))
        # TODO: Check if user is not found

        ts = util.getts()
        usr.msg_last = ts
        usr.msg_count = usr.msg_count + 1

        #if msg timeout is reached award coins
        if ts - usr.msg_awarded > MSG_TIMEOUT:
            usr.msg_awarded = ts

            # Add balance if economy cog is loaded
            cecon = self.client.get_cog('Economy')
            if cecon is not None:
                cecon.add_balance(usr, MSG_BONUS_COINS)

            # Add xp if xp cog is loaded
            cxp = self.client.get_cog('Xp')
            if cxp is not None:
                cxp.add_xp(usr, MSG_BONUS_XP)

        cassandra.save_user(usr)
Example #8
0
    async def qrole(self, ctx, action, role: discord.Role = None):
        self._logger.trace("qrole")

        srv = cassandra.get_server(ctx.guild.id)
        if action in ["unset", "remove", "delete", "del", "rem"]:
            srv.quarantine_role = None
            cassandra.save_server(srv)

            await ctx.send("Quarantine role unset.")

        elif action in ["set", "add"]:
            if role is None:
                return  # TODO: Print help

            srv.quarantine_role = role.id
            cassandra.save_server(srv)

            await ctx.send("Quarantine role set.")
Example #9
0
    async def cleanup(self, guild=None):
        self._logger.trace("cleanup(%s)" % guild)
        if guild is None:
            # For all guilds
            for g in self.client.guilds:
                # Do not do anything for unavailable guilds
                if g.unavailable == True:
                    continue

                await self.cleanup(g)

        else:
            srv = cassandra.get_server(guild.id)
            for ac in srv.autochannel_channels:
                channel = get(guild.channels, id=ac)
                if channel is None:
                    continue

                await self.delete_autochannel(srv, guild, channel)
Example #10
0
    async def delete_autochannel(self, s, dguild, channel):
        srv = cassandra.get_server(s)

        if not channel.id in srv.autochannel_channels:
            self._logger.debug("channel not an autochannel")
            return False

        # Check if the channel is empty
        if len(channel.members) > 0:
            self._logger.debug("still users in channel")
            return False

        # Remove from server data
        srv.autochannel_channels.remove(channel.id)
        cassandra.save_server(srv)

        # Delete empty channel
        await channel.delete(reason="Empty AutoChannel")

        return True
Example #11
0
    async def autochannel(self, ctx, action, router=None):
        self._logger.trace("autochannel")

        srv = cassandra.get_server(ctx.guild.id)
        if action in ["disable", "stop"]:
            srv.autochannel_router = None
            cassandra.save_server(srv)

            await self.cleanup()

            await ctx.send(
                "Autochannels are no longer active.\nIf you want to use them in the future, you need to configure them again."
            )

        elif action in ["setup", "start", "enable"]:
            if router is None:
                return  # TODO: Print help

            routerid = None
            try:
                routerid = int(router)
            except Exception:
                return  # TODO: User feedback

            routerch = get(ctx.guild.channels, id=routerid)
            if routerch is None:
                return  # TODO: User feedback

            if routerch.category is None:
                await ctx.send(
                    "The autochannel router must be part of a channel category!"
                )
                return

            srv.autochannel_router = routerid
            cassandra.save_server(srv)

            await ctx.send("Autochannels successfully configured.")