Skip to content

Commit

Permalink
add csharp code based reply
Browse files Browse the repository at this point in the history
  • Loading branch information
cc004 committed Sep 4, 2020
1 parent baad5e4 commit d67d8f3
Show file tree
Hide file tree
Showing 13 changed files with 292 additions and 29 deletions.
2 changes: 2 additions & 0 deletions BandoriBotCore/BandoriBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.7.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.CodeDom" Version="4.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion BandoriBotCore/Commands/PCRCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ protected override void Run(CommandArgs args)
catch (ApiException e)
{
args.Callback(e.Message);
PCRManager.Instance.Do_Login();
}
}
}
Expand Down Expand Up @@ -301,7 +302,7 @@ public class CPMCommand : Command

protected override void Run(CommandArgs args)
{
args.Callback(PCRClientTest.PCRManager.Instance.GetRankStatistic(int.Parse(args.Arg.Trim())));
args.Callback(PCRManager.Instance.GetRankStatistic(int.Parse(args.Arg.Trim())));
}
}
}
37 changes: 34 additions & 3 deletions BandoriBotCore/Commands/ReplyCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ protected override void Run(CommandArgs args)
string[] splits = args.Arg.Trim().Split(' ');
if (string.IsNullOrWhiteSpace(args.Arg.Trim()))
{
args.Callback("/reply <add/del/list>[123] ...\n1 is string comparision\n2 is regex match\n3 is regex match without at(requires admin).\n");
args.Callback("/reply <add/del/list>[1234] ...\n1 is string comparision\n2 is regex match\n3 is regex match without at(requires admin).\n4 is c# code");
return;
}

