Discord bot命令列表

时间:2017-12-25 02:44:12

标签: python-3.x discord

我有一个discord bot的命令列表,所以我可以稍后更改或修改它们。当有人在discord中写命令时,我正在尝试检查它是否在命令列表中。问题是我得到了错误:

    for message.content.startswith in commands:
AttributeError: 'str' object attribute 'startswith' is read-only

有办法做到这一点吗?我怎么能让它不是只读的......或者我该如何解决这个问题?

代码:

import discord, asyncio

client = discord.Client()

@client.event
async def on_ready():
    print('logged in as: ', client.user.name, ' - ', client.user.id)

@client.event
async def on_message(message):
    commands = ('!test', '!test1', '!test2')

    for message.content.startswith in commands:
        print('true')

if __name__ == '__main__':
    client.run('token')

1 个答案:

答案 0 :(得分:4)

这部分是问题:

for message.content.startswith in commands:
    print('true')

这没有任何意义。我假设message.content是一个字符串。 startswith是一个字符串方法,但需要一个参数see here。您需要传递startswith您要搜索的实际字符。例如,"hello".startswith("he")将返回true。我相信这就是你想要的:

for command in commands:
    if message.content.startswith(command):
        print('true')
相关问题