【问题标题】:How should I store this and how do I do it? discord.py我应该如何存储它,我该怎么做?不和谐.py
【发布时间】:2021-02-25 16:22:39
【问题描述】:

我正在为某人使用我的自定义 Discord 机器人处理一些事情而苦苦挣扎。我做了一系列的活动,就像这里的图片: Link to Example

我希望能够将其存储在 JSON 文件或数据库中,而无需立即发送,以便稍后我可以执行以下操作:

  • *event list - 列出所有文本事件/公告Like this
  • ;event preview 1 - Example here 使用“1”,我希望它链接到 json 文件中的事件名称,这样我就可以执行此命令,以便将任何事件名称链接到任何数字。这是为了查看您发布的公告的预览。它发送一个周期,将该周期编辑为所选事件/公告的任何内容。这样如果公告有所有人或此处 ping,当您预览公告时它不会 ping
  • ;event post 1 (or whatever number relates to the right event I want to post) Example here - 此命令发布特定事件

我不知道如何将它存储在 Json 文件或数据库中,并且能够执行上面的那些命令。我从其他开发人员那里得到的帮助很少,但我仍然无法弄清楚,需要帮助。如果有人可以帮助我,以下是我的代码:

    @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)

(对不起,如果没有正确缩进,也不知道如何在这里)

感谢您的帮助。

【问题讨论】:

  • 欢迎来到 StackOverflow!到目前为止,您尝试过将其保存为 json 文件吗?
  • 好吧,我试着把它作为一个列表来做,但后来有人告诉我把它作为一个字典来做。如果我是诚实的并且需要有人告诉我该怎么做,我真的需要紧急完成这项工作。如果您愿意,我们可以讨论不和谐,这对我来说更受欢迎,感谢您欢迎我来到 Stack Overflow :) 不和谐用户名:Jakey#5897
  • JSON 可以由列表和字典组成——只需调用json.dump 保存它,然后调用json.load 来恢复它
  • discord.py 服务器中的某个人说了一些关于我需要在 json 中拥有的 dict 但有什么区别?因为当试图提取某些数据时,它一直说关键错误。我很困惑
  • 您好,有什么帮助吗?

标签: python discord bots discord.py


【解决方案1】:

编码的时间太长了,所以我在这里建议一些步骤来简化编码!

'

 dict = {}
 dict['name'] = ctx.message.content
 #same for all
  • 在字典中存储所有类型的信息之后。您可以将字典上传到数据库

  • 现在数据存储在数据库中!如果您学习了 PyMongo,您可以轻松完成其余的查询!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-16
    • 2022-11-12
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2012-02-11
    • 2012-01-05
    • 1970-01-01
    相关资源
    最近更新 更多