Skip to content

Commit

Permalink
Add Areas command (#108)
Browse files Browse the repository at this point in the history
* Add Area command

* Enable areas command if EnableCities is false

* remove unused config option

* change embed color

Co-authored-by: Thunder <[email protected]>
  • Loading branch information
thunder123456 and Thunder authored Dec 23, 2020
1 parent a047916 commit fecdee6
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ public Bot(WhConfigHolder whConfig)
{
commands.RegisterCommands<Feeds>();
}
else
{
commands.RegisterCommands<Areas>();
}

_logger.Info($"Configured Discord server {guildId}");
if (!_servers.ContainsKey(guildId))
Expand Down
106 changes: 106 additions & 0 deletions src/Commands/Areas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
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
{
public class Areas
{
private static readonly IEventLogger _logger = EventLogger.GetLogger("AREAS", Program.LogLevel);

private readonly Dependencies _dep;

public Areas(Dependencies dep)
{
_dep = dep;

}

[
Command("areas"),
//Aliases("", ""),
Description("Shows a list of areas")
]
public async Task SendPaginated(CommandContext ctx)
{
if (!await ctx.IsDirectMessageSupported(_dep.WhConfig))
return;

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

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

var geofences = _dep.Whm.GetServerGeofences(guildId);

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 psb = new StringBuilder();
int linesThisPage = 0;
int num = 1;
var title = string.Format("Page {0}", (object)num);
foreach (var line in areas.Select(area => $"- {area}\n"))
{
var length = line.Length;
var wouldGoOver = length + pagelength > 2000;

if (wouldGoOver || linesThisPage >= 25)
{
title = string.Format("Page {0}", (object)num);
var eb = new DiscordEmbedBuilder
{
Color = DiscordColor.Blue,
Title = title,
Description = psb.ToString(),
};
pages.Add(new Page { Embed = eb });
psb.Clear();
pagelength = 0;
linesThisPage = 0;
++num;
}

psb.Append(line);
pagelength += length;
linesThisPage++;
}

if (psb.Length > 0)
{
title = string.Format("Page {0}", (object)num);
var eb = new DiscordEmbedBuilder
{
Color = DiscordColor.Red,
Title = title,
Description = psb.ToString(),

};
pages.Add(new Page { Embed = eb });
}

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

}


}

0 comments on commit fecdee6

Please sign in to comment.