示例#1
0
async def kickFunc(message, splitcontent):
    marketID = database.getMarket(message.channel.id)
    if marketID == None:
        await message.channel.send("Market not yet set")
        return
    if not database.isAdmin(marketID, message.author.id):
        await message.channel.send("User is not an admin of this market")
        return
    if len(splitcontent) < 3:
        await message.channel.send("No user supplied")
        return
    memberID = None
    if len(message.mentions) != 0:
        memberID = message.mentions[0].id
    else:
        try:
            memberID = client.get_user(eval(splitcontent[2])).id
        except:
            await message.channel.send("Invalid user id")
            return
    if database.isAdmin(marketID, memberID):
        await message.channel.send("Cannot remove an admin")
        return
    if database.removeMember(marketID, memberID):
        await message.channel.send("User removed")
    else:
        await message.channel.send("User not a member")
示例#2
0
async def demoteFunc(message, splitcontent):
    marketID = database.getMarket(message.channel.id)
    if marketID == None:
        await message.channel.send("Market not yet set")
        return
    if not database.isOwner(marketID, message.author.id):
        await message.channel.send("User is not the owner of this market")
        return
    if len(splitcontent) < 3:
        await message.channel.send("No user supplied")
        return
    memberID = None
    if len(message.mentions) != 0:
        memberID = message.mentions[0].id
    else:
        try:
            memberID = client.get_user(eval(splitcontent[2])).id
        except:
            await message.channel.send("Invalid user id")
            return
    if memberID == message.author.id:
        await message.channel.send("Cannot demote self")
        return
    database.updateMember(marketID, memberID, isadmin=False)
    await message.channel.send("User demoted")
示例#3
0
async def setpublicityFunc(message, splitcontent):
    marketID = database.getMarket(message.channel.id)
    if marketID == None:
        await message.channel.send("Market not yet set")
        return
    if not database.isOwner(marketID, message.author.id):
        await message.channel.send("User is not the owner of this market")
        return
    if len(splitcontent) < 3:
        await message.channel.send("No publicity supplied")
        return
    publicitydict = {'public': True, 'private': False}
    publicity = splitcontent[2].lower()
    if publicity not in publicitydict:
        await message.channel.send("Invalid publicity")
        return
    database.changePublic(marketID, publicitydict[publicity])
    await message.channel.send(f"Market set to {publicity}")
示例#4
0
async def listFunc(message, splitcontent):
    marketID = database.getMarket(message.channel.id)
    if marketID == None:
        await message.channel.send("Market not yet set")
        return
    if database.isPublic(marketID):
        if not database.isMember(marketID, message.author.id):
            database.updateMember(marketID, message.author.id)
    elif not database.isMember(marketID, message.author.id):
        await message.channel.send("User is not a member of this market")
        return
    data = {'price:': [], 'name:': [], 'notes:': []}
    curr = ""
    for i in range(2, len(splitcontent)):
        if splitcontent[i] in data:
            curr = splitcontent[i]
        elif curr != "":
            data[curr].append(splitcontent[i])

    #Compliance checks
    if len(data['price:']) == 0:
        await message.channel.send("No price given")
        return
    suffix = ''
    data['price:'] = data['price:'][0]
    if data['price:'][-1].isalpha():
        for i in range(len(data['price:']) - 1, -1, -1):
            if data['price:'][i].isnumeric():
                suffix = data['price:'][i + 1:]
                data['price:'] = data['price:'][:i + 1]
                break
    suffix = suffix.lower()
    if suffix not in ABBREVIATION_DICT:
        await message.channel.send("Invalid suffix")
        return
    try:
        data['price:'] = eval(data['price:'])
    except:
        await message.channel.send("Invalid price")
        return
    if data['price:'] < 0:
        await message.channel.send("Cannot list a negative price")
        return
    data['price:'] *= ABBREVIATION_DICT[suffix]
    data['price:'] = roundSig(data['price:'])[0]
    if data['price:'] > 9.999e99:
        await message.channel.send("Price too large")
        return

    data['notes:'] = ' '.join(data['notes:'])
    if len(data['notes:']) > 300:
        await message.channel.send("Notes too long")
        return

    data['name:'] = ' '.join(data['name:'])
    if len(data['name:']) == 0:
        await message.channel.send("No name given")
        return
    if len(data['name:']) > 64:
        await message.channel.send("Name too long")
        return

    if database.addListing(marketID, message.author.id, data['name:'],
                           data['price:'], data['notes:']):
        embed = {
            "color":
            7855479,
            "author": {
                "name": "Listing Created",
                "icon_url": str(client.user.avatar_url)
            },
            "fields": [{
                'name':
                f"{data['name:']} - {shortenPrice(data['price:'])}",
                'value':
                data['notes:'] if len(data['notes:']) != 0 else "No notes"
            }]
        }
        await message.channel.send(embed=discord.Embed.from_dict(embed))
    else:
        await message.channel.send(
            "Listing failed to be added, maybe you have too many listings?")
