0

I am currently making a Discord Bot with discord.py and has YAML configs. Here is a short YAML config taken from my "hartexConfig.yaml" file:

general:
    hartexToken = 'the bot token'

then I try to access it in the hartex.py file:

class Hartex(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__()

    hartexTokenValue = open('HarTex/hartexConfig.yaml', 'r')

    hartexToken = hartexTokenValue['general']['hartexToken']

    yamlStream = True

How can I do this or am I completely wrong?

PS I meant to access a specific piece of data, like in this case, I want to read ONLY the hartexToken from the YAML file.

2 Answers2

0
import yaml

class Hartex(discord.Client):
    def __init__(self, *args, **kwargs):
        super().__init__()

    with open('HarTex/hartexConfig.yaml', 'r') as hartexConfig:
        # this is the part where your config is actually parsed
        hartexTokenValue = yaml.safe_load(hartexConfig) 

    hartexToken = hartexTokenValue['general']['hartexToken']
Błotosmętek
  • 12,717
  • 19
  • 29
0

Are you sure the yaml is correct? The = should realy be a :; as it stands, the key will be general and its value hartexToken = 'the bot token':

(you need to install pyyaml)

pip install pyyaml


>>> import yaml
>>> with open('demo.yaml','r') as f:
...    datamap = yaml.safe_load(f)
... 
>>> datamap
{'general': "hartexToken = 'the bot token'"}

If the yaml is indeed as you say it is, you can of course split the value to get to 'the bot token'.

>>> datamap['general'].split('=')[1]
" 'the bot token'"
Bart Barnard
  • 1,128
  • 8
  • 17