Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 95f60b6

Browse files
committed
Asks for configuration data if it wasn't found or the located data was corrupt.
Another step towards v0.1.3! In addition to the aforementioned feature, a few changes have been made. Most notably, the googleConfig.json file now refers to the spreadsheet as *SheetsID* rather than *SpreadsheetID*. Progress towards 0.1.3 is smoothly continuing, however, I'm not completely sure if I will be able to complete it for tomorrow. If not on Sunday, then the update will almost certainly be released the next day. *Summary* This commit resolves two issues, #7 and #6 .
1 parent 615950c commit 95f60b6

File tree

4 files changed

+240
-16
lines changed

4 files changed

+240
-16
lines changed

DiscordBot/Config.cs

+38-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ internal class Config
1414
public static BotConfig Bot;
1515
public static GoogleConfig GoogleData;
1616

17+
public static bool newBotConfig, newGoogleConfig;
18+
1719

1820

1921
static Config()
@@ -40,10 +42,12 @@ private static void CreateConfigFiles()
4042
Bot = new BotConfig
4143
{
4244
// Default values for BotConfig
45+
Token = null,
4346
Prefix = "!",
44-
UpdateDelay = 3600 // 1 hour default
47+
UpdateDelay = 60 // 1 hour default
4548
};
4649

50+
newBotConfig = true;
4751
string botJson = JsonConvert.SerializeObject(Bot, Formatting.Indented);
4852
File.WriteAllText(ConfFile, botJson);
4953
}
@@ -62,6 +66,36 @@ private static void CreateConfigFiles()
6266
}
6367
}
6468

69+
public static void WriteToConfig(string token, string prefix, int delay)
70+
{
71+
Bot = new BotConfig()
72+
{
73+
Token = token,
74+
Prefix = prefix,
75+
UpdateDelay = delay
76+
};
77+
78+
string botJson = JsonConvert.SerializeObject(Bot, Formatting.Indented);
79+
File.WriteAllText(ConfFile, botJson);
80+
}
81+
82+
public static void WriteToGoogleConfig(string apiKey, string spreadsheetID, string range, int rolesStartAfter, int rolesEndBefore, int discordID, int nickname)
83+
{
84+
GoogleData = new GoogleConfig()
85+
{
86+
APIKey = apiKey,
87+
SheetsID = spreadsheetID,
88+
Range = range,
89+
RolesStartAfter = rolesStartAfter,
90+
RolesEndBefore = rolesEndBefore,
91+
DiscordIDField = discordID,
92+
NicknameField = nickname,
93+
};
94+
95+
string botJson = JsonConvert.SerializeObject(Bot, Formatting.Indented);
96+
File.WriteAllText(ConfFile, botJson);
97+
}
98+
6599
private static void CreateGoogleConfigFiles()
66100
{
67101
if (!File.Exists(GoogleConfFile))
@@ -74,11 +108,11 @@ private static void CreateGoogleConfigFiles()
74108
NicknameField = -1
75109
};
76110

111+
newGoogleConfig = true;
77112
// Default values for GoogleData
78113

