async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith(prefix):

        # get the message content
        content = message.content

        # get rid of the prefix
        command_content = content[len(prefix):].lstrip()
        response = commands.parseMessage(command_content, message.author)

        chunks = utils.split_into_chunks(response.encode('utf-8'), 1024)
        embed = discord.Embed(color=colour)
        title = command_content.split()[0] if len(
            command_content.split()) > 0 else 'Empty Message'
        for i, chunk in enumerate(chunks):
            title = "(cont'd)" if i > 0 else title
            embed.add_field(name=title,
                            value=chunk.decode('utf-8'),
                            inline=False)
            embed.set_thumbnail(url=message.author.avatar_url)
            embed.set_footer(text="Long live the Sultan")
            await message.channel.send(embed=embed)
Exemplo n.º 2
0
    async def on_message(message: discord.Message):
        if message.author == discord_client.user:
            return
        global config_prefix

        content = message.content.lstrip()

        prefixes = (
            '<@%s>' % discord_client.user.id,
            '<@!%s>' % discord_client.user.id)

        if config_prefix is not None:
            if isinstance(config_prefix, (list, tuple)):
                prefixes += tuple(config_prefix)
            elif isinstance(config_prefix, str):
                prefixes += (config_prefix.lower(),)

        if content.lower().startswith(prefixes):  # Checking all messages that start with the prefix.
            prefix = [prefix for prefix in prefixes if content.lower().startswith(prefix)][0]
            command_content = content[len(prefix):].lstrip()
            response = discord_postprocess(
                run_command(
                    DiscordAccountId(str(message.author.id)),
                    command_content,
                    server))

            chunks = split_into_chunks(response.encode('utf-8'), 1024)

            title = command_content.split()[0] if len(command_content.split()) > 0 else 'Empty Message'

            message_obj = DiscordMessage(message.author, chunks, title)

            await message_obj.send(message.channel)
            messages[message_obj.message.id] = message_obj
 def test_split_into_chunks(self):
     """Tests that a message can be split into chunks."""
     self.assertListEqual(
         split_into_chunks(b'a' * 20 + b'\n' + b'a' * 20, 30),
         [b'a' * 20, b'a' * 20])
     self.assertListEqual(
         split_into_chunks(b'a' * 40 + b'\n' + b'a' * 10, 30),
         [b'a' * 30, b'a' * 10 + b'\n' + b'a' * 10])
     self.assertListEqual(
         split_into_chunks(b'a' * 10 + b'\n' + b'a' * 10 + b'\n' + b'a' * 10, 30),
         [b'a' * 10 + b'\n' + b'a' * 10, b'a' * 10])
     self.assertListEqual(
         split_into_chunks(b'a' * 10 + b'\n' + b'a' * 10 + b'\n' + b'a' * 50, 30),
         [b'a' * 10 + b'\n' + b'a' * 10, b'a' * 30, b'a' * 20])
     self.assertListEqual(
         split_into_chunks(b'a' * 10 + b'\n' + b'a' * 10 + b'\n' + b'a' * 80, 30),
         [b'a' * 10 + b'\n' + b'a' * 10, b'a' * 30, b'a' * 30, b'a' * 20])
Exemplo n.º 4
0
    async def on_message(message: discord.Message):
        if message.guild.id != primary_guild_id:
            return
        if message.author == discord_client.user:
            return
        global config_prefix

        content = message.content.lstrip()

        prefixes = ('<@%s>' % discord_client.user.id,
                    '<@!%s>' % discord_client.user.id)

        if config_prefix is not None:
            if isinstance(config_prefix, (list, tuple)):
                prefixes += tuple(config_prefix)
            elif isinstance(config_prefix, str):
                prefixes += (config_prefix.lower(), )

        if content.lower().startswith(
                prefixes):  # Checking all messages that start with the prefix.
            prefix = [
                prefix for prefix in prefixes
                if content.lower().startswith(prefix)
            ][0]
            command_content = content[len(prefix):].lstrip()
            response = run_command(DiscordAccountId(str(message.author.id)),
                                   command_content, server)

            if isinstance(response, dict):
                params = response
                response = params["response"]
                if "to_be_muted" in params:
                    user_id = int(params["to_be_muted"].discord_id)
                    guild = primary_guild
                    member = guild.get_member(user_id)
                    role = guild.get_role(muted_role_id)
                    if role not in member.roles:
                        await member.add_roles(role)
            response = discord_postprocess(response)

            chunks = split_into_chunks(response.encode('utf-8'), 1024)

            title = command_content.split()[0] if len(
                command_content.split()) > 0 else 'Empty Message'

            message_obj = DiscordMessage(message.author, chunks, title)

            await message_obj.send(message.channel)
            messages[message_obj.message.id] = message_obj
Exemplo n.º 5
0
    async def on_message(message):
        if message.author == discord_client.user:
            return
        global config_prefix

        content = message.content.lstrip()

        prefixes = (
            '<@%s>' % discord_client.user.id,
            '<@!%s>' % discord_client.user.id)

        if config_prefix is not None:
            if isinstance(config_prefix, (list, tuple)):
                prefixes += tuple(config_prefix)
            else:
                prefixes += (config_prefix.lower(),)

        if content.lower().startswith(prefixes): # Checking all messages that start with the prefix.
            prefix = [prefix for prefix in prefixes if content.lower().startswith(prefix)][0]
            command_content = content[len(prefix):].lstrip()
            response = discord_postprocess(
                process_command(
                    DiscordAccountId(str(message.author.id)),
                    command_content,
                    server,
                    prefixes[0] + ' '))

            chunks = split_into_chunks(response.encode('utf-8'), 1024)
            try:
                embed = discord.Embed(color=int(config["colour"], base=16))
            except Exception as e:
                print_bad("Embed Colour")
                embed = discord.Embed()
                pass
            title = command_content.split()[0] if len(command_content.split()) > 0 else 'Empty Message'
            for i, chunk in enumerate(chunks):
                title = "(cont'd)" if i > 0 else title
                embed.add_field(name=title, value=chunk.decode('utf-8'), inline=False)

            embed.set_thumbnail(url=message.author.avatar_url)
            embed.set_footer(text="This was sent in response to %s's message; you can safely disregard it if that's not you." % message.author.name)

            await message.channel.send(embed=embed)
