【发布时间】:2019-04-01 01:51:07
【问题描述】:
我已经创建了机器人,但是当有人在私信中使用命令发送消息时,它会在私信中给出回复。
我想让机器人仅在用户在服务器和文本通道中时响应命令。
有什么帮助吗?
【问题讨论】:
标签: c# discord discord.net
我已经创建了机器人,但是当有人在私信中使用命令发送消息时,它会在私信中给出回复。
我想让机器人仅在用户在服务器和文本通道中时响应命令。
有什么帮助吗?
【问题讨论】:
标签: c# discord discord.net
所以基本上你需要在你的 CommandHandler 中添加一个类似的行,这就是你可以阻止任何非 Guild 消息的方法。
if (message.Channel is SocketDMChannel) return;
一旦通道是 SocketDMChannel,它就会从方法中返回。
【讨论】:
我不得不在很多地方使用这个验证,所以我扩展了 ModuleBase 并将我所有的验证放在里面。 :
public bool IsFromGuildChat()
{
var IsFromGuildChat = Context.Guild.Id != 0;
if (IsFromGuildChat == false)
throw new RequiresDiscordGuildException(); //custom exception
return IsFromGuildChat;
}
然后在我的命令顶部:
[Command("test")]
[Alias("t")]
public async Task Test()
{
//validation
if (!IsFromGuildChat())
return;
await ReplyAsync("This is only called from Guild Chat!");
}
【讨论】: