Skip to content

Commit

Permalink
DSharpPlus v4.1 & HttpServer (#193)
Browse files Browse the repository at this point in the history
* Migrate to DSharpPlus v4.1

* Fix 'Incorrect Function' error with HttpServer causing it to crash frequently.
  • Loading branch information
versx authored Jan 11, 2022
1 parent f326996 commit 0d14987
Show file tree
Hide file tree
Showing 26 changed files with 533 additions and 575 deletions.
157 changes: 52 additions & 105 deletions src/Bot.cs

Large diffs are not rendered by default.

80 changes: 37 additions & 43 deletions src/Commands/Areas.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity;

using WhMgr.Diagnostics;
using WhMgr.Extensions;
using WhMgr.Localization;
using WhMgr.Net.Webhooks;

namespace WhMgr.Commands
namespace WhMgr.Commands
{
public class Areas
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;

using WhMgr.Configuration;
using WhMgr.Diagnostics;
using WhMgr.Extensions;
using WhMgr.Net.Webhooks;

public class Areas : BaseCommandModule
{
private static readonly IEventLogger _logger = EventLogger.GetLogger("AREAS", Program.LogLevel);
private readonly WhConfigHolder _config;
private readonly WebhookController _whm;

private readonly Dependencies _dep;

public Areas(Dependencies dep)
public Areas(WhConfigHolder config, WebhookController whm)
{
_dep = dep;
_config = config;
_whm = whm;

}

Expand All @@ -37,26 +37,23 @@ public Areas(Dependencies dep)
]
public async Task SendPaginated(CommandContext ctx)
{
if (!await ctx.IsDirectMessageSupported(_dep.WhConfig))
if (!await ctx.IsDirectMessageSupported(_config.Instance))
return;

var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _dep.WhConfig.Servers.ContainsKey(x));
if (!_dep.WhConfig.Servers.ContainsKey(guildId))
var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _config.Instance.Servers.ContainsKey(x));
if (!_config.Instance.Servers.ContainsKey(guildId))
return;

var server = _dep.WhConfig.Servers[guildId];

var geofences = _dep.Whm.GetServerGeofences(guildId);
var server = _config.Instance.Servers[guildId];
var geofences = _whm.GetServerGeofences(guildId);
var areas = geofences.Select(geofence => geofence.Name).OrderBy(Name => Name).ToList();

List<string> areas = geofences.Select(geofence => geofence.Name).OrderBy(Name => Name).ToList();


var interactivity = ctx.Client.GetInteractivityModule();
List<Page> pages = new List<Page>();
int pagelength = 0;
var interactivity = ctx.Client.GetInteractivity();
var pages = new List<Page>();
var pagelength = 0;
var psb = new StringBuilder();
int linesThisPage = 0;
int num = 1;
var linesThisPage = 0;
var num = 1;
var title = string.Format("Page {0}", (object)num);
foreach (var line in areas.Select(area => $"- {area}\n"))
{
Expand Down Expand Up @@ -97,10 +94,7 @@ public async Task SendPaginated(CommandContext ctx)
pages.Add(new Page { Embed = eb });
}

await interactivity.SendPaginatedMessage(ctx.Channel, ctx.User, pages, timeoutoverride: TimeSpan.FromMinutes(5));
await interactivity.SendPaginatedMessageAsync(ctx.Channel, ctx.User, pages);//, timeoutoverride: TimeSpan.FromMinutes(5));
}

}


}
}
37 changes: 0 additions & 37 deletions src/Commands/Dependencies.cs

This file was deleted.

25 changes: 13 additions & 12 deletions src/Commands/Event.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using DSharpPlus.Entities;
using ServiceStack.OrmLite;

using WhMgr.Configuration;
using WhMgr.Data;
using WhMgr.Diagnostics;
using WhMgr.Extensions;
Expand All @@ -23,15 +24,15 @@
Hidden,
RequirePermissions(Permissions.KickMembers)
]
public class Event
public class Event : BaseCommandModule
{
private static readonly IEventLogger _logger = EventLogger.GetLogger("COMMUNITYDAY", Program.LogLevel);

private readonly Dependencies _dep;
private readonly WhConfigHolder _config;

public Event(Dependencies dep)
public Event(WhConfigHolder config)
{
_dep = dep;
_config = config;
}

[
Expand All @@ -53,9 +54,9 @@ public async Task ListAsync(CommandContext ctx)
}
};
var pkmnNames = new List<string>();
for (var i = 0; i < _dep.WhConfig.EventPokemonIds.Count; i++)
for (var i = 0; i < _config.Instance.EventPokemonIds.Count; i++)
{
var pkmnId = _dep.WhConfig.EventPokemonIds[i];
var pkmnId = _config.Instance.EventPokemonIds[i];
if (MasterFile.Instance.Pokedex.ContainsKey(pkmnId))
{
pkmnNames.Add(pkmnId + ":" + MasterFile.Instance.Pokedex[pkmnId].Name);
Expand Down Expand Up @@ -89,8 +90,8 @@ public async Task SetAsync(CommandContext ctx,
pkmnFailed.Add(eventPokemonId);
}

_dep.WhConfig.EventPokemonIds = pkmnToAdd;
_dep.WhConfig.Save(_dep.WhConfig.FileName);
_config.Instance.EventPokemonIds = pkmnToAdd;
_config.Instance.Save(_config.Instance.FileName);

var pkmnNames = new List<string>();
for (var i = 0; i < pkmnToAdd.Count; i++)
Expand Down Expand Up @@ -141,8 +142,8 @@ public async Task AddAsync(CommandContext ctx,
pkmnFailed.Add(eventPokemonId);
}

_dep.WhConfig.EventPokemonIds.AddRange(pkmnToAdd);
_dep.WhConfig.Save(_dep.WhConfig.FileName);
_config.Instance.EventPokemonIds.AddRange(pkmnToAdd);
_config.Instance.Save(_config.Instance.FileName);

var pkmnNames = new List<string>();
for (var i = 0; i < pkmnToAdd.Count; i++)
Expand Down Expand Up @@ -193,8 +194,8 @@ public async Task RemoveAsync(CommandContext ctx,
pkmnFailed.Add(eventPokemonId);
}

pkmnToRemove.ForEach(x => _dep.WhConfig.EventPokemonIds.Remove(x));
_dep.WhConfig.Save(_dep.WhConfig.FileName);
pkmnToRemove.ForEach(x => _config.Instance.EventPokemonIds.Remove(x));
_config.Instance.Save(_config.Instance.FileName);

var pkmnNames = new List<string>();
for (var i = 0; i < pkmnToRemove.Count; i++)
Expand Down
59 changes: 30 additions & 29 deletions src/Commands/Feeds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;

using WhMgr.Configuration;
using WhMgr.Diagnostics;
using WhMgr.Extensions;
using WhMgr.Localization;

public class Feeds
public class Feeds : BaseCommandModule
{
private static readonly IEventLogger _logger = EventLogger.GetLogger("FEEDS", Program.LogLevel);

private readonly Dependencies _dep;
private readonly WhConfigHolder _config;

public Feeds(Dependencies dep)
public Feeds(WhConfigHolder config)
{
_dep = dep;
_config = config;
}

[
Expand All @@ -33,14 +34,14 @@ public Feeds(Dependencies dep)
]
public async Task FeedsAsync(CommandContext ctx)
{
if (!await ctx.IsDirectMessageSupported(_dep.WhConfig))
if (!await ctx.IsDirectMessageSupported(_config.Instance))
return;

var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _dep.WhConfig.Servers.ContainsKey(x));
if (!_dep.WhConfig.Servers.ContainsKey(guildId))
var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _config.Instance.Servers.ContainsKey(x));
if (!_config.Instance.Servers.ContainsKey(guildId))
return;

var server = _dep.WhConfig.Servers[guildId];
var server = _config.Instance.Servers[guildId];
var cityRoles = server.Geofences.Select(x => x.Name).ToList();
cityRoles.Sort();
var sb = new StringBuilder();
Expand Down Expand Up @@ -73,15 +74,15 @@ public async Task FeedsAsync(CommandContext ctx)
public async Task FeedMeAsync(CommandContext ctx,
[Description("City name to join or all."), RemainingText] string cityName = null)
{
if (!await ctx.IsDirectMessageSupported(_dep.WhConfig))
if (!await ctx.IsDirectMessageSupported(_config.Instance))
return;

var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _dep.WhConfig.Servers.ContainsKey(x));
if (!_dep.WhConfig.Servers.ContainsKey(guildId))
var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _config.Instance.Servers.ContainsKey(x));
if (!_config.Instance.Servers.ContainsKey(guildId))
return;

var server = _dep.WhConfig.Servers[guildId];
var isSupporter = await ctx.Client.IsSupporterOrHigher(ctx.User.Id, guildId, _dep.WhConfig);
var server = _config.Instance.Servers[guildId];
var isSupporter = await ctx.Client.IsSupporterOrHigher(ctx.User.Id, guildId, _config.Instance);
var isFreeRole = string.IsNullOrEmpty(server.FreeRoleName) ? false : string.Compare(cityName, server.FreeRoleName, true) == 0;
if (server.CitiesRequireSupporterRole && !isSupporter && !isFreeRole)
{
Expand Down Expand Up @@ -110,7 +111,7 @@ public async Task FeedMeAsync(CommandContext ctx,
await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME_TYPE_COMMAND").FormatText(new
{
author = ctx.User.Username,
city = city,
city,
prefix = server.CommandPrefix,
}), DiscordColor.Red);
continue;
Expand All @@ -122,7 +123,7 @@ await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME_TY
await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME").FormatText(new
{
author = ctx.User.Username,
city = city,
city,
}), DiscordColor.Red);
continue;
}
Expand All @@ -142,7 +143,7 @@ await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME").

