Skip to content

Commit

Permalink
Refactor, pull requests, and other changes
Browse files Browse the repository at this point in the history
- Refactor namespaces, etc
- Move a few classes
- Rename MyCommandContext to BotCommandContext
- Add new precondition: RequireTrustedMemberAttribute
- Add classes, methods, etc related to github pull requests
  • Loading branch information
SubZero0 committed Jun 8, 2020
1 parent 8c4746a commit b60690b
Show file tree
Hide file tree
Showing 29 changed files with 320 additions and 119 deletions.
3 changes: 1 addition & 2 deletions src/DiscordNet/Controllers/MainController.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Discord.WebSocket;
using DiscordNet.Github;
using DiscordNet.Handlers;
using System;
using System.Threading.Tasks;

namespace DiscordNet.Controllers
namespace DiscordNet
{
public class MainController
{
Expand Down
3 changes: 1 addition & 2 deletions src/DiscordNet/DiscordNet.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Discord;
using Discord.WebSocket;
using DiscordNet.Controllers;
using DiscordNet.Github;
using Microsoft.Extensions.DependencyInjection;
using Paginator;
Expand Down Expand Up @@ -56,7 +55,7 @@ Task ctsTask()
}

_client.Connected += ctsTask;
await Task.Delay(30000, cts.Token);
try { await Task.Delay(30000, cts.Token); } catch { }
if (!cts.IsCancellationRequested)
Environment.Exit(1);
_client.Connected -= ctsTask;
Expand Down
29 changes: 12 additions & 17 deletions src/DiscordNet/Handlers/CommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using DiscordNet.Controllers;
using DiscordNet.EmbedExtension;
using DiscordNet.Modules.Addons;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Paginator;
Expand All @@ -13,15 +10,15 @@
using System.Text;
using System.Threading.Tasks;

namespace DiscordNet.Handlers
namespace DiscordNet
{
public class CommandHandler
{
private CommandService _commandService;
private DiscordSocketClient _client;
private IServiceProvider _services;
private MainController _mainHandler;
private MemoryCache _cache = new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(3) });
private readonly MemoryCache _cache = new MemoryCache(new MemoryCacheOptions { ExpirationScanFrequency = TimeSpan.FromMinutes(3) });

public async Task InitializeAsync(MainController MainHandler, IServiceProvider services)
{
Expand Down Expand Up @@ -92,10 +89,13 @@ private Task HandleUpdate(Cacheable<IMessage, ulong> before, SocketMessage after
public Task HandleCommand(SocketMessage parameterMessage)
{
var msg = parameterMessage as SocketUserMessage;
if (msg == null) return Task.CompletedTask;
if (msg.Author.IsBot) return Task.CompletedTask;
if (msg.Channel is ITextChannel tc && tc.GuildId == 81384788765712384)
if (msg.Channel.Name != "dotnet_discord-net" && msg.Channel.Name != "testing" && msg.Channel.Name != "playground") return Task.CompletedTask;
if (msg == null || msg.Author.IsBot)
return Task.CompletedTask;

if (msg.Channel is ITextChannel textChannel && textChannel.GuildId == 81384788765712384)
if (msg.Channel.Name != "dotnet_discord-net" && msg.Channel.Name != "testing" && msg.Channel.Name != "playground")
return Task.CompletedTask;

int argPos = 0;
if (!(msg.HasMentionPrefix(_client.CurrentUser, ref argPos) /*|| msg.HasStringPrefix(MainHandler.Prefix, ref argPos)*/)) return Task.CompletedTask;
_ = HandleCommandAsync(msg, argPos);
Expand All @@ -117,26 +117,21 @@ public async Task HandleCommandAsync(SocketUserMessage msg, int argPos)

private async Task<(string, EmbedBuilder, PaginatedMessage)> BuildReply(IUserMessage msg, string message)
{
var context = new MyCommandContext(_client, _mainHandler, msg);
var context = new BotCommandContext(_client, _mainHandler, msg);
var result = await _commandService.ExecuteAsync(context, message, _services);
if (!result.IsSuccess && result.Error != CommandError.UnknownCommand)
return (result.ErrorReason, null, null);
else if (!result.IsSuccess)
{
if (!_mainHandler.QueryHandler.IsReady())
{
return ("Loading cache...", null, null); //TODO: Change message
}
return ("Loading cache, please wait a few moments before trying again.", null, null);
else
{
try
{
var tuple = await _mainHandler.QueryHandler.RunAsync(message);
if (tuple.Item2 is PaginatorBuilder pag)
{
var paginated = new PaginatedMessage(pag.Pages, PaginatedMessageActions.Simplified, "Results", user: msg.Author, options: new AppearanceOptions { TimeoutAfterLastAction = TimeSpan.FromMinutes(3) });
return (null, null, paginated);
}
return (null, null, new PaginatedMessage(pag.Pages, PaginatedMessageActions.Simplified, "Results", user: msg.Author, options: new AppearanceOptions { TimeoutAfterLastAction = TimeSpan.FromMinutes(3) }));
else
return (tuple.Item1, tuple.Item2, null);
}
Expand Down
5 changes: 2 additions & 3 deletions src/DiscordNet/Handlers/DocsUrlHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace DiscordNet.Handlers
namespace DiscordNet
{
public class DocsUrlHandler
{
Expand All @@ -9,8 +9,7 @@ public class DocsUrlHandler
private string[] _docsUrls =
{
"https://docs.stillu.cc/",
"https://discord.foxbot.me/latest/",
"http://discord.devpaulo.com.br/"
"https://discord.foxbot.me/latest/"
};

public bool CheckAvailability()
Expand Down
3 changes: 1 addition & 2 deletions src/DiscordNet/Handlers/QueryHandler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Discord;
using DiscordNet.Github;
using DiscordNet.Query;
using DiscordNet.Query.Results;
using System.Threading.Tasks;

namespace DiscordNet.Handlers
namespace DiscordNet
{
public class QueryHandler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
using Discord;
using Discord.Commands;
using DiscordNet.Controllers;

namespace DiscordNet.Modules.Addons
{
public class MyCommandContext : ICommandContext
{
public IDiscordClient Client { get; }
public IGuild Guild { get; }
public MainController MainController { get; }
public IMessageChannel Channel { get; }
public IUser User { get; }
public IUserMessage Message { get; }

public bool IsPrivate => Channel is IPrivateChannel;

public MyCommandContext(IDiscordClient client, MainController controller, IUserMessage msg)
{
Client = client;
Guild = (msg.Channel as IGuildChannel)?.Guild;
Channel = msg.Channel;
User = msg.Author;
Message = msg;
MainController = controller;
}
}
}
using Discord;
using Discord.Commands;
using Discord.WebSocket;

namespace DiscordNet
{
public class BotCommandContext : ICommandContext
{
public DiscordSocketClient Client { get; }
public SocketGuild Guild { get; }
public MainController MainController { get; }
public IMessageChannel Channel { get; }
public IUser User { get; }
public IUserMessage Message { get; }

public bool IsPrivate => Channel is IPrivateChannel;

public BotCommandContext(DiscordSocketClient client, MainController controller, IUserMessage msg)
{
Client = client;
Guild = (msg.Channel as SocketGuildChannel)?.Guild;
Channel = msg.Channel;
User = msg.Author;
Message = msg;
MainController = controller;
}

IDiscordClient ICommandContext.Client => Client;
IGuild ICommandContext.Guild => Guild;
}
}
Loading

0 comments on commit b60690b

Please sign in to comment.