def add_message_to_db(self, message):
     from ags_experiments.client_tools import ClientTools
     self.client_tools = ClientTools(self.client)
     try:
         is_allowed = self.client_tools.channel_allowed(
             message.channel.id, message.channel, message.channel.is_nsfw())
     except AttributeError:
         is_allowed = False  # in PMs, and other channels, NSFW isn't an option
     if is_allowed:
         try:
             while True:
                 result = cursor.fetchone()
                 if result is not None:
                     logger.debug(result + " - < Unread result")
                 else:
                     break
             cursor.execute(add_message_custom, (
                 int(message.id), message.author.id, str(
                     message.channel.id),
                 message.created_at.strftime('%Y-%m-%d %H:%M:%S'),
                 message.content,))
         except mysql.connector.errors.IntegrityError:
             pass
         except mysql.connector.errors.DataError:
             logger.warn(
                 "Couldn't insert {} - likely a time issue".format(message.id))
         cnx.commit()
    def __init__(self, client):
        self.client = client
        self.client_tools = ClientTools(client)
        self.database_tools = DatabaseTools(client)

        insert_channel = "INSERT INTO channels (channel_id, channel_name) VALUES (%s, %s)"
        update_channel = "UPDATE `gssp_logging`.`channels` SET `channel_name`=%s WHERE `channel_id`=%s;"
        if not bool(config['discord'].get("skip_scrape")):
            for guild in client.guilds:
                logger.info("{}: Updating channels".format(str(guild)))
                for channel in guild.text_channels:
                    try:
                        cursor.execute(
                            insert_channel,
                            (channel.id, emoji.demojize(channel.name)))
                        logger.debug("Inserted {} to DB".format(
                            emoji.demojize(channel.name)))
                    except mysql.connector.errors.IntegrityError:
                        cursor.execute(
                            update_channel,
                            (emoji.demojize(channel.name), channel.id))
                        logger.debug("Updated {}".format(
                            emoji.demojize(channel.name)))
                logger.info("{}: Updating users".format(str(guild)))
                for member in tqdm(guild.members,
                                   total=len(guild.members),
                                   desc="Adding users for {}".format(
                                       str(guild))):
                    self.database_tools.add_user(member)

                logger.info("{}: Finished {} users".format(
                    str(guild), len(guild.members)))
                logger.info("{}: Updating roles".format(str(guild)))
                for role in guild.roles:
                    if role.name != "@everyone":
                        try:
                            cursor.execute(insert_role,
                                           (role.id, emoji.demojize(role.name),
                                            guild.id, int(role.mentionable)))
                        except mysql.connector.errors.IntegrityError:
                            cursor.execute(update_role, (emoji.demojize(
                                role.name), int(role.mentionable), role.id))

                        # this is designed to assist with migration, by moving old discord role members over to the new
                        # system seamlessly
                        member_ids = []
                        for member in role.members:
                            member_ids.append(member.id)
                        role_db = DbRole(role.id,
                                         role.name,
                                         0,
                                         members=member_ids)
                        role_db.save_members()
                logger.info("{}: Finished {} roles".format(
                    guild, len(guild.roles)))
                cnx.commit()
        else:
            logger.warn(
                "Skipping scraping data from existing servers - data may be out of date"
            )
Ejemplo n.º 3
0
 def __init__(self, client):
     # self.automated = subprocess.Popen(
     #    [sys.executable, "automated_messages.py"])
     self.client = client
     self.client_tools = ClientTools(client)
     # we look for ".admin" and then add "." to prevent matching a root directory ending in admin
     
     self.load_all_extensions()
Ejemplo n.º 4
0
 def __init__(self, client):
     self.client = client
     self.client_extras = ClientTools(client)
     self.database_extras = DatabaseTools(self.client_extras)
Ejemplo n.º 5
0
import sys
import concurrent

import discord
import markovify
import time
from discord.ext import commands
from discord import Embed
from ags_experiments.client_tools import ClientTools
from ags_experiments.database import cnx
from ags_experiments.database.database_tools import DatabaseTools
from ags_experiments.settings.config import config, strings

client = commands.Bot(command_prefix="--------------------",
                      owner_id=config['discord']['owner_id'])
client_tools = ClientTools(client)
database_tools = DatabaseTools(client)

print("Awaiting login")

position = 0
opted_in_users = []
channel = None
server = None

async def get_members(server, message=None):
    members = []
    for user in server.members:
        if database_tools.is_automated(user):
            members.append(user)
            if message is not None:
