예제 #1
0
async def show(client, message, splits):
    if not splits:
        T = Reactions.getTriggers()
        if T:
            m = "Here are 10 random triggers I have " + listify(
                sample(list(T), 10))
        else:
            m = "I don't have triggers set"
    elif isEmoji(splits[0]):
        emoji = splits[0]
        T = Reactions.getTriggersFromEmoji(emoji)
        if len(T): m = '%s: %s' % (emoji, listify(T))
        else: m = '%s isn\'t triggered by anything ... yet' % emoji
    else:
        trigger = splits[0]
        E = []
        m = False
        if "..." not in message.clean_content:
            E = Reactions.getEmojisFromTrigger(trigger)
            if len(E): m = '%s: %s' % (trigger, listify(str(e) for e in E))
        else:
            T = [
                t for t in Reactions.getTriggers()
                if t.startswith(trigger.lower())
            ]
            if len(T) == 1:
                m = '"%s" is the only trigger that starts with "%s..."' % (
                    T[0], trigger)
            elif T:
                m = '%s are all triggers that start with "%s..."' % (
                    listify(T), trigger)
        if not m: m = '"%s" doesn\'t trigger anything ... yet' % trigger
    await client.send_message(message.channel, m)
예제 #2
0
async def delete(client, message, splits):
    if cantEdit(message):
        m = "You dont have permission to Edit Entries"
    elif not splits or len(splits) > 2:
        m = 'Usage: "{0} del [emoji]" or "{0} del [trigger]"'.format(
            client.user.mention)
    else:
        # Both were almost exactly the same so to reduce code repetition I did this
        if isEmoji(splits[0]):
            get = Reactions.getTriggersFromEmoji
            none = " isn't triggered by anything ..."
            rem = Reactions.removeEmoji
            sure = " triggered by "
        else:
            get = Reactions.getEmojisFromTrigger
            none = " doesn't trigger anything ..."
            rem = Reactions.removeTrigger
            sure = " triggers "
        # This is the part where it was repeated
        s0 = splits[0]  # imagine s0 is either emoji or trigger
        C = get(s0)  # C is T or E (Aka a capital letter)
        if not C: m = ('"%s"' % s0) + none
        elif len(C) == 1:
            rem(s0)  # rem is defined above
            m = '"%s" has been removed' % (s0)
            print(m)
            Reactions.write()
        else:
            m = '"%s"%s%s. Are you sure you want to remove?' % (s0, sure,
                                                                listify(C))
            await client.send_message(message.channel, m)
            msg = await client.wait_for_message(
                timeout=10,
                author=message.author,
                channel=message.channel,
                check=lambda m: 'yes' in m.clean_content.lower(
                ) or 'no' in m.clean_content.lower())
            if msg == None:
                m = 'Looks like I\'m not deleting "%s" any time soon' % s0
            elif 'yes' in msg.clean_content.lower():
                rem(s0)  # rem is defined above
                m = '"%s" has been removed' % s0
                print(m)
                Reactions.write()
            else:
                m = 'Deletion of "%s" has been canceled' % s0
    await client.send_message(message.channel, m)
예제 #3
0
async def add(client, message, splits):
    m = 'Usage: "%s add [emoji] [trigger]"' % client.user.mention
    if cantEdit(message):
        m = "You dont have permission to Edit Entries"
    elif len(splits) > 1:
        emojis = findEmojisInSplit(splits)  # find and remove the emoji
        for e in emojis:
            splits.remove(e)
        triggers = splits
        if emojis and triggers:
            for e in emojis:
                for t in triggers:
                    Reactions.add(t, e)
            m = "Added %s and %s" % (listify(emojis), listify(triggers))
            print(m)
            Reactions.write()
    await client.send_message(message.channel, m)
예제 #4
0
    # if !database_exists:
    for guild in bot.guilds:
        create_guild_tables(guild.id)
        await bot.get_cog("Reactions").read_message_history(guild,
                                                            num_messages=200)

    await bot.change_presence(activity=discord.Game(name="tdb!help | " +
                                                    github_url),
                              status=discord.Status.online)


@bot.command(name="github")
async def github_link(ctx):
    await ctx.send("GitHub link: <https://{}>".format(github_url))


if __name__ == "__main__":

    if os.path.exists("discord_bot_token.txt"):
        token_file = open("discord_bot_token.txt")
        token = token_file.readline()
    else:
        token = os.environ['BOT_TOKEN']

    database = database.Database()
    bot.add_cog(Reactions(bot))
    bot.add_cog(TetrisCog(bot))
    bot.run(token)

    database.close()
