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

Commit

Permalink
Change logs on tag releases version 1.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioZumbi12 committed May 20, 2018
1 parent 7613934 commit 500b36d
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 28 deletions.
90 changes: 69 additions & 21 deletions SEDiscordBridge/DiscordBridge.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using DSharpPlus;
using DSharpPlus.Entities;
using NLog.Fluent;
using Sandbox.Game.Multiplayer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Torch.API;
using Torch.API.Managers;
using Torch.Commands;
using VRage.Game;
Expand Down Expand Up @@ -126,12 +128,46 @@ private Task Discord_MessageCreated(DSharpPlus.EventArgs.MessageCreateEventArgs
}
if (e.Channel.Id.Equals(ulong.Parse(Plugin.Config.CommandChannelId)) && e.Message.Content.StartsWith(Plugin.Config.CommandPrefix))
{
string cmd = e.Message.Content.Replace(Plugin.Config.CommandPrefix, "");
Plugin.Torch.Invoke(() =>
{
var manager = Plugin.Torch.CurrentSession.Managers.GetManager<CommandManager>();
manager.HandleCommandFromServer(cmd);
});
string cmd = e.Message.Content.Substring(Plugin.Config.CommandPrefix.Length);
var cmdText = new string(cmd.Skip(1).ToArray());
DiscordMessage dms = null;

if (Plugin.Torch.GameState != TorchGameState.Loaded)
{
dms = discord.SendMessageAsync(e.Channel, "Error: Server is not running.").Result;
}
else
{
var manager = Plugin.Torch.CurrentSession.Managers.GetManager<CommandManager>();
var command = manager.Commands.GetCommand(cmdText, out string argText);

if (command == null)
{
dms = discord.SendMessageAsync(e.Channel, "R: Command not found: " + cmdText).Result;
}
else
{
var cmdPath = string.Join(".", command.Path);
var splitArgs = Regex.Matches(argText, "(\"[^\"]+\"|\\S+)").Cast<Match>().Select(x => x.ToString().Replace("\"", "")).ToList();
Plugin.Log.Trace($"Invoking {cmdPath} for server.");

var context = new SEDBCommandHandler(Plugin.Torch, command.Plugin, Sync.MyId, argText, splitArgs);
if (command.TryInvoke(context))
{
if (context.Response.ToString().Length > 0)
dms = discord.SendMessageAsync(e.Channel, "R: " + context.Response.ToString()).Result;
Plugin.Log.Info($"Server ran command '{cmd}'");
}
else
{
dms = discord.SendMessageAsync(e.Channel, "R: Error executing command: " + cmdText).Result;
}
}
}
Task.Delay(10000).ContinueWith(t => dms?.DeleteAsync());
});
}
}
return Task.CompletedTask;
Expand All @@ -142,23 +178,35 @@ private string MentionNameToID(string msg, DiscordChannel chann)
var parts = msg.Split(' ');
foreach (string part in parts)
{
string name = Regex.Replace(part.Substring(1), @"[,#]", "");
if (part.StartsWith("@") && String.Compare(name, "everyone", true) == 0 && !Plugin.Config.MentEveryone)
if (part.Length > 2)
{
msg = msg.Replace(part, part.Substring(1));
continue;
}

var members = chann.Guild.GetAllMembersAsync().Result;

if (!Plugin.Config.MentOthers)
{
continue;
}
if (part.StartsWith("@") && members.Any(u => String.Compare(u.Username, name, true) == 0))
{
msg = msg.Replace(part, "<@" + members.Where(u => String.Compare(u.Username, name, true) == 0).First().Id + ">");
}
if (part.StartsWith("@"))
{
string name = Regex.Replace(part.Substring(1), @"[,#]", "");
if (String.Compare(name, "everyone", true) == 0 && !Plugin.Config.MentEveryone)
{
msg = msg.Replace(part, part.Substring(1));
continue;
}

var members = chann.Guild.GetAllMembersAsync().Result;

if (!Plugin.Config.MentOthers)
{
continue;
}
if (members.Any(u => String.Compare(u.Username, name, true) == 0))
{
msg = msg.Replace(part, "<@" + members.Where(u => String.Compare(u.Username, name, true) == 0).First().Id + ">");
}
}

var emojis = chann.Guild.Emojis;
if (part.StartsWith(":") && part.EndsWith(":") && emojis.Any(e => String.Compare(e.GetDiscordName(), part, true) == 0))
{
msg = msg.Replace(part, "<"+ part + emojis.Where(e => String.Compare(e.GetDiscordName(), part, true) == 0).First().Id + ">");
}
}
}
return msg;
}
Expand Down
22 changes: 22 additions & 0 deletions SEDiscordBridge/SEDBCommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections.Generic;
using System.Text;
using Torch.API;
using Torch.API.Plugins;
using Torch.Commands;

