0

I am struggling on something I am working on with my custom discord bot for someone. I have made a series of events that go like the picture here: Link to Example

I want to be able to store this in a JSON file or database without sending it straight away so later I can do things like:

  • *event list - lists all text events/announcements Like this
  • ;event preview 1 - Example here With the '1' I want it to link to an event name in the json file so I can do this command so whatever event name is linked to whatever number. this is to see the preview of the announcement you made. it sends a period, edits that period to whatever is the event/announcement selected. this is so that if the announcement has an everyone or here ping, it wont ping when you're previewing the announcement
  • ;event post 1 (or whatever number relates to the right event I want to post) Example here - this command posts that particular event

I can not figure out how to store it in the Json file or database and be able to do those commands above. I have had little help from other devs but I still cant figure it out and need help on how to do it. Below is my code so far if someone can please help me:

    @bot.command()
    async def event(ctx):
        def check(msg):
            return msg.channel.id == ctx.channel.id and msg.author.id == ctx.author.id
        await ctx.send("Please enter the event name:")
        eventname = await bot.wait_for("message", check=check)
        await ctx.send("Please send what you want to say in the announcement in a code block:")
        codeblock = await bot.wait_for("message", check=check)
        await ctx.send("What image should be posted? (Type 'no' for none)")
        image = await bot.wait_for("message", check=check)
        mainimage = None
        if image.content.lower() != "no":
            mainimage = image.attachments[0]
        await ctx.send('What channel should the announcement be made in? (Please tag the channel)')
        eventch = await bot.wait_for("message", check=check)
        realchannel = await commands.TextChannelConverter().convert(ctx, eventch.content)
        message = codeblock.content.replace("```", "").replace("`", "")
        if mainimage:
            return await realchannel.send(message, file=await mainimage.to_file())
        await realchannel.send(message)

(Sorry if not indented properly, nor sure how to on here)

Help is appreciated.

Kevin Sheng
  • 357
  • 1
  • 3
  • 7
  • Welcome to StackOverflow! What have you tried so far to save it as a json file? – xjcl Nov 13 '20 at 20:03
  • Well I tried doing it as a list but then someone told me to do it as a dict. I just really need this done urgently if I am honest and need someone to tell me how to do it. If you want we can talk over discord which would be much preferred for me and thanks for welcoming me to Stack Overflow :) Discord Username: Jakey#5897 – Jakes World Nov 13 '20 at 20:43
  • JSON can be composed both of lists and dicts -- just call `json.dump` to save it and `json.load` later to restore it – xjcl Nov 13 '20 at 20:47
  • someone in the discord.py server said something about a dict I needed to have in json but whats the difference? cos when trying to pull certain data it kept saying key error. im hella confused – Jakes World Nov 13 '20 at 22:18
  • Hello any help at all? – Jakes World Nov 15 '20 at 10:53
  • You should try it on your own and post a new question probably with what you tried – xjcl Nov 15 '20 at 11:46

1 Answers1

0

It kinda take too long to code so Im here to suggest a few steps to make it easy coding!

'

 dict = {}
 dict['name'] = ctx.message.content
 #same for all
  • after storing all kind of info in the dict. you can upload the dict to the database

  • Now the data are stored in Database! if you learned PyMongo you can easily do the rest of your queries!

Manoj A
  • 334
  • 3
  • 10