Skip to content

Commit

Permalink
feat: blacklist
Browse files Browse the repository at this point in the history
Closes #8
  • Loading branch information
guimc233 committed Nov 30, 2024
1 parent 2c818c4 commit e7dfe19
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
58 changes: 36 additions & 22 deletions YounBot/Command/Implements/YounkooCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public async Task AddAdmin(BotContext context, MessageChain chain, BotGroupMembe
YounBotApp.Config!.BotAdmins!.Add(member.Uin);
SaveConfig("younbot-config.json", YounBotApp.Config, true);
await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
.Forward(chain)
.Text("[滥权小助手] ").Mention(member.Uin)
.Text(" 被提升为管理员! ").Build());
}
Expand All @@ -40,6 +41,7 @@ public async Task RemoveAdmin(BotContext context, MessageChain chain, BotGroupMe
YounBotApp.Config!.BotAdmins!.Remove(member.Uin);
SaveConfig("younbot-config.json", YounBotApp.Config, true);
await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
.Forward(chain)
.Text("[滥权小助手] ").Mention(member.Uin)
.Text(" 被取消管理员! ").Build());
}
Expand All @@ -66,6 +68,7 @@ public async Task Stop(BotContext context, MessageChain chain)
if (IsBotOwner(chain))
{
await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
.Forward(chain)
.Text("Stopping YounBot " + YounBotApp.VERSION).Build());
SaveConfig("younbot-config.json", YounBotApp.Config!, true);
SaveConfig(YounBotApp.Configuration["ConfigPath:Keystore"] ?? "keystore.json", YounBotApp.Client!.UpdateKeystore(), true);
Expand Down Expand Up @@ -97,6 +100,7 @@ public async Task Status(BotContext context, MessageChain chain)
return;
}
await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
.Forward(chain)
.Text($"Uptime: {DateTimeOffset.Now.ToUnixTimeSeconds() - YounBotApp.UpTime!.Value}s\n")
.Text($"Bot Version: {YounBotApp.VERSION}\n")
.Text($"Receive pre min (1m/5m/10m): {MessageCounter.GetReceivedMessageLastMinutes()}/{Math.Round(MessageCounter.GetReceivedMessageLastMinutes(5)/5d, 2)}/{Math.Round(MessageCounter.GetReceivedMessageLastMinutes(10) / 10d, 2)}\n")
Expand All @@ -117,26 +121,36 @@ public async Task Ban(BotContext context, MessageChain chain, BotGroupMember mem
}
}

// [Command("blacklist", "黑名单")]
// public async Task Blacklist(BotContext context, MessageChain chain, BotGroupMember member, uint group = 0, string reason = "No reason")
// {
// if (HasPermission(chain))
// {
// ILiteCollection<BsonValue>? collection = YounBotApp.Db!.GetCollection<BsonValue>("blacklist");
// // find if the user is in the blacklist
// if (collection.Exists(x => x == new BsonValue(member.Uin.ToString())))
// {
// collection.Delete(new BsonValue(member.Uin.ToString()));
// await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
// .Text("已移除 ").Build());
// }
// else
// {
// collection.Insert(new BsonValue(member.Uin.ToString()));
// await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
// .Text("[滥权小助手] ").Mention(member.Uin)
// .Text("已添加").Build());
// }
// }
// }
[Command("blacklist", "黑名单")]
public async Task Blacklist(BotContext context, MessageChain chain, BotGroupMember member)
{
if (HasPermission(chain))
{
if (HasPermission(member))
{
await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
.Forward(chain)
.Text("无法将机器人管理员添加到黑名单").Build());
return;
}

// find if the user is in the blacklist
if (YounBotApp.Config!.BlackLists!.Contains(member.Uin))
{
YounBotApp.Config!.BlackLists!.Remove(member.Uin);
await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
.Forward(chain)
.Text("[滥权小助手] 已移除").Build());
}
else
{
YounBotApp.Config!.BlackLists!.Add(member.Uin);
await context.SendMessage(MessageBuilder.Group(chain.GroupUin!.Value)
.Forward(chain)
.Text("[滥权小助手] 已添加").Build());
}

SaveConfig("younbot-config.json", YounBotApp.Config!, true);
}
}
}
4 changes: 3 additions & 1 deletion YounBot/Config/YounBotConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class YounBotConfig
public int MaxMessageCache { get; set; }
public int MaxGroupMessageCache { get; set; }
public List<uint>? BotAdmins { get; set; }
public List<uint>? BlackLists { get; set; }

public static YounBotConfig NewConfig() => new()
{
Expand All @@ -19,6 +20,7 @@ public class YounBotConfig
WorkersAiBasicAuth = "username:password",
MaxMessageCache = 12,
MaxGroupMessageCache = 25,
BotAdmins = new List<uint>()
BotAdmins = new List<uint>(),
BlackLists = new List<uint>()
};
}
3 changes: 1 addition & 2 deletions YounBot/YounBotApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ public Task Run()
if (!Config!.WorkersAiUrl!.Equals("http://0.0.0.0/"))
await AntiAd.OnGroupMessage(context, @event);

// ILiteCollection<BsonValue>? collection = Db!.GetCollection<BsonValue>("blacklist");
// if (collection.Exists(x => x == new BsonValue(@event.Chain.FriendUin.ToString()))) return;
if (Config!.BlackLists!.Contains(@event.Chain.FriendUin)) return;
string text = MessageUtils.GetPlainText(@event.Chain);
string commandPrefix = Configuration["CommandPrefix"] ?? "/"; // put here for auto reload
if (text.StartsWith(commandPrefix))
Expand Down
6 changes: 2 additions & 4 deletions YounBot/YounBotAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public void ConfigureBots()
SaveConfig(configPath, _younBotConfig);
_younBotConfig = JsonSerializer.Deserialize<YounBotConfig>(File.ReadAllText(configPath)) ?? YounBotConfig.NewConfig();

if (_younBotConfig.BotAdmins == null) {
_younBotConfig.BotAdmins = new();
}

if (_younBotConfig.BotAdmins == null) _younBotConfig.BotAdmins = new();
if (_younBotConfig.BlackLists == null) _younBotConfig.BlackLists = new();
}

public YounBotApp Build() => new(this);
Expand Down

0 comments on commit e7dfe19

Please sign in to comment.