簡體   English   中英

在 dm discord.py 中等待來自同一作者的消息

[英]wait for message from same author in dm discord.py

當機器人加入時,我有一個 function 來設置服務器,但我同時與我的朋友交談並且它出現錯誤,因為我只希望機器人讀取來自 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)

有什么辦法嗎?

您可以為命令添加@commands.dm_only()裝飾器,使其僅在 DM 通道中工作:

import discord
from discord.ext import commands

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

或者您可以更改您的檢查以檢查消息是否在 DM 頻道中發送:

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

您可以簡單地使用isinstance function 並檢查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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM