【发布时间】: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