namespace SEDiscordBridge
{
public class SEDBCommandHandler : CommandContext
{
/// <inheritdoc />
public SEDBCommandHandler(ITorchBase torch, ITorchPlugin plugin, ulong steamIdSender, string rawArgs = null, List<string> args = null) :
base(torch, plugin, steamIdSender, rawArgs, args) { }

public StringBuilder Response { get; } = new StringBuilder();

public override void Respond(string message, string sender = "Server", string font = "Blue")
{
Response.AppendLine(message);
}
}
}
3 changes: 3 additions & 0 deletions SEDiscordBridge/SEDBConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,8 @@ public class SEDBConfig : ViewModel

private bool _mentionEveryone = false;
public bool MentEveryone { get => _mentionEveryone; set => SetValue(ref _mentionEveryone, value); }

private string _tokenVisibleState = "Visible";
public string TokenVisibleState { get => _tokenVisibleState; set => SetValue(ref _tokenVisibleState, value); }
}
}
13 changes: 10 additions & 3 deletions SEDiscordBridge/SEDBControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@
Click here to setup your BOT to get your bot token!
</Hyperlink>
</Label>
<Label Content="*After setup the token, save and restart the torch" />
<StackPanel Orientation="Horizontal">
<TextBox Width="420" Text="{Binding BotToken}" ToolTip="You need to create a discord bot to get the token" Margin="3" />
<Label Content="*After setup the token, save and restart the torch." />

<StackPanel Orientation="Horizontal" Visibility="{Binding TokenVisibleState}">
<TextBox Width="420" Text="{Binding BotToken}" ToolTip="You need to create a discord bot to get the token" Margin="3"/>
<Label Content="Discord Bot Token"/>
</StackPanel>
<Label>
<TextBlock>
*If the token is not visible here, you need to enable or change directly on config file.
<LineBreak/>Change between 'Visible' and 'Collapsed'.
</TextBlock>
</Label>
<StackPanel Orientation="Horizontal">
<TextBox Width="180" Text="{Binding ChatChannelId}" ToolTip="Channel ID to send/receive messages from/to in-game/discord" Margin="3" />
<Label Content="Channel id for chat messages"/>
Expand Down
10 changes: 6 additions & 4 deletions SEDiscordBridge/SEDicordBridgePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,17 @@ private void SessionChanged(ITorchSession session, TorchSessionState state)
{
_multibase.PlayerJoined += _multibase_PlayerJoined;
MyEntities.OnEntityAdd += MyEntities_OnEntityAdd;
_multibase.PlayerLeft += _multibase_PlayerLeft;
_multibase.PlayerLeft += _multibase_PlayerLeft;
}
else
Log.Warn("No join/leave manager loaded!");

_chatmanager = Torch.CurrentSession.Managers.GetManager<ChatManagerServer>();
if (_chatmanager != null)
_chatmanager.MessageRecieved += MessageRecieved;
{
_chatmanager.MessageRecieved += MessageRecieved;
}

else
Log.Warn("No chat manager loaded!");

Expand Down Expand Up @@ -168,8 +171,7 @@ private void _multibase_PlayerJoined(IPlayer obj)
if (Config.Connect.Length > 0)
{
DDBridge.SendStatusMessage(obj.Name, Config.Connect);
}

}
}

private void MyEntities_OnEntityAdd(VRage.Game.Entity.MyEntity obj)
Expand Down
1 change: 1 addition & 0 deletions SEDiscordBridge/SEDiscordBridge.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<Compile Include="SEDBCommandHandler.cs" />
<Compile Include="DiscordBridge.cs" />
<Compile Include="SEDicordBridgePlugin.cs" />
<Page Include="SEDBControl.xaml">
Expand Down

0 comments on commit 500b36d

Please sign in to comment.