Nekidev
Nekidev

Reputation: 79

wait for message from same author in dm discord.py

I've got a function to set up the server when the bot joins, but i'm talking with my friends at the same time and it is getting errors because i only want the bot to read messages from dm

async def on_guild_join(guild):

    print("Bot added to server: " + guild.name)

    gid = str(guild.id)

    server = Server()

    server.name = guild.name
    server.discordId = guild.id
    server.ownerId = guild.id
    server.welcome_message = None
    server.lang = "en"
    # here goes another tons of values

    if guild.id in db['guilds']:
        pass
    else:
        servers_dic = db['guilds']
        servers_dic[gid] = server.toJSON()
        print(server.toJSON())
        db['guilds'] = servers_dic

    await guild.owner.send(f"Hi! Thanks for adding me to your server, {guild.name}! To start using me, we'll have to set up a few things. Do you want to do it now or later?\n\n(n/l)")

    msg = await bot.wait_for('message', check=lambda message: message.author == guild.owner)

    if msg.content.lower() in ['n', 'now']:

        server = deencoder(db['guilds'][gid])
        if isinstance(server, Server):
            print("Class created successfully!")
        print(server)

is there any way to do that?

Upvotes: 0

Views: 1379

Answers (2)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15689

You can simply use the isinstance function and check for a discord.DMChannel

def check(message):
    return message.author == guild.owner and isinstance(message.channel, discord.DMChannel)

msg = await bot.wait_for('message', check=check)

# or if you still want to use lambda expressions
msg = await bot.wait_for('message', check=lambda message: message.author == guild.owner and isinstance(message.channel, discord.DMChannel))

Upvotes: 1

creed
creed

Reputation: 1427

You could add the @commands.dm_only() decorator for the command to only work in a DM channel:

import discord
from discord.ext import commands

@bot.command()
@commands.dm_only()
async def something(ctx):
    #do something

Or you could change your check to check if the message was sent in a DM channel:

msg = await bot.wait_for('message', check=lambda message: message.author == guild.owner and isinstance(message.channel, discord.DMChannel))

Upvotes: 1

Related Questions