Skip to content

Commit

Permalink
Merge pull request #51 from GravityWolfNotAmused/Status-Format-Update
Browse files Browse the repository at this point in the history
User Defined Status Update
  • Loading branch information
GravityWolfNotAmused authored Sep 25, 2022
2 parents 7dc7571 + 66695e0 commit 3171a98
Show file tree
Hide file tree
Showing 65 changed files with 464 additions and 552 deletions.
2 changes: 1 addition & 1 deletion DiscordPlayerCountBot.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DiscordPlayerCountBot.Json;
using PlayerCountBot.Json;

namespace DiscordPlayerCountBot.Tests;

Expand Down
4 changes: 1 addition & 3 deletions DiscordPlayerCountBot/Attributes/AttributeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Linq;

namespace DiscordPlayerCountBot.Attributes
namespace PlayerCountBot.Attributes
{
public static class AttributeHelper
{
Expand Down
7 changes: 3 additions & 4 deletions DiscordPlayerCountBot/Attributes/NameAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;

namespace DiscordPlayerCountBot.Attributes
namespace PlayerCountBot.Attributes
{

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
public class NameAttribute : Attribute
{
public string Name { get; set; }
public string Name { get; private set; }

public NameAttribute(string name)
{
Expand Down
55 changes: 20 additions & 35 deletions DiscordPlayerCountBot/Bot/Bot.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
using Discord;
using Discord.WebSocket;
using DiscordPlayerCountBot;
using DiscordPlayerCountBot.Enum;
using DiscordPlayerCountBot.Http;
using DiscordPlayerCountBot.Providers;
using DiscordPlayerCountBot.Providers.Base;
using log4net;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace PlayerCountBot
namespace PlayerCountBot
{
public class Bot

[Name("Bot")]
public class Bot : LoggableClass
{
public BotInformation Information { get; set; }
public DiscordSocketClient DiscordClient { get; set; }
public Dictionary<int, IServerInformationProvider> DataProviders { get; set; } = new();
public Dictionary<string, string> ApplicationTokens { get; set; } = new();

private ILog Logger = LogManager.GetLogger(typeof(Bot));

public Bot(BotInformation info, Dictionary<string, string> applicationTokens)
public Bot(BotInformation info, Dictionary<string, string> applicationTokens) : base(info)
{
if (info is null) throw new ArgumentNullException(nameof(info));
if (applicationTokens is null) throw new ArgumentException(nameof(applicationTokens));

ApplicationTokens = applicationTokens;
Information = info;

DiscordClient = new DiscordSocketClient(new DiscordSocketConfig()
{
Expand All @@ -39,11 +25,10 @@ public Bot(BotInformation info, Dictionary<string, string> applicationTokens)

public void InitDataProviders()
{
DataProviders.Add((int)DataProvider.STEAM, new SteamProvider());
DataProviders.Add((int)DataProvider.CFX, new CFXProvider());
DataProviders.Add((int)DataProvider.SCUM, new ScumProvider());
DataProviders.Add((int)DataProvider.MINECRAFT, new MinecraftProvider());
DataProviders.Add((int)DataProvider.BATTLEMETRICS, new BattleMetricsProvider());
DataProviders.Add((int)DataProvider.STEAM, new SteamProvider(Information));
DataProviders.Add((int)DataProvider.CFX, new CFXProvider(Information));
DataProviders.Add((int)DataProvider.MINECRAFT, new MinecraftProvider(Information));
DataProviders.Add((int)DataProvider.BATTLEMETRICS, new BattleMetricsProvider(Information));
}

public async Task StartAsync(bool shouldStart)
Expand All @@ -53,7 +38,7 @@ public async Task StartAsync(bool shouldStart)
Information.Address = await AddressHelper.ResolveAddress(Information.Address);
}

Logger.Info($"[Bot] - Loaded {Information.Name} at address and port: {Information.Address}, {Information.ProviderType}");
Info($"Loaded {Information.Name} at address and port: {Information.Address}, {Information.ProviderType}");
await DiscordClient.LoginAndStartAsync(Information.Token, Information.Address, shouldStart);
}

Expand All @@ -68,7 +53,14 @@ public async Task UpdateAsync()

if (dataProviderType != Information.ProviderType)
{
Logger.Warn($"[Bot] - Config for bot at address: {Information.Address} has an invalid provider type: {Information.ProviderType}");
Warn($"Config for bot at address: {Information.Address} has an invalid provider type: {Information.ProviderType}");
}

var activityInteger = EnumHelper.GetActivityType(Information.Status);

if (Information.Status != activityInteger)
{
Warn($"Config for bot at address: {Information.Address} has an invalid activity type: {Information.Status}");
}

var dataProvider = DataProviders[dataProviderType];
Expand All @@ -79,16 +71,9 @@ public async Task UpdateAsync()
return;
}

var gameStatus = serverInformation.GetStatusString(Information.Name, Information.UseNameAsLabel);
var activityInteger = EnumHelper.GetActivityType(Information.Status);

if (Information.Status != activityInteger)
{
Logger.Warn($"[Bot] - Config for bot at address: {Information.Address} has an invalid activity type: {Information.Status}");
}
var gameStatus = serverInformation.ReplaceTagsWithValues(Information.StatusFormat, Information.UseNameAsLabel, Information.Name);

var activityType = (ActivityType)(activityInteger);
await DiscordClient.SetGameAsync(gameStatus, null, activityType);
await DiscordClient.SetGameAsync(gameStatus, null, (ActivityType)activityInteger);
await DiscordClient.SetChannelName(Information.ChannelID, gameStatus);
}
}
Expand Down
13 changes: 2 additions & 11 deletions DiscordPlayerCountBot/Bot/BotConfig.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,14 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace PlayerCountBot
namespace PlayerCountBot
{
public class BotConfig
{
[JsonProperty]
public int UpdateTime { get; set; }

[JsonProperty]
public List<BotInformation> ServerInformation { get; set; } = new();

[JsonProperty]
public Dictionary<string, string> ApplicationTokens { get; set; } = new();

public void CreateDefaults()
{

ServerInformation.Add(new BotInformation()
ServerInformation.Add(new()
{
Name = "TestBot",
Address = "127.0.0.1:27014",
Expand Down
23 changes: 4 additions & 19 deletions DiscordPlayerCountBot/Bot/BotInformation.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,17 @@
using log4net.Repository.Hierarchy;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using System;

namespace PlayerCountBot
namespace PlayerCountBot
{
public class BotInformation
{
[JsonProperty]
[JsonIgnore]
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; }

[JsonProperty]
public string Address { get; set; }

[JsonProperty]
public string Token { get; set; }

[JsonProperty]
public int Status { get; set; }

[JsonProperty]
public bool UseNameAsLabel { get; set; }

[JsonProperty]
public int ProviderType { get; set; } = 0;

[JsonProperty]
public ulong? ChannelID { get; set; }
public string? StatusFormat { get; set; }

public Tuple<string, ushort> GetAddressAndPort()
{
Expand Down
35 changes: 0 additions & 35 deletions DiscordPlayerCountBot/Bot/GenericServerInformation.cs

This file was deleted.

7 changes: 1 addition & 6 deletions DiscordPlayerCountBot/Configuration/Base/IConfigurable.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using PlayerCountBot;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace DiscordPlayerCountBot.Configuration.Base
namespace PlayerCountBot.Configuration.Base
{
public interface IConfigurable
{
Expand Down
Loading

0 comments on commit 3171a98

Please sign in to comment.