-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUtils.cs
More file actions
55 lines (53 loc) · 2.06 KB
/
Utils.cs
File metadata and controls
55 lines (53 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using cqhttp.Cyan.Enums;
using cqhttp.Cyan.Events.CQEvents;
using cqhttp.Cyan.Events.CQEvents.Base;
namespace EssentialBot {
[AttributeUsage (AttributeTargets.Method, AllowMultiple = true)]
public class OnCommandAttribute : Attribute {
public string command;
public OnCommandAttribute (string command) {
this.command = command;
}
}
[AttributeUsage (AttributeTargets.Method)]
public class OnMessageAttribute : Attribute { }
public static class Extensions {
public static IEnumerable<MethodInfo> GetMethodsBySig (this Type type, Type returnType, params Type[] parameterTypes) {
return type.GetMethods ().Where ((m) => {
if (m.ReturnType != returnType) return false;
var parameters = m.GetParameters ();
if ((parameterTypes == null || parameterTypes.Length == 0))
return parameters.Length == 0;
if (parameters.Length != parameterTypes.Length)
return false;
for (int i = 0; i < parameterTypes.Length; i++) {
if (parameters[i].ParameterType != parameterTypes[i])
return false;
}
return true;
});
}
public static (MessageType, long) GetEndpoint (this MessageEvent e) {
if (e is GroupMessageEvent) {
return (
MessageType.group_,
(e as GroupMessageEvent).group_id
);
} else if (e is PrivateMessageEvent) {
return (
MessageType.private_,
(e as PrivateMessageEvent).sender_id
);
} else if (e is DiscussMessageEvent) {
return (
MessageType.discuss_,
(e as DiscussMessageEvent).discuss_id
);
} else return (MessageType.private_, 745679136);
}
}
}