Skip to content

Commit eb996c7

Browse files
committed
Improved coding style
1 parent c3f059e commit eb996c7

File tree

2 files changed

+58
-57
lines changed

2 files changed

+58
-57
lines changed

iTool.DiscordBot/Modules/Weather.cs

+41-39
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,55 @@ public class Weather : ModuleBase
1212
[Summary("Gets the weather info")]
1313
public async Task GetWeather(string input)
1414
{
15-
if (!string.IsNullOrEmpty(Program.Settings.OpenWeatherMapKey))
15+
if (string.IsNullOrEmpty(Program.Settings.OpenWeatherMapKey))
1616
{
17-
//TODO: Add weather
18-
OpenWeatherClient client = new OpenWeatherClient(Program.Settings.OpenWeatherMapKey);
19-
WeatherInfo weather = await client.GetCurrentAsync(input);
20-
weather.Temperature = weather.Temperature.ToCelsius(); //TODO: Add temperaturescale setting
17+
await Program.Log(new LogMessage(LogSeverity.Warning, "", "No OpenWeatherMapKey found."));
18+
return;
19+
}
2120

22-
EmbedBuilder b = new EmbedBuilder()
23-
{
24-
Title = weather.City.Name + " " + weather.City.Country,
25-
Color = new Color(3, 144, 255),
26-
ThumbnailUrl = weather.IconURL
27-
};
28-
b.AddField(delegate (EmbedFieldBuilder f)
29-
{
30-
f.IsInline = true;
31-
f.Name = "Temperature";
32-
f.Value = $"Max: {weather.Temperature.Max} {weather.Temperature.Unit}" + Environment.NewLine +
33-
$"Gem: {weather.Temperature.Value} {weather.Temperature.Unit}" + Environment.NewLine +
34-
$"Min: {weather.Temperature.Min} {weather.Temperature.Unit}";
35-
});
21+
OpenWeatherClient client = new OpenWeatherClient(Program.Settings.OpenWeatherMapKey);
22+
WeatherInfo weather = await client.GetCurrentAsync(input);
23+
weather.Temperature = weather.Temperature.ToCelsius(); //TODO: Add temperaturescale setting
3624

37-
if (weather.Precipitation.Value != 0)
38-
{
39-
b.AddField(delegate (EmbedFieldBuilder f)
40-
{
41-
f.IsInline = true;
42-
f.Name = "Precipation";
43-
f.Value = weather.Precipitation.Value + weather.Precipitation.Unit;
44-
});
45-
}
46-
47-
b.AddField(delegate (EmbedFieldBuilder f)
48-
{
49-
f.IsInline = true;
50-
f.Name = "Humidity";
51-
f.Value = weather.Humidity.Value + weather.Humidity.Unit;
52-
});
25+
EmbedBuilder b = new EmbedBuilder()
26+
{
27+
Title = weather.City.Name + " " + weather.City.Country,
28+
Color = new Color(3, 144, 255),
29+
ThumbnailUrl = weather.IconURL
30+
};
31+
b.AddField(delegate (EmbedFieldBuilder f)
32+
{
33+
f.IsInline = true;
34+
f.Name = "Temperature";
35+
f.Value = $"Max: {weather.Temperature.Max} {weather.Temperature.Unit}" + Environment.NewLine +
36+
$"Gem: {weather.Temperature.Value} {weather.Temperature.Unit}" + Environment.NewLine +
37+
$"Min: {weather.Temperature.Min} {weather.Temperature.Unit}";
38+
});
39+
40+
if (weather.Precipitation.Value != 0)
41+
{
5342
b.AddField(delegate (EmbedFieldBuilder f)
5443
{
5544
f.IsInline = true;
56-
f.Name = "Wind";
57-
f.Value = weather.Wind.Speed.Name + Environment.NewLine +
58-
weather.Wind.Speed.Value + "m/s";
45+
f.Name = "Precipation";
46+
f.Value = weather.Precipitation.Value + weather.Precipitation.Unit;
5947
});
60-
await ReplyAsync("", embed: b);
6148
}
49+
50+
b.AddField(delegate (EmbedFieldBuilder f)
51+
{
52+
f.IsInline = true;
53+
f.Name = "Humidity";
54+
f.Value = weather.Humidity.Value + weather.Humidity.Unit;
55+
});
56+
b.AddField(delegate (EmbedFieldBuilder f)
57+
{
58+
f.IsInline = true;
59+
f.Name = "Wind";
60+
f.Value = weather.Wind.Speed.Name + Environment.NewLine +
61+
weather.Wind.Speed.Value + "m/s";
62+
});
63+
await ReplyAsync("", embed: b);
6264
}
6365
}
6466
}

iTool.DiscordBot/Program.cs

+17-18
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ static class Program
1515
{
1616
public static void Main(string[] args) => Start().GetAwaiter().GetResult();
1717

18-
public static CommandHandler CommandHandler { get; private set;}
18+
public static CommandHandler CommandHandler { get; private set; }
1919
public static Settings Settings { get; set; }
2020

2121
private static DiscordSocketClient discordClient;
@@ -35,28 +35,26 @@ public static async Task Start()
3535
Console.ReadKey();
3636
Environment.Exit(0);
3737
}
38-
else
39-
{
40-
discordClient = new DiscordSocketClient();
4138

42-
await discordClient.LoginAsync(TokenType.Bot, Settings.DiscordToken);
43-
await discordClient.ConnectAsync();
44-
Console.WriteLine("Succesfully connected.");
39+
discordClient = new DiscordSocketClient();
4540

46-
if (!string.IsNullOrEmpty(Settings.Game))
47-
{
48-
await discordClient.SetGameAsync(Settings.Game);
49-
}
41+
await discordClient.LoginAsync(TokenType.Bot, Settings.DiscordToken);
42+
await discordClient.ConnectAsync();
43+
Console.WriteLine("Succesfully connected.");
5044

51-
DependencyMap map = new DependencyMap();
52-
map.Add(discordClient);
45+
if (!string.IsNullOrEmpty(Settings.Game))
46+
{
47+
await discordClient.SetGameAsync(Settings.Game);
48+
}
5349

54-
CommandHandler = new CommandHandler();
55-
await CommandHandler.Install(map);
50+
DependencyMap map = new DependencyMap();
51+
map.Add(discordClient);
5652

57-
discordClient.Log += Log;
58-
discordClient.MessageReceived += DiscordClient_MessageReceived;
59-
}
53+
CommandHandler = new CommandHandler();
54+
await CommandHandler.Install(map);
55+
56+
discordClient.Log += Log;
57+
discordClient.MessageReceived += DiscordClient_MessageReceived;
6058

6159
while (true)
6260
{
@@ -89,6 +87,7 @@ private async static Task DiscordClient_MessageReceived(SocketMessage arg)
8987
public static Task Log(LogMessage msg)
9088
{
9189
Console.WriteLine(msg.ToString());
90+
if (msg.Exception != null) { Console.WriteLine(msg.Exception.ToString()); }
9291
return Task.CompletedTask;
9392
}
9493

0 commit comments

Comments
 (0)