示例#5
0
async def searchFunc(message, splitcontent):
    marketID = database.getMarket(message.channel.id)
    if marketID == None:
        await message.channel.send("Market not yet set")
        return
    if len(splitcontent) < 3:
        await message.channel.send("No query supplied")
        return
    query = ' '.join(splitcontent[2:])
    listings = database.search(marketID, query)
    if len(listings) == 0:
        await message.channel.send("No listings found")
        return
    embed = {
        "color": 7855479,
        "footer": {
            "text":
            "Press ❗ to toggle notification mode, then click on a number to notify the seller"
        },
        "author": {
            "name": "Search Results",
            "icon_url": str(client.user.avatar_url)
        },
        "fields": []
    }

    offset = 0
    reactions = [
        '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟',
        '◀️', '▶️', '❗'
    ]
    notifiedset = set()

    def setupListings(embed, listings):
        embed["fields"] = []
        emotectr = 0
        for i in listings[offset:offset + 10]:
            if i[4] >= 1000 or i[4] % 1 != 0:
                shortenedPrice = shortenPrice(float(i[4]))
            else:
                shortenedPrice = shortenPrice(int(i[4]))
            notes = i[5]
            if len(notes) == 0:
                notes = "No notes"
            embed["fields"].append({
                "name":
                f"{'❗' if offset + emotectr in notifiedset else reactions[emotectr]} {i[3]} - {str(client.get_user(i[2]))} - {shortenedPrice}",
                "value": notes
            })
            emotectr += 1

    setupListings(embed, listings)
    sentMsg = await message.channel.send(embed=discord.Embed.from_dict(embed))
    for i in range(min(10, len(listings))):
        await sentMsg.add_reaction(reactions[i])
    for i in range(10, 13):
        await sentMsg.add_reaction(reactions[i])

    def check(reaction, user):
        return reaction.message.id == sentMsg.id and user == message.author and str(
            reaction.emoji) in reactions

    waitForReaction = True
    notifymode = False

    while waitForReaction:
        try:
            done, pending = await asyncio.wait(
                [
                    client.wait_for('reaction_add', check=check),
                    client.wait_for('reaction_remove', check=check)
                ],
                return_when=asyncio.FIRST_COMPLETED,
                timeout=30,
            )
            #Cancel other task
            gather = asyncio.gather(*pending)
            gather.cancel()
            try:
                await gather
            except asyncio.CancelledError:
                pass
            if len(done) == 0:
                raise asyncio.TimeoutError('No change in reactions')
            reaction = done.pop().result()[0]
        except asyncio.TimeoutError:
            waitForReaction = False
            embed['color'] = 0xff6961
            await sentMsg.edit(embed=discord.Embed.from_dict(embed))
        else:
            emote = str(reaction.emoji)
            match = -1
            for i in range(12, -1,
                           -1):  #Search for matching emote in emote list
                if reactions[i] == emote:
                    match = i
                    break
            if match == 12:  #X selected
                notifymode = not notifymode
            elif match == 11:  #Next page
                if offset + 10 < len(listings):
                    offset += 10
                    setupListings(embed, listings)
                    await sentMsg.edit(embed=discord.Embed.from_dict(embed))
            elif match == 10:  #Previous page
                if offset - 10 >= 0:
                    offset -= 10
                    setupListings(embed, listings)
                    await sentMsg.edit(embed=discord.Embed.from_dict(embed))
            else:
                if notifymode and offset + match < len(
                        listings) and offset + match not in notifiedset:
                    notifiedset.add(offset + match)
                    setupListings(embed, listings)
                    await sentMsg.edit(embed=discord.Embed.from_dict(embed))
                    lister = client.get_user(listings[offset + match][2])
                    if lister.dm_channel == None:
                        await lister.create_dm()
                    await lister.dm_channel.send(
                        f'<@{message.author.id}> is interested in your item named "{listings[offset + match][3]}"'
                    )
