discord.py Bot non risponde all'input nei DM

Dec 23 2020

Cosa sto cercando di fare : ricevere una risposta dall'autore del messaggio nei loro messaggi diretti con il bot.

Il mio problema : il bot non risponde quando viene inviato un messaggio nei DM come previsto. Non ci sono messaggi di errore.

Codice :

@client.command()
async def test(ctx):
    await ctx.send("Sending a dm now")
    def check(message):
        return message.author == ctx.author and message.channel == discord.channel.DMChannel
    try:
        await ctx.author.send("Say test: ")
        response = await client.wait_for('message', check=check)
        if response.content.lower() == 'test':
            await ctx.send("Test successful")
        elif response.content.lower() == 'banana':
            await ctx.author.send("That works too")
    except:
        # do things here

Immagini :

(Immagine sopra) Non viene fornita alcuna risposta nonostante siano soddisfatte le condizioni fornite.

Riferimenti / Altre domande a cui ho fatto riferimento :

  • Discord.py Controlla se Channel è un DM
  • discord.DMChannel API Reference
  • Discord.py - Rispondi a DM con un messaggio specifico
  • Discord.py bot: come posso fare in modo che il mio discord bot mi invii le risposte a un comando che gli utenti usano nei DM, ad esempio per un sondaggio?

Risposte

1 NuKeFluffy Dec 23 2020 at 09:49

C'è un problema con il tuo assegno, se stampi message.channelotterrai:

Direct Message with username#1234

E se stampi discord.channel.DMChannelotterrai:

<class 'discord.channel.DMChannel'>

Noterai che sono due cose diverse, cambiando il tuo assegno in questo dovrebbe risolvere il problema:

def check(message):
    return message.author == ctx.author and str(message.channel.type) == "private"