79114
string googleJson = JsonConvert.SerializeObject(GoogleData, Formatting.Indented);
80115
File.WriteAllText(GoogleConfFile, googleJson);
81-
Console.WriteLine("Google Sheets data not found or corrupt.");
82116
}
83117
else
84118
{
@@ -112,13 +146,13 @@ public struct BotConfig
112146
{
113147
public string Token; // Get this from discordapp.com/developers (this is also where you can add the bot to a server)
114148
public string Prefix; // The character required to send a command
115-
public int UpdateDelay; // Delay between checking for updates (in seconds)
149+
public int UpdateDelay; // Delay between checking for updates (in minutes)
116150
}
117151

118152
public struct GoogleConfig // Get the API Key from console.developers.google.com and make a sheets api and key. Get the sheets id from the google sheet (make one from a google form)
119153
{
120154
public string APIKey;
121-
public string SpreadsheetID;
155+
public string SheetsID;
122156
public string Range; // Range on the form ie. B2:L which gets all information from B2 to L (goes all the way down too)
123157
public int RolesStartAfter; // # of spots after the initial index in the range where the roles begin
124158
public int RolesEndBefore; // # of spots before the final index in the range where the last role lies

DiscordBot/DiscordBot.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<UpdatePeriodically>false</UpdatePeriodically>
2323
<UpdateRequired>false</UpdateRequired>
2424
<MapFileExtensions>true</MapFileExtensions>
25-
<ApplicationRevision>0</ApplicationRevision>
26-
<ApplicationVersion>0.1.2.%2a</ApplicationVersion>
25+
<ApplicationRevision>2</ApplicationRevision>
26+
<ApplicationVersion>0.1.3.%2a</ApplicationVersion>
2727
<UseApplicationTrust>false</UseApplicationTrust>
2828
<PublishWizardCompleted>true</PublishWizardCompleted>
2929
<BootstrapperEnabled>true</BootstrapperEnabled>

DiscordBot/Program.cs

+199-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Form2Role Bot v0.1.3a1 created by Talios0 (August 4th, 2018)
1+
/* Form2Role Bot v0.1.3a2 created by Talios0 (August 4th, 2018)
22
* Check the project out on Github: https://github.com/talios0/Form2RoleBot
33
* The program uses Newtonsoft's JSON, RogueExceptions' Discord.NET, and the Google Sheets API v4
44
*/
@@ -8,6 +8,7 @@
88
using System.Threading.Tasks;
99
using Discord;
1010
using Discord.WebSocket;
11+
using Google.Apis.Sheets.v4.Data;
1112

1213
namespace DiscordBot
1314
{
@@ -16,41 +17,230 @@ internal class Program
1617
private DiscordSocketClient _client;
1718
private CommandHandler _handler;
1819

19-
private const string Version = "0.1.3a1";
20+
private const string Version = "0.1.3a2";
2021

2122
private static void Main(string[] args)
2223
=> new Program().StartAsync(args).GetAwaiter().GetResult();
2324

2425
public async Task StartAsync(string[] args)
2526
{
27+
Console.WriteLine("---------------------------------------------------------------------");
28+
Console.WriteLine("Form2Role Bot v0.1.3a2");
29+
Console.WriteLine("Created by Talios0");
30+
Console.WriteLine("Check for updates at https://github.com/talios0/Form2RoleBot/releases");
31+
Console.WriteLine("----------------------------------------------------------------------\n\n");
32+
2633
StartArgsHandler(args);
2734

35+
RequestConfig();
36+
RequestGoogleConfig();
37+
38+
Console.WriteLine("\n");
2839
Console.WriteLine("Bot Token: " + Config.Bot.Token);
2940
Console.WriteLine("Bot Prefix: " + Config.Bot.Prefix);
3041

31-
if (string.IsNullOrEmpty(Config.Bot.Token))
32-
{
33-
return;
34-
}
3542
_client = new DiscordSocketClient(new DiscordSocketConfig
3643
{
3744
LogLevel = LogSeverity.Info
3845
});
46+
3947
_client.Log += Log;
4048
await _client.LoginAsync(TokenType.Bot, Config.Bot.Token);
4149
await _client.StartAsync();
4250
_handler = new CommandHandler();
4351
await _handler.InitializeAsync(_client);
44-
await Task.Delay(1000); // delay so update roles doesn't run before connecting to server.
52+
53+
54+
await Task.Delay(2500); // delay so update roles doesn't run before connecting to server.
55+
56+
Console.WriteLine("\n");
4557
await Sheets.UpdateRoles(_client); // forces update initially on all servers
46-
Console.WriteLine("Starting sheet checking loop");
58+
4759
while (true)
4860
{
49-
await Task.Delay(Config.Bot.UpdateDelay * 1000); // an hour between updates
61+
await Task.Delay(Config.Bot.UpdateDelay * 60000); // delay in minutes
5062
await Sheets.CheckSheets(_client);
5163
}
5264
}
5365

66+
private void RequestGoogleConfig()
67+
{
68+
if (Config.newGoogleConfig || Config.GoogleData.APIKey == null)
69+
{
70+
Console.WriteLine("Google configuration data wasn't found or is corrupt.\n\nPress enter to keep the default value.");
71+
int i = 0;
72+
73+
string apiKey = "";
74+
75+
while (string.IsNullOrWhiteSpace(apiKey))
76+
{
77+
if (i != 0) Console.WriteLine("Press enter to keep the default value. You can also configure the APIKey manually by editing the googleConfig.json file.\nAdditional information on how to find the API Key can be found on this repository's Readme");
78+
79+
Console.Write("API Key: ");
80+
apiKey = Console.ReadLine();
81+
i = 1;
82+
83+
if (apiKey == "") break;
84+
}
85+
86+
string spreadsheetId = "";
87+
while (string.IsNullOrWhiteSpace(spreadsheetId))
88+
{
89+
if (i != 1) Console.WriteLine("Press enter to keep the default value. You can also manually configure the SheetsID in googleConfig.json.\nAdditional information can be found on the Github repository.");
90+
91+
Console.Write("Spreadsheet ID: ");
92+
spreadsheetId = Console.ReadLine();
93+
i = 2;
94+
95+
if (spreadsheetId == "") break;
96+
}
97+
98+
string range = "";
99+
while (string.IsNullOrWhiteSpace(range))
100+
{
101+
if (i != 2) Console.WriteLine("Press enter to keep the default value. You can also manually configure the Range in googleConfig.json.\nAdditional information can be found on the Github repository.");
102+
103+
Console.Write("Range: ");
104+
range = Console.ReadLine();
105+
i = 3;
106+
107+
if (range == "") break;
108+
}
109+
110+
int rolesStartAfter = -5;
111+
while (rolesStartAfter == -5)
112+
{
113+
if (i != 3) Console.WriteLine("Press enter to keep the default value. You can also manually configure RolesStartAfter in googleConfig.json.\nAdditional information can be found on the Github repository.");
114+
115+
Console.Write("Roles Start After: ");
116+
string value = Console.ReadLine();
117+
i = 4;
118+
119+
if (value == "") break;
120+
121+
int.TryParse(value, out int temp);
122+
if (temp >= 0) rolesStartAfter = temp;
123+
}
124+
125+
int rolesEndBefore = -5;
126+
while (rolesEndBefore == -5)
127+
{
128+
if (i != 4) Console.WriteLine("Press enter to keep the default value. You can also manually configure RolesEndBefore in googleConfig.json.\nAdditional information can be found on the Github repository.");
129+
130+
Console.Write("Roles End Before: ");
131+
string value = Console.ReadLine();
132+
i = 5;
133+
134+
if (value == "") break;
135+
136+
int.TryParse(value, out int temp);
137+
if (temp >= 0)
138+
{
139+
rolesEndBefore = temp;
140+
}
141+
}
142+
143+
int discordID = -5;
144+
while (discordID == -5)
145+
{
146+
if (i != 5) Console.WriteLine("Press enter to keep the default value. You can also manually configure DiscordIDField in googleConfig.json.\nAdditional information can be found on the Github repository.");
147+
148+
Console.Write("Discord ID Field: ");
149+
string value = Console.ReadLine();
150+
i = 6;
151+
152+
if (value == "") break;
153+
154+
int.TryParse(value, out int temp);
155+
if (temp >= -1)
156+
{
157+
discordID = temp;
158+
}
159+
}
160+
161+
int nickname = -5;
162+
while (nickname == -5)
163+
{
164+
if (i != 6) Console.WriteLine("Press enter to keep the default value. You can also manually configure NicknameField in googleConfig.json.\nAdditional information can be found on the Github repository.");
165+
166+
Console.Write("Nickname Field: ");
167+
string value = Console.ReadLine();
168+
i = 7;
169+
170+
if (value == "") break;
171+
172+
int.TryParse(value, out int temp);
173+
if (temp >= -2)
174+
{
175+
nickname = temp;
176+
}
177+
}
178+
Config.WriteToGoogleConfig(apiKey, spreadsheetId, range, rolesStartAfter, rolesEndBefore, discordID, nickname);
179+
}
180+
181+
}
182+
183+
private static void RequestConfig()
184+
{
185+
if (Config.newBotConfig || Config.Bot.Token == null)
186+
{
187+
Console.WriteLine("Bot configuration data was not found or is corrupt.\n\nPress enter to keep the default value.");
188+
string token = "";
189+
int i = 0;
190+
while (string.IsNullOrWhiteSpace(token))
191+
{
192+
if (i != 0)
193+
Console.WriteLine("Please enter a bot token or configure it manually in the config.json file. Please refer to the readme for instructions on how to find the token.");
194+
195+
i = 1;
196+
Console.Write("Bot Token: ");
197+
token = Console.ReadLine();
198+
if (token == "") break;
199+
}
200+
201+
string prefix = "";
202+
while (string.IsNullOrWhiteSpace(prefix))
203+
{
204+
if (i != 1)
205+
Console.WriteLine(
206+
"Please enter a single character to serve as the command prefix. Otherwise, enter one manually in config.json.");
207+
208+
i = 2;
209+
Console.Write("Bot Prefix: ");
210+
prefix = Console.ReadLine();
211+
212+
if (prefix.Length == 0)
213+
{
214+
prefix = Config.Bot.Prefix;
215+
break;
216+
}
217+
else if (prefix.Length != 1) prefix = "";
218+
}
219+
220+
int delay = 0;
221+
while (delay <= 0)
222+
{
223+
if (i != 2)
224+
Console.WriteLine(
225+
"The delay between checking for updates from the attached Google Sheet, specified in minutes, must be greater than 0. This value can be changed manually in config.json.");
226+
227+
Console.Write("Delay (in minutes): ");
228+
string value = Console.ReadLine();
229+
230+
i = 3;
231+
if (value == "")
232+
{
233+
delay = Config.Bot.UpdateDelay;
234+
break;
235+
}
236+
237+
int.TryParse(value, out delay);
238+
}
239+
240+
Config.WriteToConfig(token, prefix, delay);
241+
}
242+
}
243+
54244

55245
private static async Task Log(LogMessage message)
56246
{

DiscordBot/Sheets.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace DiscordBot
1212
class Sheets
1313
{
1414

15-
private static readonly string SheetId = Config.GoogleData.SpreadsheetID;
15+
private static readonly string SheetId = Config.GoogleData.SheetsID;
1616
private static readonly string Range = Config.GoogleData.Range;
1717

1818
private static SheetsService _service;

0 commit comments

Comments
 (0)