Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others


0 votes
in Technique[技术] by (71.8m points)

python - Empty set when trying to create multidimmensional dictionary for discord bot on Repl.it database

I'm trying to create a simple bot for tallying requests for reviews of ticker symbols.

there's a small community of traders who make requests for the TA expert to analyze charts. This is going to run as a discord bot.

I want to create an dictionary something like

poll[ticker][user] = 1

this way it will hold an dictionary of dictionary of users keyed off the ticker symbol.

this is so ticker requests can not be duplicated and a user cannot vote for the same ticker twice.

I'm then going to save it in the key store on Repl.it

This is my second day with python although i have coded in other languages before but just not for many years.. just wanted to make sure i was on the right track.

Users will enter their votes like:

!v ZRX

Ultimately we will want to see the results like:


ZRX : MoonRaccoon, Dontcallmeskaface, TheDirtyTree
LINK : MoonRaccoon, TheDirtyTree
XRP: Dontcallmeskaface

So I figured the best data structure to support this would be something like:

poll = {'ZRX' : {'MoonRaccoon' : 1, 'Dontcallmeskaface' : 1, 'TheDirtyTree' : 1}, 'LINK' : {'MoonRaccoon' : 1, 'TheDirtyTree' : 1}, 'XRP' : {'Dontcallmeskaface'} : 1}

Does this make sense?

client = discord.Client()

def update_poll(ticker,requestor):
  vote = {ticker : {requestor : 1}}
  if "poll" in db.keys():
    db["poll"].setdefault(ticker, {})[requestor] = 1
    db["poll"] = vote

async def on_ready():
  print('We have logged in as {0.user}'.format(client))

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

  msg = message.content
  if msg.startswith("!v"):
    ticker = msg.split("!v ",1)[1]
    author = str(message.author).split("#",1)[0]
    await message.channel.send("vote for " + ticker + " by " + author + " added.")
    await message.channel.send(db["poll"])

  if msg.startswith("!clear"):
    db["poll"] = {}
    await message.channel.send("poll cleared")
    await message.channel.send(db["poll"])

I set db = {} and it doesn't seem to be adding any votes to it now.

output every time of the data structure is the same no matter who i vote for. :


vote for MATIC by MoonRaccoon added.

Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share