Skip to content
This repository has been archived by the owner on Dec 18, 2021. It is now read-only.

Commit

Permalink
Merge branch 'n-Ultima-feature/InlineTags' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikdemir committed Nov 26, 2021
2 parents c3eadaf + 437d01c commit b369d17
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion CWE/Services/CommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace CWE.Services
using System.Text.RegularExpressions;

namespace CWE.Services
{
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -290,6 +292,20 @@ private async Task HandleMessage(SocketMessage incomingMessage)
return;
}

if (message.Content.Contains("$"))
{
var content = Regex.Replace(message.Content, @"(`{1,3}).*?(.\1)", string.Empty, RegexOptions.Singleline);
content = Regex.Replace(content, "^>.*$", string.Empty, RegexOptions.Multiline);
if (!string.IsNullOrWhiteSpace(content))
{
var match = new Regex(@"\$(\S+)\b").Match(content);
if (match.Success)
{
await this.HandleTag(message, match);
}
}
}

int argPos = 0;
if (!message.HasStringPrefix(this.configuration["Prefix"], ref argPos) && !message.HasMentionPrefix(this.client.CurrentUser, ref argPos))
{
Expand All @@ -300,6 +316,25 @@ private async Task HandleMessage(SocketMessage incomingMessage)
await this.commandService.ExecuteAsync(context, argPos, this.provider);
}

private async Task HandleTag(SocketUserMessage message, Match regexMatch)
{
var tagName = regexMatch.Groups[1].Value;
if (string.IsNullOrWhiteSpace(tagName))
{
return;
}

using var scope = this.provider.CreateScope();
var dataAccessLayer = scope.ServiceProvider.GetRequiredService<DataAccessLayer>();
var tag = await dataAccessLayer.GetTag(tagName);
if (tag == null)
{
return;
}

await message.Channel.SendMessageAsync(tag.Content);
}

private async Task CommandExecutedAsync(Optional<CommandInfo> command, ICommandContext context, IResult result)
{
if (!command.IsSpecified || result.IsSuccess)
Expand Down

0 comments on commit b369d17

Please sign in to comment.