if (assigned.Count == 0 && alreadyAssigned.Count == 0)
{
ctx.Client.DebugLogger.LogMessage(DSharpPlus.LogLevel.Debug, "Feeds", $"No roles assigned or already assigned for user {ctx.User.Username} ({ctx.User.Id}). Value: {string.Join(", ", cityNames)}", DateTime.Now);
_logger.Error($"No roles assigned or already assigned for user {ctx.User.Username} ({ctx.User.Id}). Value: {string.Join(", ", cityNames)}");
return;
}

Expand Down Expand Up @@ -177,15 +178,15 @@ await ctx.RespondEmbed
public async Task FeedMeNotAsync(CommandContext ctx,
[Description("City name to leave or all."), RemainingText] string cityName)
{
if (!await ctx.IsDirectMessageSupported(_dep.WhConfig))
if (!await ctx.IsDirectMessageSupported(_config.Instance))
return;

var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _dep.WhConfig.Servers.ContainsKey(x));
if (!_dep.WhConfig.Servers.ContainsKey(guildId))
var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _config.Instance.Servers.ContainsKey(x));
if (!_config.Instance.Servers.ContainsKey(guildId))
return;

var server = _dep.WhConfig.Servers[guildId];
var isSupporter = await ctx.Client.IsSupporterOrHigher(ctx.User.Id, guildId, _dep.WhConfig);
var server = _config.Instance.Servers[guildId];
var isSupporter = await ctx.Client.IsSupporterOrHigher(ctx.User.Id, guildId, _config.Instance);
var isFreeRole = string.IsNullOrEmpty(server.FreeRoleName) ? false : string.Compare(cityName, server.FreeRoleName, true) == 0;
if (server.CitiesRequireSupporterRole && !isSupporter && !isFreeRole)
{
Expand Down Expand Up @@ -214,7 +215,7 @@ public async Task FeedMeNotAsync(CommandContext ctx,
await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME_TYPE_COMMAND").FormatText(new
{
author = ctx.User.Username,
city = city,
city,
server.CommandPrefix,
}), DiscordColor.Red);
continue;
Expand All @@ -226,7 +227,7 @@ await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME_TY
await ctx.RespondEmbed(Translator.Instance.Translate("FEEDS_INVALID_CITY_NAME").FormatText(new
{
author = ctx.User.Username,
city = city,
city,
}), DiscordColor.Red);
continue;
}
Expand Down Expand Up @@ -269,17 +270,17 @@ await ctx.RespondEmbed

private async Task AssignAllDefaultFeedRoles(CommandContext ctx)
{
var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _dep.WhConfig.Servers.ContainsKey(x));
var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _config.Instance.Servers.ContainsKey(x));

if (_dep.WhConfig.Servers[guildId].Geofences == null)
if (_config.Instance.Servers[guildId].Geofences == null)
{
_logger.Warn($"City roles empty.");
return;
}

try
{
var server = _dep.WhConfig.Servers[guildId];
var server = _config.Instance.Servers[guildId];
var areas = server.Geofences.Select(x => x.Name).ToList();
for (var i = 0; i < areas.Count; i++)
{
Expand Down Expand Up @@ -312,17 +313,17 @@ private async Task AssignAllDefaultFeedRoles(CommandContext ctx)

private async Task RemoveAllDefaultFeedRoles(CommandContext ctx)
{
var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _dep.WhConfig.Servers.ContainsKey(x));
var guildId = ctx.Guild?.Id ?? ctx.Client.Guilds.Keys.FirstOrDefault(x => _config.Instance.Servers.ContainsKey(x));

if (_dep.WhConfig.Servers[guildId].Geofences == null)
if (_config.Instance.Servers[guildId].Geofences == null)
{
_logger.Warn($"City roles empty.");
return;
}

try
{
var server = _dep.WhConfig.Servers[guildId];
var server = _config.Instance.Servers[guildId];
var areas = server.Geofences.Select(x => x.Name).ToList();
for (var i = 0; i < areas.Count; i++)
{
Expand Down
Loading

0 comments on commit 0d14987

Please sign in to comment.