Skip to content
This repository was archived by the owner on Sep 2, 2023. It is now read-only.

Commit 5176f69

Browse files
author
Awbugl
committed
Update Code
1 parent 5a84b21 commit 5176f69

File tree

5 files changed

+63
-45
lines changed

5 files changed

+63
-45
lines changed

Andreal.Core/Common/MessageInfo.cs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ internal class MessageInfo
2525
private static readonly Dictionary<string, string> AbbreviationPairs = new()
2626
{
2727
{ "/a ", "/arc " },
28-
{ "/o ", "/osu " },
29-
{ "/p ", "/pjsk " },
30-
{ "/ar ", "/arc room " }
28+
{ "/p ", "/pjsk " }
3129
};
3230

3331
internal Bot Bot { get; set; }
@@ -73,26 +71,48 @@ internal async Task<bool> PermissionCheck() =>
7371

7472
internal bool MasterCheck() => FromQQ == _master;
7573

76-
private object SendPrivateMessage(MessageChain messages) =>
77-
Bot.SendFriendMessage(FromQQ, FromMessageChain(messages));
74+
private async Task<bool> SendPrivateMessage(MessageChain messages)
75+
{
76+
try
77+
{
78+
return await Bot.SendFriendMessage(FromQQ, FromMessageChain(messages));
79+
}
80+
catch (Exception e)
81+
{
82+
Reporter.ExceptionReport(e);
83+
return false;
84+
}
85+
}
7886

79-
private object SendGroupMessage(MessageChain messages) =>
80-
Bot.SendGroupMessage(FromGroup, FromMessageChain(messages));
87+
private async Task<bool> SendGroupMessage(MessageChain messages)
88+
{
89+
try
90+
{
91+
return await Bot.SendGroupMessage(FromGroup, FromMessageChain(messages));
92+
}
93+
catch (Exception e)
94+
{
95+
Reporter.ExceptionReport(e);
96+
return false;
97+
}
98+
}
8199

82-
internal void SendMessage(MessageChain? message)
100+
internal async void SendMessage(MessageChain? message)
83101
{
84102
if (message is null) return;
85-
_ = FromGroup != 0 && MessageType == MessageInfoType.Group
86-
? SendGroupMessage(message.Prepend(new ReplyMessage(Message)))
87-
: SendPrivateMessage(message);
103+
if (FromGroup != 0 && MessageType == MessageInfoType.Group)
104+
await SendGroupMessage(message.Prepend(new ReplyMessage(Message)));
105+
else
106+
await SendPrivateMessage(message);
88107
}
89108

90-
internal void SendMessageOnly(MessageChain? message)
109+
internal async void SendMessageOnly(MessageChain? message)
91110
{
92111
if (message is null) return;
93-
_ = FromGroup != 0 && MessageType == MessageInfoType.Group
94-
? SendGroupMessage(message)
95-
: SendPrivateMessage(message);
112+
if (FromGroup != 0 && MessageType == MessageInfoType.Group)
113+
await SendGroupMessage(message);
114+
else
115+
await SendPrivateMessage(message);
96116
}
97117

98118
public static void Process(Bot bot, int messageType, uint fromGroup, uint fromQq, MessageStruct message)
@@ -175,7 +195,6 @@ or TaskCanceledException
175195
}
176196
}
177197
}
178-
179198
return;
180199
}
181200
});
@@ -192,9 +211,6 @@ private static string Replace(string rawMessage)
192211
case "/a":
193212
case "/arc":
194213
return "/arc info";
195-
case "/o":
196-
case "/osu":
197-
return "/osu mode";
198214
}
199215

200216
foreach (var (key, value) in

Andreal.Core/Executor/PermissionExecutor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ public PermissionExecutor(MessageInfo info) : base(info) { }
1616
if (Info.MasterCheck()) Info.SendMessageOnly(string.Join(" ", Command));
1717
return null;
1818
}
19-
19+
2020
[CommandPrefix("/remove")]
21-
private MessageChain? RemoveGroup()
21+
private async Task<MessageChain?> RemoveGroup()
2222
{
2323
if (!Info.MasterCheck()) return null;
2424
if (CommandLength != 1) return RobotReply.ParameterLengthError;
25-
Info.Bot.GroupLeave(uint.Parse(Command[0]));
25+
await Info.Bot.GroupLeave(uint.Parse(Command[0]));
2626
return "Removed.";
2727
}
2828

Andreal.Core/Model/Arcaea/ArcaeaChart.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,18 @@ internal async Task<Image> GetSongImage()
5757
if (!SongImage.TryGetValue(path, out var stream))
5858
{
5959
await using var fileStream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);
60-
stream = new MemoryStream(new byte[fileStream.Length]);
61-
await fileStream.CopyToAsync(stream);
60+
var bytes = new byte[fileStream.Length];
61+
fileStream.Read(bytes, 0, bytes.Length);
62+
await fileStream.FlushAsync();
6263
fileStream.Close();
64+
stream = new MemoryStream(bytes);
6365
SongImage.TryAdd(path, stream);
6466
}
6567

