我试图有一个单一的消息与多个动作行,每个有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
是由encoder.py在遍历discord_componenets库后抛出的。
完全错误:
Traceback (most recent call last):
File "C:UsersAAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordclient.py", line 343, in _run_event
await coro(*args, **kwargs)
File "S:Documentstest.py", line 179, in on_message
category_message = await message.channel.send(content="The week", components=actionRowOfRows)
File "C:UsersAAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscord_componentsclient.py", line 48, in send_component_msg_prop
return await self.send_component_msg(ctxorchannel, *args, **kwargs)
File "C:UsersAAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscord_componentsclient.py", line 177, in send_component_msg
data = await self.bot.http.request(
File "C:UsersAAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordhttp.py", line 156, in request
kwargs['data'] = utils.to_json(kwargs.pop('json'))
File "C:UsersAAppDataLocalPackagesPythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0LocalCachelocal-packagesPython39site-packagesdiscordutils.py", line 328, in to_json
return json.dumps(obj, separators=(',', ':'), ensure_ascii=True)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0libjson__init__.py", line 234, in dumps
return cls(
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0libjsonencoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0libjsonencoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.9_3.9.1776.0_x64__qbz5n2kfra8p0libjsonencoder.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)