Discord bot Ticket

Good evening, I’m from Indonesia, I want to know how Python works, maybe my language is a bit strange because I use Goggle Translet. so I have a problem with my coding but it’s correct when I turn on the bot. My problem is when I respond to the bot with “halo/hai”, it will reply. I do it via discord dm, but when I respond to my chat server, the bot doesn’t respond to all the permissions and guides that I have. I’m looking for a search so that I can fix this problem. I ask the replit community for help in fixing this problem

I’m new here I don’t know how to share my coding code properly so I put my code here

import discord
from discord.ext import commands

intents = discord.Intents.default()

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_message(message):
    if message.author.bot:
        return
    if message.content.startswith('hai'):
        await message.channel.send('Hai! Selamat datang di server kami! ada yang bisa kami bantu?')
    if message.content.startswith('halo'):
        await message.channel.send('Halo! Selamat datang di server kami! ada yang bisa kami bantu?')

    print(f"PESAN: {message.content}")
    await bot.process_commands(message)

@bot.event
async def on_command_error(ctx, error):
    print(f'Error: {error}')
    print(f'Command: {ctx.message.content}')

@bot.command()
async def ticket(ctx):
    if ctx.channel.permissions_for(ctx.me).send_messages and ctx.channel.permissions_for(ctx.me).embed_links:
        embed = discord.Embed(title="Menu Tiket",
                              description="Pilih opsi untuk membuat tiket:",
                              color=discord.Color.blue())
        embed.add_field(name="1️⃣ Dukungan",
                        value="Buat tiket untuk mendapatkan dukungan",
                        inline=False)
        embed.add_field(name="2️⃣ Laporkan Bug",
                        value="Laporkan bug atau masalah",
                        inline=False)
        embed.add_field(name="3️⃣ Pertanyaan Umum",
                        value="Pertanyaan umum atau informasi",
                        inline=False)
        message = await ctx.send(embed=embed)
        for emoji in ['1️⃣', '2️⃣', '3️⃣']:
            await message.add_reaction(emoji)
    else:
        await ctx.send("Saya tidak memiliki izin yang diperlukan untuk membuat tiket di saluran ini.")

bot.run('bot token')

Hello @ZAZARRYU and welcome to the community!

In your code, you are using discord.Intents.default() , which does not include the intent to listen to messages in a guild (server). You need to enable the GUILD_MESSAGES intent like:

intents = discord.Intents.default()
intents.messages = True
intents.guilds = True
intents.guild_messages = True

Also, make sure that your bot has the necessary permissions on the server, such as Read Messages and Send Messages .

3 Likes

thank you for answering my question but here the code you gave is still the same still can’t send messages to the chat server. I’ve actually checked the bot but it’s the same

Can you provide the link to your repl and the error that the console is giving?

Looks like this is the link to my replit
https://replit.com/@ZAZARRYU/TICKET-BOT


In the console there is actually no problem, only a message like that appears

The code seems ok, did you enable the intents in the Discord Developer Portal as well?

You can also add print statements to see if the event is being triggered in your channel

@bot.event
async def on_message(message):
    # Add here
    print(f"Message received in channel '{message.channel}' from '{message.author}': {message.content}")

    if message.author.bot:
        return
    if message.content.startswith('hai'):
        await message.channel.send('Hai! Selamat datang di server kami! ada yang bisa kami bantu?')
        print("Responded to 'hai'")
    if message.content.startswith('halo'):
        await message.channel.send('Halo! Selamat datang di server kami! ada yang bisa kami bantu?')
        print("Responded to 'halo'")
2 Likes

Of course I have the intentions in the Discord Developer Portal but it still doesn’t work. Why don’t you try it with your own bot?

Try to add the print statement to your code and tell me what happens when you give the command

3 Likes

this is what happens in the logs

this is from dm:
Unknown User' from 'zazarryu': this on dm bot

this is from server chat:
Message received in channel 'general' from 'zazarryu': (here is a message that I wrote but can't see)

Try to further debug the code

@bot.event
async def on_message(message):
    # Include this more so we can have a different approach
    channel_type = "DM" if isinstance(message.channel, discord.DMChannel) else "Server"
    print(f"Message received in {channel_type} channel '{message.channel}' from '{message.author}': {message.content}")
    if channel_type == "Server":
        print(f"Server (Guild) Name: {message.guild.name}")

    if message.author.bot:
        return
    if message.content.startswith('hai'):
        await message.channel.send('Hai! Selamat datang di server kami! ada yang bisa kami bantu?')
        print("Responded to 'hai'")
    if message.content.startswith('halo'):
        await message.channel.send('Halo! Selamat datang di server kami! ada yang bisa kami bantu?')
        print("Responded to 'halo'")

    await bot.process_commands(message)

And try the message again and give me the results

1 Like

This is correct :white_check_mark:

For future reference please refer to Discord.JS documentation:

Discord.JS Docs

this on console
Message received in Server channel ‘general’ from ‘zazarryu’:
Server (Guild) Name: BOT TEST

do you know how to change it to discord.js?

This confirms that the bot is active and listening to messages in your server.
So, there is no error in your code, the most likely thing is that your bot is lacking permissions.

Are you sure that you enabled all contents in the Developer Portal Settings in your Bot Application? Discord Developer Portal

Enable every intent permission but these two are essential.

  • Privileged Gateway Intents
    • SERVER MEMBERS INTENT
    • MESSAGE CONTENT INTENT

Also, your bot need the necessary permissions in the server too. It needs the ‘Read Message History’ and ‘Send Messages’ permissions to function correctly.

You can also change the intents from your code to accept all intents

Change to:

intents = discord.Intents.all()

If you checked everything and it’s still the same, we can debug it EVEN FURTHER by logging additional information about each message.

Change your code to this:

@bot.event
async def on_message(message):
    channel_type = "DM" if isinstance(message.channel, discord.DMChannel) else "Server"
    print(f"Message received in {channel_type} channel '{message.channel}' from '{message.author}': {message.content}")
    if channel_type == "Server":
        print(f"Server (Guild) Name: {message.guild.name}, Channel ID: {message.channel.id}")

    # Here you check if the message has an embed or attachment
    if message.embeds:
        print(f"Message has embeds: {message.embeds}")
    if message.attachments:
        print(f"Message has attachments: {message.attachments}")

    # You can pick additional metadata too
    print(f"Author ID: {message.author.id}, Message ID: {message.id}")

    if message.author.bot:
        return
    if message.content.startswith('hai'):
        await message.channel.send('Hai! Selamat datang di server kami! ada yang bisa kami bantu?')
        print("Responded to 'hai'")
    if message.content.startswith('halo'):
        await message.channel.send('Halo! Selamat datang di server kami! ada yang bisa kami bantu?')
        print("Responded to 'halo'")

    await bot.process_commands(message)

And message me again about the result.
But remember to check every permission and intents first.

3 Likes

Thank you very much, now the bot can be chat server, maybe the problem is in intents = discord.Intents.all() I am very happy with the replit community, thank you very much for helping answer this question @WindLother

1 Like

Please refer to the Discord.JS documentation next time:

Discord.JS Docs

No need to question my response, it’s actually pretty straightforward.

Discord.JS Docs - Intents

Furthermore refer to Discord’s official API for future reference.

Discord Official API - Intents

It is. Only problem with it is that OP is using Python. Not JS.

Do you realize Discord.JS is a python module?

He has even imported discord module, either way it would not change anything I do not know why people have to over complicate things, it’s really not that complex.

I think you mean Discord.py.