es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Cómo detener los mensajes repetitivos, y el token es cambiado, pero no se ejecuta.

Hoy comencé a aprender Python y creé un bot de Discord. Tengo algunos problemas:

  1. Se usó If message.author == en el bloque on_message, pero el bot continuó respondiendo a sí mismo.
  2. Después, se creó un nuevo bot con un nuevo token y el código no funcionó.

Busqué mucho en este sitio y en Google. No encontré ninguna solución. Está bien modificar mi código. Todo proviene de internet, así que puede ser un desastre. Por favor, ayúdame.

import discord
import asyncio
from discord.ext import commands

client = discord.Client()

bot = commands.Bot(command_prefix="!")

@bot.event
async def on_ready():
    print('Conectado como Bot: ', bot.user.name)
    print('ID del Bot: ', bot.user.id)
    print('¡La conexión fue exitosa!')
    print('=' * 30)

@client.event
async def on_message(message) :

<pre><code>if on_message.content.startswith('!의뢰'):
    msg = on_message.channel.content[3:]
    embed = discord.Embed(title = "브리핑", description = msg, color = 0x62c1cc)
    embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
    embed.set_footer(text="C0de") 
    await on_message.channel.send("새로운 의뢰가 들어왔습니다", embed=embed)
    await client.process_commands(message)
</code></pre>

client.run("Token de mi bot.")
Tags:  , ,

Answer

  1. Avatar for davy.ai

    Solución para Problemas de Bots en Discord

    1. Para evitar que el bot responda a sí mismo, debes utilizar if message.author.bot: para verificar si el remitente del mensaje es un bot o no. Si es un bot, entonces la función debe retornar sin realizar más procesamiento.

    Aquí está el código actualizado:

    @client.event
    async def on_message(message):
        if message.author.bot:
            return
        if message.content.startswith('!의뢰'):
            msg = message.content[3:]
            embed = discord.Embed(title="브리핑", description=msg, color=0x62c1cc)
            embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
            embed.set_footer(text="C0de")
            channel = message.channel
            await channel.send("새로운 의뢰가 들어왔습니다", embed=embed)
    
    1. En el segundo problema, es posible que el problema haya ocurrido porque usaste client.run() en lugar de bot.run(). Asegúrate de usar el nombre de variable correcto al ejecutar el bot.

    Aquí está el código corregido:

    import discord
    import asyncio
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix="!")
    
    @bot.event
    async def on_ready():
        print('Logged in Bot:', bot.user.name)
        print('Bot id:', bot.user.id)
        print('La conexión fue exitosa!')
        print('=' * 30)
    
    @bot.event
    async def on_message(message):
        if message.author.bot:
            return
        if message.content.startswith('!의뢰'):
            msg = message.content[3:]
            embed = discord.Embed(title="브리핑", description=msg, color=0x62c1cc)
            embed.set_thumbnail(url="https://i.imgur.com/UDJYlV3.png")
            embed.set_footer(text="C0de")
            channel = message.channel
            await channel.send("새로운 의뢰가 들어왔습니다", embed=embed)
    
    bot.run("El token de mi bot.")
    

    Espero que esto te ayude con tu bot de Discord ¡Buena suerte con tus futuros proyectos en Python!

Comments are closed.