不久前开始编码,大约一个月左右。我目前正在为Discord编写机器人,一切正常,直到我在添加新命令后尝试运行机器人时收到此错误消息:
Traceback (most recent call last):
File "C:\Users\Jeriel\Desktop\JerryBot\run.py", line 162, in main
from musicbot import MusicBot
File "C:\Users\Jeriel\Desktop\JerryBot\musicbot\__init__.py", line 1, in <modu
le>
from .bot import MusicBot
File "C:\Users\Jeriel\Desktop\JerryBot\music\bot.py", line 2094
if __name__ == "__main__":
^
IndentationError: unexpected unindent
在我添加之后就开始了。我在这之前检查了每一行,我无法在任何地方找到一个单独的:
async def kick(message,*args):
"""Kicks the specified user from the server"""
if len(message.mentions) < 1:
return False
if message.channel.is_private:
msg = await client.send_message(message.channel,'Users cannot be kicked/banned from private channels.')
asyncio.ensure_future(message_timeout(msg, 40))
return
if not message.channel.permissions_for(message.server.get_member(client.user.id)).kick_members:
msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to kick users.')
asyncio.ensure_future(message_timeout(msg, 40))
return
members = []
if not message.channel.is_private and message.channel.permissions_for(message.author).kick_members:
for member in message.mentions:
if member != message.author:
try:
await client.kick(member)
members.append(member.name)
except:
pass
else:
msg = await client.send_message(message.channel, message.author.mention + ', You should not kick yourself from a channel, use the leave button instead.')
asyncio.ensure_future(message_timeout(msg, 40))
else:
msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to kick users, or this is a private message channel.')
asyncio.ensure_future(message_timeout(msg, 40))
msg = await client.send_message(message.channel,'Successfully kicked user(s): `{}`'.format('`, `'.join(members)))
asyncio.ensure_future(message_timeout(msg, 60))
@register('ban','@<mention users>',owner=True)
async def ban(message,*args):
"""Bans the specified user from the server"""
if len(message.mentions) < 1:
return False
if message.channel.is_private:
msg = await client.send_message(message.channel,'Users cannot be kicked/banned from private channels.')
asyncio.ensure_future(message_timeout(msg, 40))
return
if not message.channel.permissions_for(message.server.get_member(client.user.id)).ban_members:
msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to ban users.')
asyncio.ensure_future(message_timeout(msg, 40))
return
members = []
if message.channel.permissions_for(message.author).ban_members:
for member in message.mentions:
if member != message.author:
try:
await client.ban(member)
members.append(member.name)
except:
pass
else:
msg = await client.send_message(message.channel, message.author.mention + ', You should not ban yourself from a channel, use the leave button instead.')
asyncio.ensure_future(message_timeout(msg, 40))
else:
msg = await client.send_message(message.channel, message.author.mention + ', I do not have permission to ban users, or this is a private message channel.')
asyncio.ensure_future(message_timeout(msg, 40))
msg = await client.send_message(message.channel,'Successfully banned user(s): `{}`'.format('`, `'.join(members)))
asyncio.ensure_future(message_timeout(msg, 30))
@register('bans',alias='bannedusers')
@register('bannedusers')
if __name__ == "__main__":
bot = JerryBot()
bot.run("---")
答案 0 :(得分:2)
你的问题在这里:
@register('bans',alias='bannedusers')
@register('bannedusers')
if __name__ == "__main__":
bot = JerryBot()
bot.run("---")
装饰器语法需要@
行下的函数定义,该行必须与@
处于同一缩进级别。换句话说,你不能在那里发表if
声明。你需要一个函数定义。缩进只是它抓住的第一个问题;如果您缩进if
语句,则会出现其他错误。
我无法判断您是否在此处包含错误的@
行,或者您是否省略了要置于此处的函数定义。编写函数或删除@
行,以符合您的目的。
答案 1 :(得分:0)
你有可能混合标签和空格吗?对于初学者来说,这是一个常见的错误。从技术上讲,您可以使用其中任何一种,但不能混用它们。我的解决方案(我使用vim作为IDE)是将tab键设置为我的.vimrc中实际为4个空格。
您可以通过输入gg=G
并在命令模式下按Enter键来尝试在vim中重新设置。