-
Notifications
You must be signed in to change notification settings - Fork 2
/
Program.cs
168 lines (141 loc) · 5.29 KB
/
Program.cs
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using Approvers.King.Common;
using Approvers.King.Events;
using Discord.WebSocket;
namespace Approvers.King;
public static class Program
{
/// <summary>
/// エントリーポイント
/// </summary>
private static void Main(string[] args)
{
BuildAsync(args).GetAwaiter().GetResult();
}
private static async Task BuildAsync(string[] args)
{
await InitializeModulesAsync();
await InitializeStatesAsync();
RegisterEvents();
// 永久に待つ
await Task.Delay(-1);
}
/// <summary>
/// 共通基盤系を初期化する
/// </summary>
private static async Task InitializeModulesAsync()
{
TimeManager.Instance.Initialize();
await MasterManager.FetchAsync();
SchedulerManager.Initialize();
await DiscordManager.InitializeAsync();
}
private static async Task InitializeStatesAsync()
{
await using var app = AppService.CreateSession();
var gacha = await app.GetDefaultGachaAsync();
if (gacha.GachaItems.Count == 0)
{
gacha.ShuffleMessageRates();
}
if (gacha.HitProbability == Multiplier.Zero)
{
gacha.ShuffleRareReplyRate();
}
await app.SaveChangesAsync();
}
/// <summary>
/// 全てのイベントを登録する
/// </summary>
private static void RegisterEvents()
{
DiscordManager.Client.MessageReceived += message =>
{
OnMessageReceived(message);
return Task.CompletedTask;
};
SchedulerManager.RegisterDaily<DailyResetPresenter>(TimeManager.DailyResetTime);
SchedulerManager.RegisterMonthly<MonthlyResetPresenter>(TimeManager.MonthlyResetDay, TimeManager.DailyResetTime);
SchedulerManager.RegisterOn<SlotConditionRefreshPresenter>(x => x.Minute is 0);
}
/// <summary>
/// トリガーとなる文言を含んでいるか
/// </summary>
private static bool IsContainsTriggerPhrase(string content, TriggerType triggerType)
{
return MasterManager.TriggerPhraseMaster
.GetAll(x => x.TriggerType == triggerType)
.Any(x => content.Contains(x.Phrase));
}
private static void OnMessageReceived(SocketMessage message)
{
// botは弾く
if (message is not SocketUserMessage userMessage || userMessage.Author.IsBot) return;
if (userMessage.MentionedUsers.Any(x => x.Id == DiscordManager.Client.CurrentUser.Id))
{
if (message.Content.EndsWith("reload"))
{
// マスタデータをリロード
DiscordManager.ExecuteAsync<AdminMasterReloadPresenter>(userMessage).Run();
return;
}
if (TryExecuteMarugame(userMessage))
{
return;
}
if (IsContainsTriggerPhrase(userMessage.Content, TriggerType.GachaRanking))
{
// ガチャランキングの表示
DiscordManager.ExecuteAsync<GachaRankingPresenter>(userMessage).Run();
return;
}
if (IsContainsTriggerPhrase(userMessage.Content, TriggerType.SlotRanking))
{
// スロットランキングの表示
DiscordManager.ExecuteAsync<SlotRankingPresenter>(userMessage).Run();
return;
}
if (IsContainsTriggerPhrase(userMessage.Content, TriggerType.GachaExecute))
{
// 10連ガチャ
DiscordManager.ExecuteAsync<GachaCommandPresenter>(userMessage).Run();
return;
}
if (IsContainsTriggerPhrase(userMessage.Content, TriggerType.GachaGet))
{
// 排出率を投稿する
DiscordManager.ExecuteAsync<GachaInfoCommandPresenter>(userMessage).Run();
return;
}
if (IsContainsTriggerPhrase(userMessage.Content, TriggerType.SlotExecute))
{
// スロットを回す
DiscordManager.ExecuteAsync<SlotExecutePresenter>(userMessage).Run();
return;
}
// 返信
DiscordManager.ExecuteAsync<GachaInteractReplyPresenter>(userMessage).Run();
return;
}
// 発言
DiscordManager.ExecuteAsync<GachaRareReplyPresenter>(userMessage).Run();
}
private static bool TryExecuteMarugame(SocketUserMessage userMessage)
{
// 「丸亀製麺」の次にある改行以降が対象
var marugameTrigger =
MasterManager.TriggerPhraseMaster.FirstOrDefault(x => x.TriggerType == TriggerType.Marugame)?.Phrase ?? "";
var marugameIndex =
userMessage.Content.IndexOf(marugameTrigger, StringComparison.InvariantCulture);
if (marugameIndex < 0) return false;
var subs = userMessage.Content[(marugameIndex + marugameTrigger.Length)..];
var contentIndex = subs.IndexOf('\n');
if (contentIndex < 0) return false;
// 丸亀製麺
DiscordManager.ExecuteAsync<MarugamePresenter>(userMessage, presenter =>
{
presenter.Content = subs[(contentIndex + 1)..];
return Task.CompletedTask;
}).Run();
return true;
}
}