Exemplo n.º 6
0
def get_updated_reactions(parent_id: int,
                          chat_id: int) -> InlineKeyboardMarkup:
    msg_id = make_msg_id(parent_id, chat_id)

    with get_conn() as conn:
        ret = conn.execute(
            "select type, cnt, (select min(timestamp) from reaction where type=subq.type and parent=?) as time from (SELECT type, count(*) as cnt from reaction where parent=? group by type) as subq order by -cnt, time;",
            (msg_id, msg_id),
        )
        reactions = list(ret.fetchall())

    markup = [
        InlineKeyboardButton(get_reaction_representation(r[0],
                                                         r[1],
                                                         with_count=True),
                             callback_data=r[0]) for r in reactions
    ]

    if SETTINGS.show_summary_button:
        markup.append(get_show_reaction_stats_button(chat_id, parent_id))

    return InlineKeyboardMarkup(inline_keyboard=split_into_chunks(markup, 4), )
Exemplo n.º 7
0
def kth_by_mom(unsorted_list, k):
    if len(unsorted_list) <= CHUNK_SIZE:
        return get_kth(unsorted_list, k)

    chunks = split_into_chunks(unsorted_list, CHUNK_SIZE)

    medians_list = []

    for chunk in chunks:
        median_chunk = get_median(chunk)
        medians_list.append(median_chunk)

    size = len(medians_list)
    mom = kth_by_mom(medians_list, size / 2 + (size % 2))
    smaller, larger = split_list_by_pivot(unsorted_list, mom)
    values_before_mom = len(smaller)

    if values_before_mom == (k - 1):
        return mom
    elif values_before_mom > (k - 1):
        return kth_by_mom(smaller, k)
    else:
        return kth_by_mom(larger, k - values_before_mom - 1)
Exemplo n.º 8
0
    async def on_message(message):
        if message.author == discord_client.user:
            return

        content = message.content.lstrip()
        prefixes = (
            '<@%s>' % discord_client.user.id,
            '<@!%s>' % discord_client.user.id)

        if content.startswith(prefixes): # Checking all messages that start with the prefix.
            response = discord_postprocess(
                process_command(
                    DiscordAccountId(str(message.author.id)),
                    content[content.index('>') + 1:].lstrip(),
                    server,
                    prefixes[0] + ' '))

            chunks = split_into_chunks(response.encode('utf-8'), 1024)
            embed = discord.Embed(color=0x3b4dff)
            for i, chunk in enumerate(chunks):
                title = "(cont'd)" if i > 0 else 'Response to %s' % message.author.name
                embed.add_field(name=title, value=chunk.decode('utf-8'), inline=False)

            await message.channel.send(embed=embed)
Exemplo n.º 9
0
print('Data loading...')
timeseries, dates = utils.load_snp_close()
#print("timeseries:", timeseries)
#print("dates:", dates)
dates = [dt.datetime.strptime(d, '%Y-%m-%d').date() for d in dates]
plt.title("APPL stock close")
plt.plot(dates, timeseries)

TRAIN_SIZE = 20
TARGET_TIME = 1
LAG_SIZE = 1
EMB_SIZE = 1

X, Y = utils.split_into_chunks(timeseries,
                               TRAIN_SIZE,
                               TARGET_TIME,
                               LAG_SIZE,
                               binary=False,
                               scale=True)
X, Y = np.array(X), np.array(Y)
X_train, X_test, Y_train, Y_test = utils.create_Xt_Yt(X, Y, percentage=0.9)

Xp, Yp = utils.split_into_chunks(timeseries,
                                 TRAIN_SIZE,
                                 TARGET_TIME,
                                 LAG_SIZE,
                                 binary=False,
                                 scale=False)
Xp, Yp = np.array(Xp), np.array(Yp)
X_trainp, X_testp, Y_trainp, Y_testp = utils.create_Xt_Yt(Xp,
                                                          Yp,
                                                          percentage=0.9)
Exemplo n.º 10
0
test_name = "enron_3000"
split_ratio = 0
# training_set, testing_set = split_dataset(init(train_name), split_ratio)
training_set = init(train_name)
testing_set = init(test_name)
print("size of training set %d" % len(training_set))
print("size of testing set %d" % len(testing_set))

nb = NaiveBayes()
with_time(nb.fit, [training_set])
with_time(nb.train, [])
# nb.fit(training_set).train()

workers = 6  # IMPORTANT: should be number of physical cores of your PC
chunk_size = 100  # size of each chunk
chunks = list(split_into_chunks(testing_set, chunk_size))
chunks_num = len(chunks)
print("total", chunks_num, "chunks")


def process(chunk_id):
    prompt = 'chunk {}/{}'.format(chunk_id, chunks_num)
    print(prompt, "starts")
    tests = chunks[chunk_id]
    # here we assume that spam is positive
    TP = FP = TN = FN = 0
    for test in tests:
        predict = nb.predict(test.words)
        if test.label == predict:
            if predict == "spam":
                TP += 1