discord - discord ActionRow 不是 JSON 可序列化的
问题描述
我试图让一条消息包含多个操作行,每行包含 5 个或更少的按钮。通过查看文档,这似乎是正确的,但显然不是。
我在那里有另一种方式的按钮列表也不起作用,所以我知道它在这里没用。
# Sends a message with all the days of the week in
await message.channel.send("Days of the week.")
# Message per category
actionRowOfRows = ActionRow()
buttonRow = []
fill = 0
for day in ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]: # For role in category
if fill < 5:
buttonRow.append(Button(style=ButtonStyle.blue, custom_id=day,label=day)) # New button in existing buttonRow
fill += 1
else:
actionRowOfRows.append(ActionRow(buttonRow)) # Adds row to the row of rows
buttonRow = []
buttonRow.append(Button(style=ButtonStyle.blue, custom_id=day,label=day)) # New button in the blank buttonRow
fill=0
# Add button to message per day
category_message = await message.channel.send(content="The week", components=actionRowOfRows)
TypeError: Object of type ActionRow is not JSON serializable
在通过 discord_componenets 库后由 encoder.py 抛出。
完全错误:
Traceback (most recent call last):
File "C:\Users\A\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "S:\Documents\test.py", line 179, in on_message
category_message = await message.channel.send(content="The week", components=actionRowOfRows)
File "C:\Users\A\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord_components\client.py", line 48, in send_component_msg_prop
return await self.send_component_msg(ctxorchannel, *args, **kwargs)
File "C:\Users\A\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord_components\client.py", line 177, in send_component_msg
data = await self.bot.http.request(
File "C:\Users\A\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\http.py", line 156, in request
kwargs['data'] = utils.to_json(kwargs.pop('json'))
File "C:\Users\A\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\discord\utils.py", line 328, in to_json
return json.dumps(obj, separators=(',', ':'), ensure_ascii=True)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\__init__.py", line 234, in dumps
return cls(
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ActionRow is not JSON serializable```
解决方案
我遇到了同样的困难,我需要在每行上放 5 个按钮。我不知道您的代码到底有什么问题,但我想发布我的解决方案。
await message.channel.send("Days of the week.")
# Message per category
buttons = [[]]
for day in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
"Sunday"]: # For role in category
if len(buttons[-1]) > 4:
buttons.append([])
buttons[-1].append(Button(style=ButtonStyle.blue, custom_id=day, label=day)) # New button in the blank buttonRow
# Add button to message per day
category_message = await message.channel.send(content="The week", components=buttons)
推荐阅读
- jquery - jQuery Css 速度实现
- html - 想让 Nav-Item 与 Nav-Bar 高度相同
- flutter - Flutter: Card() / Firebase - (初学者问题)
- ios - iOS 不会在一台设备上请求 AASA 文件,但适用于所有其他设备
- sql - 如何在选择中创建while循环?
- python - 在 Django 中获取变量 OneToOneField
- php - 在laravel路由中使用参数调用自定义类方法
- r - R:合并行以导出到excel
- java - PANIC:损坏的 AVD 系统路径。检查您的 ANDROID_SDK_ROOT
- html - 高密度显示器上笔画宽度中的 REM 单位