Home > Back-end >  the code works no matter which text I type
the code works no matter which text I type

Time:01-03

Wow... Hello, I'm back here after two days. (Thank you to the person who helped me last time..) First of all, this code works in the direction I want. However, there is a problem.

This bot's command is "!공지". However, this bot sends me a message because the code works no matter what chat I write... I want to make it work only when I write "!공지"

    @client.event
async def on_message(message):
    if message.author == client.user: 
        return
    if message.content.startswith("!공지"):
        await message.channel.purge(limit=1)
    i = (message.author.guild_permissions.send_messages)
    if i is True:
            notice = message.content[4:]
            channel = client.get_channel(927233433489526904)
            embed = discord.Embed(title="*브리핑*", description="\n――――――――――――――――――――――――――――\n\n{}\n\n――――――――――――――――――――――――――――".format(notice), color=0x00ff00)
            embed.set_footer(text="-C0de")
            embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
            await channel.send (embed=embed)
 
    if i is False:
            await message.channel.send("{}, 당신은 관리자가 아닙니다".format(message.author.mention))

CodePudding user response:

The problem lies in this:

i = (message.author.guild_permissions.send_messages)
if i is True:

Since anyone who writes something has that permission, i will always be true.

I guess what you want to do is to indent those two lines, so it is inside the if-block that checks the command.

CodePudding user response:

If you want the code to exit if the user's message doesn't start with "!공지" then add

if message.content.startswith("!공지"):
    await message.channel.purge(limit=1)
else:
    return

This will exit the function if the message does not start with those characters.

CodePudding user response:

Try moving your i = (message.author.guild_permissions.send_messages) line to the same indentation as the if conditional, while also initializing your i before the if-else statement:

i = False
if message.content.startswith("!공지"):
    await message.channel.purge(limit=1)
    i = (message.author.guild_permissions.send_messages) # set only in condition.
else:
    i = False

When inserted in code:

    @client.event
async def on_message(message):
    if message.author == client.user: 
        return
    i = False # initialize i
    if message.content.startswith("!공지"):
        await message.channel.purge(limit=1)
        i = (message.author.guild_permissions.send_messages) # set only in condition.
    else:
        i = False
    if i is True:
        notice = message.content[4:]
        channel = client.get_channel(927233433489526904)
        embed = discord.Embed(title="*브리핑*", description="\n――――――――――――――――――――――――――――\n\n{}\n\n――――――――――――――――――――――――――――".format(notice), color=0x00ff00)
        embed.set_footer(text="-C0de")
        embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
        await channel.send (embed=embed)

    if i is False:
        await message.channel.send("{}, 당신은 관리자가 아닙니다".format(message.author.mention))

This should let your code properly branch based on the command

  • Related