Skip to content

Commit

Permalink
fixed #14; fixed #16; closes #15; closes #13; implemented event featu…
Browse files Browse the repository at this point in the history
…res closes #7
  • Loading branch information
MarcoPstr committed Feb 14, 2021
1 parent 07734b6 commit 4da4a2b
Showing 15 changed files with 530 additions and 48 deletions.
35 changes: 33 additions & 2 deletions Config.cs
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ public class ServerConfig
public ulong guildID { get; set; }
public ulong NewsChannelID { get; set; }
public ushort MinimumStars { get; set; }
public bool OnlyFeaturedEvents { get; set; }
}

public class Config : ModuleBase<SocketCommandContext>
@@ -67,7 +68,7 @@ public async Task ChangeMinStars(string stars = "")
ServerConfig _config = new ServerConfig();
_config = GetServerConfig(Context.Guild);
_config.MinimumStars = starsNum;
FileStream fs = new FileStream("./cache/serverconfig/" + Context.Guild.Id + ".xml", FileMode.Open);
FileStream fs = new FileStream("./cache/serverconfig/" + Context.Guild.Id + ".xml", FileMode.Create);
XmlSerializer _xml = new XmlSerializer(typeof(ServerConfig));
_xml.Serialize(fs, _config);
fs.Close();
@@ -78,6 +79,35 @@ public async Task ChangeMinStars(string stars = "")
await ReplyAsync("", false, builder.Build());
}

[Command("featuredevents"), RequireUserPermission(GuildPermission.Administrator)]
public async Task ChangeFeaturedEvents(string arg = "")
{
EmbedBuilder builder = new EmbedBuilder();
ServerConfig _config = new ServerConfig();
_config = GetServerConfig(Context.Guild);
string description = "You successfully changed the event output ";
if (arg.ToLower() != "true" && arg.ToLower() != "false")
{
builder.WithColor(Color.Red)
.WithTitle("SYNTAX ERROR")
.WithDescription("Please mind the syntax: !featuredevents [true/false]")
.WithCurrentTimestamp();
await ReplyAsync("", false, builder.Build());
}
else if (arg.ToLower() == "true") { _config.OnlyFeaturedEvents = true; description += "ONLY FEATURED EVENTS"; }
else { _config.OnlyFeaturedEvents = false; description += "SHOW ALL EVENTS"; }

FileStream fs = new FileStream("./cache/serverconfig/" + Context.Guild.Id + ".xml", FileMode.Create);
XmlSerializer _xml = new XmlSerializer(typeof(ServerConfig));
_xml.Serialize(fs, _config);
fs.Close();
builder.WithColor(Color.Green)
.WithTitle("SUCCESS")
.WithDescription(description)
.WithCurrentTimestamp();
await ReplyAsync("", false, builder.Build());
}

/// <summary>
/// Creates channel and sets it as output for HLTVNews and HLTVMatches
/// </summary>
@@ -107,7 +137,8 @@ public async Task GuildJoined(SocketGuild guild, DiscordSocketClient client, Soc

_config.NewsChannelID = channel.Id;
_config.guildID = guild.Id;
_config.MinimumStars = 0;
_config.MinimumStars = 0;
_config.OnlyFeaturedEvents = false;

_xml = new XmlSerializer(typeof(ServerConfig));
Directory.CreateDirectory("./cache/serverconfig");
12 changes: 9 additions & 3 deletions Modules/CacheCleaner.cs
Original file line number Diff line number Diff line change
@@ -9,8 +9,7 @@ namespace HLTVDiscordBridge.Modules
public class CacheCleaner : ModuleBase<SocketCommandContext>
{
public void Cleaner(DiscordSocketClient client)
{

{
//upcoming.json
string upcoming = File.ReadAllText("./cache/upcoming.json");
JArray jArr = JArray.Parse(upcoming);
@@ -44,7 +43,14 @@ public void Cleaner(DiscordSocketClient client)
{
if (Directory.GetCreationTime(dir).AddDays(7).Date == DateTime.Now.Date) { Directory.Delete(dir); }
}


if(DateTime.Now.DayOfWeek == DayOfWeek.Tuesday)
{
foreach(string file in Directory.GetFiles("./cache/ranking"))
{
File.Delete(file);
}
}
}
}
}
53 changes: 33 additions & 20 deletions Modules/HLTVRanking.cs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@
using Discord.Commands;
using Newtonsoft.Json.Linq;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

@@ -32,29 +33,41 @@ public async Task getRanking(string arg = "GLOBAL")
uri = new Uri("https://hltv-api-steel.vercel.app/api/ranking/" + arg);
}


HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = uri;
HttpResponseMessage response = await httpClient.GetAsync(uri);
JArray jArr = JArray.Parse("[]");
try { jArr = JArray.Parse(await response.Content.ReadAsStringAsync()); }
catch (Newtonsoft.Json.JsonReaderException)
{
Console.WriteLine($"{DateTime.Now.ToString().Substring(11)}API\t API down");
embed.WithColor(Color.Red)
.WithTitle($"SYSTEM ERROR")
.WithDescription("Our API is down! Please try again later or contact us on [github](https://github.com/Zsunamy/HLTVDiscordBridge/issues).");
await ReplyAsync("", false, embed.Build());
return;
}

if (jArr.Count == 0)
//cache
JArray jArr;
Directory.CreateDirectory("./cache/ranking");
if(!File.Exists($"./cache/ranking/ranking_{arg.ToLower()}.json"))
{
embed.WithColor(Color.Red)
HttpClient httpClient = new HttpClient();
httpClient.BaseAddress = uri;
HttpResponseMessage response = await httpClient.GetAsync(uri);
try { jArr = JArray.Parse(await response.Content.ReadAsStringAsync()); }
catch (Newtonsoft.Json.JsonReaderException)
{
Console.WriteLine($"{DateTime.Now.ToString().Substring(11)}API\t API down");
embed.WithColor(Color.Red)
.WithTitle($"SYSTEM ERROR")
.WithDescription("Our API is down! Please try again later or contact us on [github](https://github.com/Zsunamy/HLTVDiscordBridge/issues).");
await ReplyAsync("", false, embed.Build());
return;
}
if (jArr.Count == 0)
{
embed.WithColor(Color.Red)
.WithTitle($"{arg} DOES NOT EXIST")
.WithDescription("Please state a valid country!");
await ReplyAsync("", false, embed.Build());
return;
await ReplyAsync("", false, embed.Build());
return;
} else
{
FileStream fs = File.Create($"./cache/ranking/ranking_{arg.ToLower()}.json");
fs.Close();
File.WriteAllText($"./cache/ranking/ranking_{arg.ToLower()}.json", jArr.ToString());
}
}
else
{
jArr = JArray.Parse(File.ReadAllText($"./cache/ranking/ranking_{arg.ToLower()}.json"));
}

int teamsDisplayed = jArr.Count;
Loading

0 comments on commit 4da4a2b

Please sign in to comment.