[英]Discord.py Check if Channel is a DM
我正在创建一个我只想通过带有机器人的 DM 执行的命令。 当前代码可以将命令发送到任何通道,我想防止这种情况。
@client.command()
async def check(ctx, arg):
if discord.ChannelType.private:
await ctx.send(arg)
我也试过: discord.ChannelType == discord.ChannelType.private & discord.DMChannel
在 discord.py 中,直接消息通道对象来自class discord.channel.DMChannel
。 我们可以使用isinstance()
检查 object 是否来自 class :
@client.command()
async def check(ctx, arg):
if isinstance(ctx.channel, discord.channel.DMChannel):
await ctx.send(arg)
添加dm_only
检查:
@client.command()
@commands.dm_only()
async def check(ctx, arg):
await ctx.send(arg)
你可以试试:
@client.command()
async def check(ctx, arg):
if ctx.guild is False:
await ctx.send(arg)
我使用 discord.py 版本 1.3.3 并且if ctx.guild is False:
对我不起作用。 在我看来,您应该使用 discord class discord.ChannelType ,这就是它的用途。 以下代码适用于我
@client.command()
async def check(ctx, arg):
if ctx.channel.type is discord.ChannelType.private:
await ctx.send(arg)
@client.command()
async def check(ctx, arg):
if str(ctx.type) == "private":
await ctx.send(arg)
https://discordpy.readthedocs.io/en/latest/api.html#discord.ChannelType
#use this code if you are using @client.event
import discord
@client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith("register"):
if isinstance(message.channel, discord.DMChannel):
await message.author.send("registration mode coming soon!")
else:
await message.channel.send("pls register in bot dm")
client.run(bot token)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.