Search code examples
discorddiscord.pybots

My discord bot doesn't reply to my message


import discord
client = discord.Client(intents=discord.Intents.default())
TOKEN = "MY TOKEN HERE"

@client.event
async def on_ready():
    print(f"{client.user.name} has logged in!")

async def on_message(message):
    if message.content == "안녕?":
        await message.send("안녕!", reference=message)

client.run(TOKEN)

This is my code. And I want my discord bot to reply to my message, but it does not reply to my message. What's wrong with my code?

Please fix this please. Im working on this problem for 2 days. And I can't find the answer.

ps.i'm using Macbook. (is there any difference between making a discord bot on Mac or Windows?)


Solution

  • First of all, where you defined client, you need to give it a prefix, which is the special character you can mention it with. So like this:

    client = commands.Bot(command_prefix="!",intents=discord.Intents.all())
    

    Then you get the on_ready event right. Moving on to the commands. You need another import which is:

    from discord.ext import commands
    

    After that you can create your first command:

    @client.command()
    async def myfirstcommand(ctx:commands.Context):
        await ctx.send(f"Hello {ctx.author.mention}!")
    

    And you can trigger it by typing !myfirstcommand in the chat. However I see that you want to trigger it by an event. You can do that too:

    @client.event
    async def on_message(message):
        await client.process_commands(message)
        if message.content.lower() == "hi":
            await message.channel.send(f"Hello {message.author.mention}!")