Запретить боту отправлять несколько сообщений для message.mentions

#python #python-3.x #discord #discord.py

#python #python-3.x #Discord #discord.py

Вопрос:

С помощью этого бота пользователи могут грабить друг друга за деньги. Проблема, с которой я сталкиваюсь, заключается в том, что если пользователь помечает нескольких пользователей при отправке одного сообщения, бот ответит на каждого отмеченного человека. Это позволяет пользователям обойти if get_robbery(message.author) >= 20: проверку, если они отметили более одного пользователя. Есть ли способ убедиться, что бот отвечает только первому помеченному пользователю, а не каждому помеченному пользователю?

Редактировать: код изменен, чтобы отразить изменения. Изменен for user in message.content на user = message.mentions[0] , и он работает безупречно.

 if message.content.startswith('!rob'):
    if message.author.id in blacklist:
        await client.send_message(message.channel, "You are blacklisted for breaking the rules {}".format(message.author.mention))       
    else:
        if get_robbery(message.author) >= 20:
            await client.send_message(message.channel, "You can only rob 20 times every 30 minutes {}".format(message.author.mention))
        else:
            if message.author == user:
                await client.send_message(message.channel, "{} you can't rob yourself dummy".format(message.author.mention))
            else:
                if get_dollars(message.author) < 0:
                    await client.send_message(message.channel, "{} you can't even afford a gun <:papi:523340666269335552>".format(message.author.mention))
                else:   
                    user = message.mentions[0]
                    if (get_dollars(user)) < 1:
                        add_robbery(message.author, 1)
                        await client.send_message(message.channel, "{} is too broke to rob 🤣".format(user.mention))
                    else:
                        user = message.mentions[0]
                        if (get_city(user)) != get_city(message.author):
                            await client.send_message(message.channel, "{} you can't rob {} because they are in **{}** ".format(message.author.mention, user.mention, get_city(user)))   
                        else:
                            if steal >= 3:
                                if get_dollars(message.author) < 20000 and (get_dollars(user)) < 20000:
                                    add_robbery(message.author, 1)
                                    remove_dollars(user, stealdollars)
                                    add_dollars(message.author, stealdollars)
                                    add_dollars_stolen(message.author, stealdollars)
                                    print("{} catches {} slippin and sticks them up for ${} 😨<:BangBang:523383990825123840>".format(message.author.mention, user.mention, stealdollars))
                                    await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} 😨<:BangBang:523383990825123840>".format(message.author.mention, user.mention, stealdollars))
                                else:
                                    if get_dollars(message.author) >= 20000 and (get_dollars(user)) < 20000:
                                        add_robbery(message.author, 1)
                                        remove_dollars(user, stealdollars)
                                        add_dollars(message.author, stealdollars)
                                        add_dollars_stolen(message.author, stealdollars)
                                        print("{} catches {} slippin and sticks them up for ${} 😨<:BangBang:523383990825123840>".format(message.author.mention, user.mention, stealdollars))
                                        await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} 😨<:BangBang:523383990825123840>".format(message.author.mention, user.mention, stealdollars))
                                    else:
                                        if get_dollars(message.author) < 20000 and (get_dollars(user)) >= 20000:
                                            add_robbery(message.author, 1)
                                            remove_dollars(user, stealdollars)
                                            add_dollars(message.author, stealdollars)
                                            add_dollars_stolen(message.author, stealdollars)
                                            print("{} catches {} slippin and sticks them up for ${} 😨<:BangBang:523383990825123840>".format(message.author.mention, user.mention, stealdollars))
                                            await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} 😨<:BangBang:523383990825123840>".format(message.author.mention, user.mention, stealdollars))
                                        else: 
                                            if get_dollars(message.author) >= 20000 and (get_dollars(user)) >= 20000:
                                                add_robbery(message.author, 1)
                                                remove_dollars(user, stealdollarsx3)
                                                add_dollars(message.author, stealdollarsx3)
                                                add_dollars_stolen(message.author, stealdollarsx3)
                                                print("{} catches {} slippin and sticks them up for ${} 😨<:BangBang:523383990825123840>".format(message.author.mention, user.mention, stealdollarsx3))
                                                await client.send_message(message.channel, "{} catches {} slippin and sticks them up for ${} 😨<:BangBang:523383990825123840>".format(message.author.mention, user.mention, stealdollarsx3))
                            elif steal == 2:
                                if get_dollars(message.author) < 20000 and (get_dollars(user)) < 20000:
                                    add_robbery(message.author, 1)
                                    remove_dollars(message.author, stealrdollars)
                                    print("{} gets arrested trying to rob {} and has to post ${} bail 👮🚨".format(message.author.mention, user.mention, stealrdollars))
                                    await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post ${} bail 👮🚨".format(message.author.mention, user.mention, stealrdollars))

                                else:
                                    if get_dollars(message.author) >= 20000 and (get_dollars(user)) < 20000:
                                        add_robbery(message.author, 1)
                                        remove_dollars(message.author, stealrdollars)
                                        print("{} gets arrested trying to rob {} and has to post ${} bail 👮🚨".format(message.author.mention, user.mention, stealrdollars))
                                        await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post ${} bail 👮🚨".format(message.author.mention, user.mention, stealrdollars))

                                    else:
                                        if get_dollars(message.author) < 20000 and (get_dollars(user)) >= 20000:
                                            add_robbery(message.author, 1)
                                            remove_dollars(message.author, stealrdollars)
                                            print("{} gets arrested trying to rob {} and has to post ${} bail 👮🚨".format(message.author.mention, user.mention, stealrdollars))
                                            await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post ${} bail 👮🚨".format(message.author.mention, user.mention, stealrdollars))

                                        else:
                                            if get_dollars(message.author) >= 20000 and (get_dollars(user)) >= 20000:
                                                add_robbery(message.author, 1)
                                                remove_dollars(message.author, stealrdollarsx2)
                                                print("{} gets arrested trying to rob {} and has to post ${} bail 👮🚨".format(message.author.mention, user.mention, stealrdollarsx2))
                                                await client.send_message(message.channel, "{} gets arrested trying to rob {} and has to post ${} bail 👮🚨".format(message.author.mention, user.mention, stealrdollarsx2))
                            elif steal == 0:
                                add_robbery(message.author, 1)
                                print("{} kicks {}'s door down but doesn't find any cash 🤬".format(message.author.mention, user.mention))
                                await client.send_message(message.channel, "{} kicks {}'s door down but doesn't find any cash 🤬".format(message.author.mention, user.mention))

                            elif steal == 1:
                                if get_dollars(message.author) < 20000 and (get_dollars(user)) < 20000:
                                    add_robbery(message.author, 1)
                                    remove_dollars(message.author, robbeddollars)
                                    add_dollars(user, robbeddollars)
                                    add_dollars_stolen(user, robbeddollars)
                                    remove_grams(message.author, robbedgrams)
                                    add_grams(user, robbedgrams)
                                    add_grams_stolen(user, robbedgrams)
                                    print("{} kicks {}'s door down but is met with {}'s glock in their face and robbed of ${} and {} grams".format(message.author.mention, user.mention, user.mention, robbeddollars, robbedgrams))
                                    await client.send_message(message.channel, "{} kicks {}'s door down but is met with {}'s glock in their face and robbed of ${} and {} grams <:BangBang:523383990825123840> <:datboi:523383990825123840>".format(message.author.mention, user.mention, user.mention, robbeddollars, robbedgrams))

                                else:
                                    if get_dollars(message.author) >= 20000 and (get_dollars(user)) < 20000:
                                        add_robbery(message.author, 1)
                                        remove_dollars(message.author, robbeddollars)
                                        add_dollars(user, robbeddollars)
                                        add_dollars_stolen(user, robbeddollars)
                                        remove_grams(message.author, robbedgrams)
                                        add_grams(user, robbedgrams)
                                        add_grams_stolen(user, robbedgrams)
                                        print("{} kicks {}'s door down but is met with {}'s glock in their face and robbed of ${} and {} grams".format(message.author.mention, user.mention, user.mention, robbeddollars, robbedgrams))
                                        await client.send_message(message.channel, "{} kicks {}'s door down but is met with {}'s glock in their face and robbed of ${} and {} grams <:BangBang:523383990825123840> <:datboi:523383990825123840>".format(message.author.mention, user.mention, user.mention, robbeddollars, robbedgrams))
                                    else:
                                        if get_dollars(message.author) < 20000 and (get_dollars(user)) >= 20000:
                                            add_robbery(message.author, 1)
                                            remove_dollars(message.author, robbeddollars)
                                            add_dollars(user, robbeddollars)
                                            add_dollars_stolen(user, robbeddollars)
                                            remove_grams(message.author, robbedgrams)
                                            add_grams(user, robbedgrams)
                                            add_grams_stolen(user, robbedgrams)
                                            print("{} kicks {}'s door down but is met with {}'s glock in their face and robbed of ${} and {} grams".format(message.author.mention, user.mention, user.mention, robbeddollars, robbedgrams))
                                            await client.send_message(message.channel, "{} kicks {}'s door down but is met with {}'s glock in their face and robbed of ${} and {} grams <:BangBang:523383990825123840> <:datboi:523383990825123840>".format(message.author.mention, user.mention, user.mention, robbeddollars, robbedgrams))
                                        else:
                                            if get_dollars(message.author) >= 20000 and (get_dollars(user)) >= 20000:
                                                add_robbery(message.author, 1)
                                                remove_dollars(message.author, robbeddollarsx2)
                                                add_dollars(user, robbeddollarsx2)
                                                add_dollars_stolen(user, robbeddollarsx2)
                                                remove_grams(message.author, robbedgrams)
                                                add_grams(user, robbedgrams)
                                                add_grams_stolen(user, robbedgrams)
                                                print("{} kicks {}'s door down but is met with {}'s glock in their face and robbed of ${} and {} grams".format(message.author.mention, user.mention, user.mention, robbeddollarsx2, robbedgrams))
                                                await client.send_message(message.channel, "{} kicks {}'s door down but is met with {}'s glock in their face and robbed of ${} and {} grams <:BangBang:523383990825123840> <:datboi:523383990825123840>".format(message.author.mention, user.mention, user.mention, robbeddollarsx2, robbedgrams))
  

Комментарии:

1. Удалите for цикл и просто сделайте user = message.mentions[0] . Вам потребуется некоторая обработка ошибок для сценария, в котором они никого не упоминают.

2. Код отредактирован с учетом вашего предложения. Работает как по волшебству, я полагаю, что я действительно выполнял цикл for, чтобы избежать ошибок в прошлом, и забыл, почему это было там, но я просто добавлю туда некоторую обработку ошибок. Еще раз спасибо.