class DatabaseTools():
    def __init__(self, client):
        self.client = client

    def add_message_to_db(self, message):
        from ags_experiments.client_tools import ClientTools
        self.client_tools = ClientTools(self.client)
        try:
            is_allowed = self.client_tools.channel_allowed(
                message.channel.id, message.channel, message.channel.is_nsfw())
        except AttributeError:
            is_allowed = False  # in PMs, and other channels, NSFW isn't an option
        if is_allowed:
            try:
                while True:
                    result = cursor.fetchone()
                    if result is not None:
                        logger.debug(result + " - < Unread result")
                    else:
                        break
                cursor.execute(add_message_custom, (
                    int(message.id), message.author.id, str(
                        message.channel.id),
                    message.created_at.strftime('%Y-%m-%d %H:%M:%S'),
                    message.content,))
            except mysql.connector.errors.IntegrityError:
                pass
            except mysql.connector.errors.DataError:
                logger.warn(
                    "Couldn't insert {} - likely a time issue".format(message.id))
            cnx.commit()

    def opted_in(self, user=None, user_id=None):
        """
        ID takes priority over user if provided

        User: Logged username in DB
        ID: ID of user

        Returns true if user is opted in, false if not
        """
        try:
            cursor.fetchall()  # we do this just to make sure we don't get any erorrs from MySQL later
        except mysql.connector.errors.InterfaceError:
            pass
        if user_id is None:
            get_user = "******"
        else:
            get_user = "******"
            user = user_id
        cursor.execute(get_user, (user,))
        results = cursor.fetchall()
        try:
            if results[0][0] != 1:
                return False
        except IndexError:
            return False
        return results[0][1]

    async def save_markov(self, model, user_id):
        """
        Save a model to markov table

        user_id : user's ID we want to save for
        model: Markov model object
        """
        save = "INSERT INTO `markovs` (`user`, `markov_json`) VALUES (%s, %s);"
        save_update = "UPDATE `markovs` SET `markov_json`=%s WHERE `user`=%s;"

        try:
            cursor.execute(save, (user_id, model.to_json()))
        except mysql.connector.errors.IntegrityError:
            cursor.execute(save_update, (model.to_json(), user_id))
        cnx.commit()
        return

    async def get_blocklist(self, user_id):
        user_id = str(user_id)
        get = "SELECT blocklist FROM blocklists WHERE user_id = %s"
        cursor.execute(get, (user_id,))
        resultset = cursor.fetchall()
        if not resultset:
            # add a blank blocklist
            create_user = "******"
            cursor.execute(create_user, (user_id,))
            return []
        return json.loads(resultset[0][0])

    def is_automated(self, user):
        """
        Returns true if user is opted in to automation, false if not
        """
        cnx.commit()
        get_user = "******"
        cursor.execute(get_user, (user.id,))
        results = cursor.fetchall()
        cnx.commit()
        try:
            if results[0][0] != 1:
                return False
        except IndexError:
            return False
        return True

    async def get_messages(self, user_id, limit: int, server=False):
        """
        user_id : ID of user you want to get messages for

        Returns:

        messages: list of all messages from a user
        channels: list of all channels relevant to messages, in same order
        """
        if server:
            get_messages = "SELECT `contents`, `channel_id` FROM `messages_detailed` ORDER BY TIME DESC LIMIT " + str(
                int(limit))
            cursor.execute(get_messages)
        else:
            get_messages = "SELECT `contents`, `channel_id` FROM `messages_detailed` WHERE `user_id` = %s ORDER BY TIME DESC LIMIT " + str(
                int(limit))
            cursor.execute(get_messages, (user_id,))
        results = cursor.fetchall()
        messages = []
        channels = []
        if server is True:
            blocklist = []
        else:
            blocklist = await self.get_blocklist(user_id)
        for result in results:
            valid = True
            for word in result[0].split(" "):
                if word in blocklist:
                    valid = False
            if valid:
                messages.append(result[0])
                channels.append(result[1])

        return messages, channels

    async def get_message_count(self, user_id=None):
        """
        Get number of messages sent
        You can specify user_id to get messages from only one user
        """
        if user_id is None:
            query = "SELECT COUNT(*) as message_count FROM messages_detailed"
            cursor_dict.execute(query)
        else:
            query = "SELECT COUNT(*) as message_count FROM messages_detailed WHERE user_id = %s"
            cursor_dict.execute(query, (user_id, ))
        res = cursor_dict.fetchall()[0]
        return int(res['message_count'])

    def add_user(self, member):
        try:
            cursor.execute(insert_users, (member.id,))
            cnx.commit()
        except mysql.connector.errors.IntegrityError:
            pass  # we pass because we just want to make sure we add any new users, so we expect some already here
        try:
            cursor.execute(insert_settings, (member.id,))
            cnx.commit()
        except mysql.connector.errors.IntegrityError:
            pass  # see above
Ejemplo n.º 7
0
 def __init__(self, client):
     self.client = client
     self.prefix = config['discord']['prefix']
     self.client_extras = ClientTools(client)
     self.database_extras = DatabaseTools(self.client_extras)
Ejemplo n.º 8
0
 def __init__(self, client):
     self.client = client
     self.client_tools = ClientTools(client)