示例#6
0
async def mylistingsFunc(message, splitcontent):
    marketID = database.getMarket(message.channel.id)
    if marketID == None:
        await message.channel.send("Market not yet set")
        return
    listings = database.getListings(marketID, message.author.id)
    if len(listings) == 0:
        await message.channel.send("No listings found")
        return
    embed = {
        # "title" : discord.Embed.Empty,
        # "description" : discord.Embed.Empty,
        # "url" : discord.Embed.Empty,
        "color": 7855479,
        # "timestamp" : discord.Embed.Empty,
        "footer": {
            "text":
            "Press ❌ to toggle deletion mode, then click on a number to delete the listing"
        },
        # "thumbnail" : discord.Embed.Empty,
        # "image" : discord.Embed.Empty,
        "author": {
            "name": f"{message.author.name}'s listings",
            "icon_url": str(message.author.avatar_url)
        },
        "fields": []
    }

    offset = 0
    reactions = [
        '1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟',
        '◀️', '▶️', '❌'
    ]

    def setupListings(embed, listings):
        embed["fields"] = []
        emotectr = 0
        for i in listings[offset:offset + 10]:
            if i[4] >= 1000 or i[4] % 1 != 0:
                shortenedPrice = shortenPrice(float(i[4]))
            else:
                shortenedPrice = shortenPrice(int(i[4]))
            notes = i[5]
            if len(notes) == 0:
                notes = "No notes"
            embed["fields"].append({
                "name": f"{reactions[emotectr]} {i[3]} - {shortenedPrice}",
                "value": notes
            })
            emotectr += 1

    setupListings(embed, listings)
    sentMsg = await message.channel.send(embed=discord.Embed.from_dict(embed))

    for i in range(min(10, len(listings))):
        await sentMsg.add_reaction(reactions[i])
    for i in range(10, 13):
        await sentMsg.add_reaction(reactions[i])

    def check(reaction, user):
        return reaction.message.id == sentMsg.id and user == message.author and str(
            reaction.emoji) in reactions

    waitForReaction = True
    removemode = False

    while waitForReaction:
        try:
            done, pending = await asyncio.wait(
                [
                    client.wait_for('reaction_add', check=check),
                    client.wait_for('reaction_remove', check=check)
                ],
                return_when=asyncio.FIRST_COMPLETED,
                timeout=30,
            )
            #Cancel other task
            gather = asyncio.gather(*pending)
            gather.cancel()
            try:
                await gather
            except asyncio.CancelledError:
                pass
            if len(done) == 0:
                raise asyncio.TimeoutError('No change in reactions')
            reaction = done.pop().result()[0]
        except asyncio.TimeoutError:
            waitForReaction = False
            embed['color'] = 0xff6961
            await sentMsg.edit(embed=discord.Embed.from_dict(embed))
        else:
            emote = str(reaction.emoji)
            match = -1
            for i in range(12, -1,
                           -1):  #Search for matching emote in emote list
                if reactions[i] == emote:
                    match = i
                    break
            if match == 12:  #X selected
                removemode = not removemode
            elif match == 11:  #Next page
                if offset + 10 < len(listings):
                    offset += 10
                    setupListings(embed, listings)
                    await sentMsg.edit(embed=discord.Embed.from_dict(embed))
            elif match == 10:  #Previous page
                if offset - 10 >= 0:
                    offset -= 10
                    setupListings(embed, listings)
                    await sentMsg.edit(embed=discord.Embed.from_dict(embed))
            else:
                if removemode and offset + match < len(listings):
                    database.removeListing(listings[offset + match][0])
                    del listings[offset + match]
                    if len(listings) != 0 and not (offset < len(listings)):
                        offset -= 10
                    setupListings(embed, listings)
                    await sentMsg.edit(embed=discord.Embed.from_dict(embed))