discord.py json report system
class my_modal(ui.Modal, title="Report or Suggest"):
answer = ui.TextInput(label="Is this a report or a suggestion?", style=discord.TextStyle.short, required=True, placeholder="Report/Suggestion")
answer2 = ui.TextInput(label="What is your report/suggestion?", style=discord.TextStyle.long, required=True, placeholder="Your Text Here")
async def on_submit(self, interaction: discord.Interaction):
with open("reports.json", "r") as f:
data = json.load(f)
if self.answer.value.lower() == "report" or self.answer.value.lower() == "suggestion":
await interaction.response.send_message("Successfully submitted your report/suggestion.", ephemeral=True)
print(f"{interaction.user} sent a {self.answer}: {self.answer2}")
data["name"] = interaction.user
data["choice"] = self.answer.value.lower()
data["message"] = self.answer2.value.lower()
with open("reports.json", "w") as f:
json.dump(data, f)
else:
await interaction.response.send_message("You either didn't put a valid input in your first choice or didn't type it properly. Make sure you typed it properly and try again.", ephemeral=True)
return
@client.tree.command()
@commands.guild_only()
async def report(interaction: discord.Interaction):
await interaction.response.send_modal(my_modal())
I am trying to make a json db (I know it's not recommended but I am just using this for time being) and when a user makes a report or suggestion, it stores their username, whether it's a suggestion or report, and their actual suggestion/report but I am getting this error
Traceback (mos开发者_运维知识库t recent call last):
File "/home/container/.local/lib/python3.9/site-packages/discord/ui/modal.py", line 186, in _scheduled_task
await self.on_submit(interaction)
File "/home/container/main.py", line 311, in on_submit
json.dump(data, f)
File "/usr/local/lib/python3.9/json/__init__.py", line 179, in dump
for chunk in iterable:
File "/usr/local/lib/python3.9/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/local/lib/python3.9/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/local/lib/python3.9/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/usr/local/lib/python3.9/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Member is not JSON serializable
You're not storing their username, you're trying to store the entire discord.Member
instance:
data["name"] = interaction.user
Docs: https://discordpy.readthedocs.io/en/stable/interactions/api.html?highlight=interaction%20user#discord.Interaction.user
Python has no idea how to turn that into something and store it in a JSON file.
If you want a user's name, use User.display_name
: https://discordpy.readthedocs.io/en/stable/api.html#discord.User.display_name
Also, consider storing id's instead of names. Multiple users can have the same name, and users can change their name. This would break your whole application unnecessarily.
精彩评论