예제 #5
0
#from sqlalchemy import create_engine
#engine = create_engine('mysql://*****:*****@localhost/telegram_bot')
#engine = create_engine('sqlite://')

from base import Session, engine, Base
from reactions import Reactions

# 2 - generate database schema
Base.metadata.create_all(bind=engine)

# 3 - create a new session
session = Session()

# 4 - create test
test = Reactions(message_id=23, value="yes", user="******")
session.add(test)

session.commit()
session.close()
예제 #6
0
def button(client, callbackQuery):
    """Show new choice of buttons"""
    query = callbackQuery
    query.answer()

    #reply_markup = query.message.reply_markup
    button_no = int(query.data)
    username = getUsername(query.from_user)
    print(query.inline_message_id)
    print(query.id)
    print(query.__dict__)

    session = Session()

    #save reaction
    if button_no < 98:
        your_reactions = session.query(Reactions).filter(
            Reactions.message_id == query.inline_message_id,
            Reactions.user == username).all()
        print(your_reactions)
        if not your_reactions:
            reaction = Reactions(message_id=query.inline_message_id,
                                 value=button_no,
                                 user=username)
            session.add(reaction)
        else:
            for yr in your_reactions:
                if int(yr.value) == int(button_no):
                    session.delete(yr)
                else:
                    yr.value = button_no
                    #session.add(yr)
        session.commit()
        session.close()

    cb_buttons = createButtons()

    #read reactions
    select = session.query(Reactions.value, func.count(
        Reactions.value)).filter(
            Reactions.message_id == query.inline_message_id).group_by(
                Reactions.value).all()

    if select:
        for tup in select:
            button_number = int(tup[0])
            button_count = str(tup[1])
            cb_buttons[0][button_number] = InlineKeyboardButton(
                buttonstext[button_number] + " + " + button_count,
                callback_data=str(button_number))

    if button_no == 99:
        if 1 not in cb_buttons:
            cb_buttons.append([None])
            selectVotes = session.query(Reactions).filter(
                Reactions.message_id == query.inline_message_id).all()
            print("selectVotes")
            print(selectVotes)
            ergStr = ""
            for reaction in selectVotes:
                ergStr += str(reaction.user) + " : " + buttonstext[int(
                    reaction.value)]
            cb_buttons[1][0] = InlineKeyboardButton(ergStr, callback_data="98")
        else:
            del cb_buttons[1]

    if button_no == 98:
        if 1 in cb_buttons:
            del cb_buttons[1]

    client.edit_inline_reply_markup(query.inline_message_id,
                                    InlineKeyboardMarkup(cb_buttons))
예제 #7
0
# ---------------------------------------------------------------------
#  Initializing starts here
# ---------------------------------------------------------------------

if __name__ == host.stats().parent().guid:
    if host.settings("").guid == "client":
        raise TrassirError("Script starts on Trassir server only, not client")

    schedule_ = None
    if SCHEDULE:
        from schedule import ScheduleObject

        schedule_ = ScheduleObject(SCHEDULE)

    reactions = Reactions(schedule_)

    # -------------------------------------------------------
    # Setup channels to work
    # -------------------------------------------------------
    SELECTED_SERVER = host.settings("").guid
    assert CHANNELS_NAMES, host.tr("Необходимо выбрать каналы для работы!")
    selected_channels = get_channels(SELECTED_SERVER,
                                     parse_object_names(CHANNELS_NAMES))

    # -------------------------------------------------------
    # Play sound
    # -------------------------------------------------------
    if PLAY_SOUND:
        reactions.add_executor(PlaySound(SOUND_NAME))
예제 #8
0
from reactions import Reactions
Reactions = Reactions.read()

# List of People's IDs that can edit the bot
from os.path import exists


def read(f):
    if exists(f):
        with open(f, 'r') as fi:
            yield from fi.readlines()


EditPerms = set(i.strip() for i in read("EditPermission.ids"))
ReactPerms = set(i.strip() for i in read("DontReactChats.ids"))


def cantEdit(message):
    # If EditPerms doesnt exist anyone can edit
    # If it does exist and you're not in there ... You cant edit
    return bool(EditPerms) and message.author.id not in EditPerms


def isEmoji(x):
    return all(ord(x) > 255 for x in x)


def listify(C):
    return ", ".join('%s' % r for r in sorted(C))