我试图检查用户是否在其中发布了带有黑名单的消息,如果找到列入黑名单的单词则删除该消息但是我无法以任何方式检查这些单词。我已将OnMessageReceived
回调分配给MessageReceived
功能。我对此有点新意,所以这可能是一个简单的答案,但我在过去的两天里一直试图解决这个问题。
private async Task OnMessageReceived(SocketMessage msg)
{
for (int i = 0; i < blacklistedWords.Length; i++)
{
if (msg.Content.Contains(blacklistedWords[i]))
{
Console.WriteLine(msg.Author.ToString() + " did a bad");
await msg.Channel.DeleteMessagesAsync(); //I can't find out what to pass in here to delete the message
}
}
}
答案 0 :(得分:0)
尝试创建全局重复数据结构并将编辑后的内容分配到数据结构中。即
List<SocketMessageModel > mylist = new List<SocketMessageModel >();
private async Task OnMessageReceived(SocketMessage msg)
{
SocketMessageModel msgModel = new SocketMessageModel();
msgModel.Content = msg.Content;
msgModel.Author = msg.Author;
Boolean blackListCheck = false;
for (int i = 0; i < blacklistedWords.Length; i++)
{
Console.WriteLine(msg.Author.ToString() + " did a bad");
if (msg.Content.Contains(blacklistedWords[i]))
{
blackListCheck = true;
break;
}
}
if (blackListCheck == false)
{
mylist.add(msg);
}
}
public class SocketMessageModel
{
public string Author {get; set; }
public string Content {get; set; }
}
使用此解决方案,将从全局数据结构中排除包含列入黑名单的所有消息。然后,在解析每条消息后,您可以对存储在简单列表中的所有消息执行任何操作。
答案 1 :(得分:0)
好的我明白了。我只需要先收到我试图删除的消息。这是最终的代码
private async Task OnMessageReceived(SocketMessage msg)
{
foreach (var word in blacklistedWords)
{
if (msg.Content.Contains(word))
{
var messagesToDelete = await msg.Channel.GetMessagesAsync(1).Flatten();
await msg.Channel.DeleteMessagesAsync(messagesToDelete);
}
}
}