我试图编程一个不和谐的机器人,链接到谷歌页,但已经遇到了无数的错误,我的代码。我得到的错误如下:
Traceback (most recent call last):
File "/home/runner/Bundeswehr-Bot/venv/lib/python3.8/site-packages/discord/ui/modal.py", line 186, in _scheduled_task
await self.on_submit(interaction)
File "/home/runner/Bundeswehr-Bot/modals.py", line 45, in on_submit
if sheet.find(self.host.value) == None:
File "/home/runner/Bundeswehr-Bot/venv/lib/python3.8/site-packages/gspread/cell.py", line 44, in __eq__
same_row = self.row == other.row
AttributeError: 'NoneType' object has no attribute 'row'我的代码是:
class bct_modal(discord.ui.Modal, title="BCT Logging"):
host = discord.ui.TextInput(
label='Host ID',
placeholder='The hosts discord ID here...',
required=True
)
async def on_submit(self, interaction: discord.Interaction):
ids = []
ids.append(self.host.value)
print(ids)
if sheet.find(self.host.value) == None:
sheet.append_row([self.host.value, 0, 0, 0, 0, 0, 0, 0, 0])
for id in ids:
bct_host_col = sheet.find("Hosted BCT").col
cell_row = sheet.find(id).row
sheet.update_cell(cell_row, bct_host_col, int(sheet.cell(cell_row, bct_host_col).value) + 1)
elif sheet.find(self.host.value) != None:
print("This ID is already in the sheet.")任何帮助都将不胜感激。
注意:当我输入一个还没有在google电子表格中的ID时,它会按照我想要的方式完美地附加它,但是如果我尝试输入一个已经在电子表格中的ID,它会抛出这个错误。
发布于 2022-10-24 01:41:09
在您的脚本中,我认为可能需要修改if sheet.find(self.host.value) == None:。所以,==是is。我认为这可能是您发布'NoneType' object has no attribute 'row'的原因。
我认为在这种情况下,this thread可能是有用的。
那么,下面的修改如何?
发自:
if sheet.find(self.host.value) == None:至:
if sheet.find(self.host.value) is None:当我测试您的脚本时,
参考资料:
与Python None comparison: should I use "is" or ==?相关的
在这种情况下,这一点很重要,因为如果存在一个针对x == y类的__eq__方法,那么__eq__就会将x转换为x,而gscedCell类实现了一个自定义__eq__方法,该方法尝试对other对象(other.row)进行无保护的属性访问,如果other不是一个Cell (在本例中,它抛出一个错误,但传入另一个具有row属性的类可能会有不同的行为)。
虽然这是一个非常糟糕的实践,但如果您使用is作为与None进行比较的规则,则可以避免这种做法。
https://stackoverflow.com/questions/73911302
复制相似问题