python - Discord.py 检查 Channel 是否为 DM

标签 python discord.py

我正在创建一个命令,我只想通过机器人的 DM 执行该命令。当前代码可以将命令发送到任何 channel ,我想防止这种情况。

@client.command()
async def check(ctx, arg):
    if discord.ChannelType.private:
        await ctx.send(arg)

我也试过: discord.ChannelType == discord.ChannelType.private & discord.DMChannel

最佳答案

在 discord.py 中,直接消息 channel 对象来自 class discord.channel.DMChannel .我们可以使用 isinstance() 检查一个对象是否来自一个类:

@client.command()
async def check(ctx, arg):
    if isinstance(ctx.channel, discord.channel.DMChannel):
        await ctx.send(arg)

关于python - Discord.py 检查 Channel 是否为 DM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58987677/

相关文章:

python - Linux : Error during parsing: 'ascii' codec can't decode byte 0x8b in position 1: ordinal not in range(128)

python - Discord channel 成员(member)只返回一名成员(member)?

python - 重写discord.py |我怎样才能把它变成一个整数?

python runpy.run_module 退出代码

python - 获取所有可能的值组合

python - 如何配置 View 流过滤器?

discord.py - 如何在机器人启动时向它所在的每台服务器发送消息?

python - AIOHTTP meme 命令discord.py

python - 如何使用discord.py 获取discord 用户的用户ID

python 3 : Standard way to surpress the b' prefix when printing