Expand All @@ -41,13 +41,20 @@ protected override void Run(CommandArgs args)
case "add1":
case "add2":
case "add3":
case "add4":
{
if (splits.Length < 3)
{
args.Callback("Invalid argument count.");
return;
}
if (splits[0] != "add1")
switch (splits[0])
{
case "add1":

break;
}
if (splits[0] == "add1" || splits[0] == "add2")
{
try
{
Expand All @@ -59,6 +66,7 @@ protected override void Run(CommandArgs args)
return;
}
}

var reply = new Reply
{
qq = qq,
Expand All @@ -67,7 +75,7 @@ protected override void Run(CommandArgs args)

var data = config[int.Parse(splits[0].Substring(3))];

if (splits[0] == "add3" && !args.IsAdmin)
if ((splits[0] == "add3" || splits[0] == "add4" ) && !args.IsAdmin)
{
args.Callback("Access denied!");
return;
Expand All @@ -88,6 +96,27 @@ protected override void Run(CommandArgs args)
else
data.Add(splits[1], new List<Reply> { reply });

if (splits[0] == "add4")
{
try
{
config.ReloadAssembly();
}
catch (Exception e)
{
args.Callback(e.ToString());
try
{
config.Load();
}
catch
{
config.LoadDefault();
}
break;
}
}

config.Save();
args.Callback($"successfully added {(splits[0] == "add" ? "" : "regular expression")}`{splits[1]}` => `{reply.reply}`");

Expand All @@ -96,6 +125,7 @@ protected override void Run(CommandArgs args)
case "del1":
case "del2":
case "del3":
case "del4":
{
if (splits.Length < 3)
{
Expand Down Expand Up @@ -134,6 +164,7 @@ protected override void Run(CommandArgs args)
case "list1":
case "list2":
case "list3":
case "list4":
{
var data = config[int.Parse(splits[0].Substring(4))];
if (splits.Length == 1)
Expand Down
31 changes: 27 additions & 4 deletions BandoriBotCore/Commands/SetuCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BandoriBot.Config;
using BandoriBot.Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -36,6 +37,8 @@ protected override void Run(CommandArgs args)

public class SetuCommand : Command
{
private readonly Random rand = new Random();

private class SearchResult
{
public string uri;
Expand Down Expand Up @@ -134,13 +137,33 @@ private static async Task<List<SearchResult>> SearchAll(string keyword)

protected override void Run(CommandArgs args)
{
if (!Configuration.GetConfig<NormalAllowed>().hash.Contains(args.Source.FromGroup)) return;
var flag = Configuration.GetConfig<NormalAllowed>().hash.Contains(args.Source.FromGroup);
var flag2 = Configuration.GetConfig<R18Allowed>().hash.Contains(args.Source.FromGroup);

if (string.IsNullOrWhiteSpace(args.Arg))
{
var pics = Configuration.GetConfig<SetuConfig>().t.Where(pic => pic.r18 ? flag2 : flag).ToArray();
if (pics.Length == 0)
{
args.Callback("图片库为空或者你所在的群没有权限!");
return;
}
var pic = pics[rand.Next(pics.Length)];
var client = new HttpClient();
var imgres = Image.FromStream(client.GetStreamAsync(pic.url
.Replace("img-original", "c/540x540_70/img-master")
.Split("_p0.").First() + "_p0_master1200.jpg").Result);

args.Callback($"作品id: {pic.pid}\n" +
$"画师id: {pic.uid}\n" +
$"神秘链接: {pic.url}\n" +
Utils.GetImageCode(imgres));
return;
}

var result = SearchAll(args.Arg.Trim()).Result;

var flag = Configuration.GetConfig<R18Allowed>().hash.Contains(args.Source.FromGroup);

result = result.Where(t => t.sanity == 2 ^ flag).ToList();
result = result.Where(t => t.sanity == 2 && flag || t.sanity != 2 && flag2).ToList();

if (result.Count == 0)
{
Expand Down
6 changes: 6 additions & 0 deletions BandoriBotCore/Config/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ public static void SaveAll()
config.Save();
}
}

public static void Save<T>() where T : Configuration
{
GetConfig<T>().Save();
}

public static void LoadAll()
{
foreach (Configuration config in instances)
Expand Down
33 changes: 23 additions & 10 deletions BandoriBotCore/Config/DictConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand All @@ -7,31 +8,43 @@

namespace BandoriBot.Config
{
public abstract class DictConfiguration<TKey, TValue> : JConfiguration<JObject> where TKey : IEquatable<TKey>
[JsonObject]
public class Pair<TKey, TValue>
{
public TKey key;
public TValue value;
}

public abstract class DictConfiguration<TKey, TValue> : SerializableConfiguration<List<Pair<TKey, TValue>>> where TKey : IEquatable<TKey>
{
public override void LoadDefault()
{
t = new List<Pair<TKey, TValue>>();
}

public TValue this[TKey key]
{
get
{
foreach (var obj in list)
if (obj.Value<TKey>("key").Equals(key))
foreach (var obj in t)
if (obj.key.Equals(key))
{
return obj.Value<TValue>("value");
return obj.value;
}
return default;
}
set
{
foreach (var obj in list)
if (obj.Value<TKey>("key").Equals(key))
foreach (var obj in t)
if (obj.key.Equals(key))
{
obj["value"] = JToken.FromObject(value);
obj.value = value;
return;
}
list.Add(new JObject
t.Add(new Pair<TKey, TValue>
{
["name"] = JToken.FromObject(key),
["value"] = JToken.FromObject(value)
key = key,
value = value
});
}
}
Expand Down
12 changes: 12 additions & 0 deletions BandoriBotCore/Config/Save.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace BandoriBot.Config
{
public class Save : DictConfiguration<long, JObject>
{
public override string Name => "save.json";
}
}
21 changes: 21 additions & 0 deletions BandoriBotCore/Config/SetuConfig.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,4 +15,24 @@ public class NormalAllowed : HashConfiguration
{
public override string Name => "normalallowed.json";
}

[JsonObject]
public class Picture
{
public int pid, uid, p;
public string title, author, url;
public bool r18;
public int width, height;
public string[] tags;
}

public class SetuConfig : SerializableConfiguration<Picture[]>
{
public override string Name => "setu.json";

public override void LoadDefault()
{
t = Array.Empty<Picture>();
}
}
}
12 changes: 12 additions & 0 deletions BandoriBotCore/Handler/MessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Mirai_CSharp;
using Mirai_CSharp.Models;
using Mirai_CSharp.Plugin.Interfaces;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -15,6 +16,17 @@ public struct Source
{
public long FromGroup, FromQQ;
public MiraiHttpSession Session;

public JObject GetSave()
{
var result = Configuration.GetConfig<Save>()[FromQQ];
if (result == null)
{
result = new JObject();
Configuration.GetConfig<Save>()[FromQQ] = result;
}
return result;
}
}

public delegate void ResponseCallback(string message);
Expand Down
Loading

0 comments on commit d67d8f3

Please sign in to comment.