Reputation: 11
I am looking for a simple solution that can be used here: This is my code.
import discord
import json
from discord.ext import commands
print("Starting bot...")
TOKEN = ''
client = commands.Bot(command_prefix = '.')
@client.event
async def on_message(message):
embeds = message.embeds
for embed in embeds:
emb = str(embed.to_dict())
if 'owned' in emb:
owned = object('title')
print(owned)
print("Bot is ready!")
client.run(TOKEN)
(I do have a discord bot token in place I removed it to show y'all) Anyways my goal is to be able to take emb, which comes out of the script looking like this:
{'footer': {'text': 'Item Type: Sellable'}, 'thumbnail': {'width': 75, 'url': 'https://cdn.discordapp.com/emojis/714980893999104051.png', 'proxy_url': 'https://images-ext-1.discordapp.net/external/c4teYBjoGAhxnygEZ5F2GlHWzReCIg_xEOX1PPtZdIQ/https/cdn.discordapp.com/emojis/714980893999104051.png', 'height': 75}, 'color': 15684432, 'type': 'rich', 'description': "This item's purpose is to be collected or sold. Nothing more, nothing less.\n\n**BUY** - Not able to be purchased\n**SELL** - ⏣ 1,680 (multiplier included)\n**TRADE** - 2k - 7k", 'title': '**Common Fish** (697 owned)'}
and to be able to take only the last part, the 697. My major problem here is that this will sometimes be 500, sometimes only be 2 but I would like to be able to extract that and use it as a variable. Keep in mind that the length of the lines will vary. Thanks in advance!
Upvotes: 1
Views: 41
Reputation: 59
I don't have enough reputation to comment so I have to write this in answer. This is very easy actually you just need basic python string manipulation skills for this
let's assume, your dictionary/json is assigned to a variable x
x = {'footer': {'text': 'Item Type: Sellable'}, 'thumbnail': {'width': 75, 'url': 'https://cdn.discordapp.com/emojis/714980893999104051.png', 'proxy_url': 'https://images-ext-1.discordapp.net/external/c4teYBjoGAhxnygEZ5F2GlHWzReCIg_xEOX1PPtZdIQ/https/cdn.discordapp.com/emojis/714980893999104051.png', 'height': 75}, 'color': 15684432, 'type': 'rich', 'description': "This item's purpose is to be collected or sold. Nothing more, nothing less.\n\n**BUY** - Not able to be purchased\n**SELL** - ⏣ 1,680 (multiplier included)\n**TRADE** - 2k - 7k", 'title': '**Common Fish** (697 owned)'}
We can get the title by x['title']
First step is splitting at first opening bracket (
which would make it x['title'].split('(')
which will give us an output as list ['**Common Fish**', '697 owned)']
then we will index 1, because we need the second item in this list (because the number you need is in there)
which would make it x['title'].split('(')[1]
now we further split it, but this time we provide no arguments to .split()
which will just split on spaced characters.
which would make it x['title'].split('(')[0].split()
with that, you will again get a list like this:
['your desired number', 'owned)']
Then all there is left is to index this list to 0!
which would make it x['title'].split('(')[1].split()[0]
which will give us your number (697 in this case)
Works with any x
number
x = {'footer': {'text': 'Item Type: Sellable'}, 'thumbnail': {'width': 75, 'url': 'https://cdn.discordapp.com/emojis/714980893999104051.png', 'proxy_url': 'https://images-ext-1.discordapp.net/external/c4teYBjoGAhxnygEZ5F2GlHWzReCIg_xEOX1PPtZdIQ/https/cdn.discordapp.com/emojis/714980893999104051.png', 'height': 75}, 'color': 15684432, 'type': 'rich', 'description': "This item's purpose is to be collected or sold. Nothing more, nothing less.\n\n**BUY** - Not able to be purchased\n**SELL** - ⏣ 1,680 (multiplier included)\n**TRADE** - 2k - 7k", 'title': '**Common Fish** (697 owned)'}
my_num = x['title'].split('(')[1].split()[0]
Upvotes: 1