同一条消息出现在不同的命令中

时间:2018-11-29 17:15:13

标签: python discord.py

因此,我在使用这个discord bot时遇到了问题,其中相同的消息出现在两个不同的命令中。这是代码。在这种情况下,问题出在代码底部的句子“您已经是管理员”。同样的句子出现在命令!ping中,位于代码的顶部。如果有人可以帮助我,我将不胜感激,因为我是编程的新手。

谢谢!

@client.event
async def on_message(message):
    if message.content.upper().startswith('!PING'):
        userID = message.author.id
        await client.send_message(message.channel, "<@%s> Pong!" % (userID))
    if message.content.upper().startswith('!SAY'):
        if message.author.id == "218895918416658432":
            args = message.content.split(" ")
            await client.send_message(message.channel, "%s" % (" 
    ".join(args[1:])))
    else:
        await client.send_message(message.channel, "Insufficient 
permissions!")
    if message.content.upper().startswith('!AMIADMIN'):
        if "517064875134943262" in [role.id for role in message.author.roles]:
            await client.send_message(message.channel, "You are an admin!")
        else:
            await client.send_message(message.channel, "You are not an admin")
    contents = message.content.split(" ")
    for word in contents:
        if word.upper() in chat_filter:
            if not message.author.id in bypass_list:
                try:
                    await client.delete_message(message)
                    await client.send_message(message.channel, "**Hey!** You 
cannot use this word!")
                except discord.errors.NotFound:
                    return

    if message.author == client.user:
    return
    if message.content.upper().startswith('!ROLE ADMIN'):
        role = get(message.server.roles, id="517064875134943262")
        userID = message.author.id
    if not "343249790047485952" in [role.id for role in 
message.author.roles]:
        return
    elif "517064875134943262" in [role.id for role in message.author.roles]:
        await client.send_message(message.channel, 'You are already an admin 
    <@%s>' % (userID))
    else:
        await client.add_roles(message.author, role)
        await client.send_message(message.channel, 'Admin given to <@%s>' % 
    (userID))

1 个答案:

答案 0 :(得分:1)

问题出在底部的“ if”语句中。在您的代码此处:

if message.content.upper().startswith('!ROLE ADMIN'):
    role = get(message.server.roles, id="517064875134943262")
    userID = message.author.id
if not "343249790047485952" in [role.id for role in 
message.author.roles]:
    return
elif "517064875134943262" in [role.id for role in message.author.roles]:
    await client.send_message(message.channel, 'You are already an admin 
    <@%s>' % (userID))
else:
    await client.add_roles(message.author, role)
    await client.send_message(message.channel, 'Admin given to <@%s>' % 
    (userID))

问题在于,通过您的漫游器发出的每条消息都将检查用户的角色,并通过出现的任何消息返回一条消息。因此,将最后一个“ if”,“ elif”和“ else”语句移到主!ROLE ADMIN“ if”语句下,如下所示:

if message.content.upper().startswith('!ROLE ADMIN'):
    role = get(message.server.roles, id="517064875134943262")
    userID = message.author.id
    if not "343249790047485952" in [role.id for role in 
message.author.roles]:
        return
    elif "517064875134943262" in [role.id for role in message.author.roles]:
        await client.send_message(message.channel, 'You are already an admin 
    <@%s>' % (userID))
    else:
        await client.add_roles(message.author, role)
        await client.send_message(message.channel, 'Admin given to <@%s>' % 
    (userID))