本文介绍了Discord.py 检查 Channel 是否为 DM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am creating a command that i only want to be executable through a DM with the bot. The current code makes it possible to send the command to any channel, i want to prevent this.

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

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

I've also tried: discord.ChannelType == discord.ChannelType.private & discord.DMChannel

推荐答案

在discord.py中,直接消息通道对象来自discord.channel.DMChannel.我们可以使用 isinstance():

In discord.py, direct message channel objects come from class discord.channel.DMChannel. We can check if an object comes from a class using isinstance():

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

这篇关于Discord.py 检查 Channel 是否为 DM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 10:32