6668
var img = new Image(stream);
6769
if (img.Width == 512) return img;
6870
var newimg = new Image(img, 512, 512);
69-
newimg.SaveAsJpgWithQuality(path,85);
71+
newimg.SaveAsPng(path);
7072
img.Dispose();
7173
return newimg;
7274
}

Andreal.Window/Common/Program.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal static class Program
2828

2929
internal static AndrealConfig Config = JsonConvert.DeserializeObject<AndrealConfig>(File.ReadAllText(Path.Config))!;
3030

31-
public static readonly ConcurrentDictionary<uint, string> BotFriendList = new();
31+
private static readonly ConcurrentDictionary<uint, string> BotFriendList = new();
3232

3333
private static BotConfig _botConfig = BotConfig.Default();
3434

@@ -127,6 +127,7 @@ private static void Init(Bot bot)
127127
bot.OnLog += OnLog;
128128
bot.OnBotOnline += OnBotOnline;
129129
bot.OnBotOffline += OnBotOffline;
130+
bot.OnGroupMute += OnBotGroupMute;
130131
}
131132

132133
internal static async Task ProgramInit()
@@ -210,7 +211,8 @@ private static void OnFriendMessage(Bot b, FriendMessageEvent e)
210211
Add(Messages,
211212
new()
212213
{
213-
FromQQ = $"{(BotFriendList.ContainsKey(e.FriendUin) ? BotFriendList[e.FriendUin] : "")} ({e.FriendUin})",
214+
FromQQ
215+
= $"{(BotFriendList.ContainsKey(e.FriendUin) ? BotFriendList[e.FriendUin] : "")} ({e.FriendUin})",
214216
FromGroup = "-1 (私聊)",
215217
Time = e.EventTime,
216218
Message = e.Chain.ToString(),
@@ -222,19 +224,24 @@ private static void OnFriendMessage(Bot b, FriendMessageEvent e)
222224
if (Config.EnableHandleMessage) External.Process(b, 0, 0, e.FriendUin, e.Message);
223225
}
224226

225-
private static void OnFriendRequest(Bot b, FriendRequestEvent e)
227+
private static async void OnFriendRequest(Bot b, FriendRequestEvent e)
226228
{
227229
if (Config.EnableHandleMessage)
228230
if (Config.Settings.FriendAdd)
229-
b.ApproveFriendRequest(e.ReqUin, e.Token);
231+
await b.ApproveFriendRequest(e.ReqUin, e.Token);
230232
}
231233

232-
private static void OnGroupInvite(Bot b, GroupInviteEvent e)
234+
private static async void OnGroupInvite(Bot b, GroupInviteEvent e)
233235
{
234236
if (Config.EnableHandleMessage)
235237
if (Config.Settings.GroupAdd || e.InviterUin == Config.Master
236238
|| Config.Settings.GroupInviterWhitelist.Contains(e.InviterUin))
237-
b.ApproveGroupInvitation(e.GroupUin, e.InviterUin, e.Token);
239+
await b.ApproveGroupInvitation(e.GroupUin, e.InviterUin, e.Token);
240+
}
241+
242+
private static async void OnBotGroupMute(Bot b, GroupMuteMemberEvent e)
243+
{
244+
if (e.MemberUin == b.Uin) await b.GroupLeave(e.GroupUin);
238245
}
239246

240247
private static void OnCaptcha(Bot b, CaptchaEvent e)

Andreal.Window/UI/App.xaml.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Threading.Tasks;
32
using System.Windows;
43
using Andreal.Core.Common;
54
using Andreal.Window.Common;
@@ -12,19 +11,13 @@ internal partial class App
1211
{
1312
protected override void OnStartup(StartupEventArgs e)
1413
{
15-
TaskScheduler.UnobservedTaskException += (_, args) =>
16-
{
17-
Reporter.ExceptionReport(args.Exception);
18-
args.SetObserved();
19-
};
20-
21-
DispatcherUnhandledException += (_, args) =>
22-
{
23-
Reporter.ExceptionReport(args.Exception);
24-
args.Handled = true;
25-
};
26-
27-
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
14+
Current.DispatcherUnhandledException += (_, args) =>
15+
{
16+
Reporter.ExceptionReport(args.Exception);
17+
args.Handled = true;
18+
};
19+
20+
AppDomain.CurrentDomain.UnhandledException += (_, args) =>
2821
Reporter.ExceptionReport(args.ExceptionObject as Exception);
2922

3023
Reporter.OnExceptionRecorded += exception =>

0 commit comments

Comments
 (0)