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

Commit

Permalink
Added connection message and option to use other channel for status m…
Browse files Browse the repository at this point in the history
…essages
  • Loading branch information
FabioZumbi12 committed May 12, 2018
1 parent 1a64505 commit 95ceb85
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 57 deletions.
32 changes: 20 additions & 12 deletions SEDiscordBridge/DiscordBridge.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using DSharpPlus;
using DSharpPlus.Entities;
using Sandbox.Game.World;
using System.Threading;
using System.Threading.Tasks;
using Torch;
using Torch.API.Managers;
using Torch.Commands;
using Torch.Server;
using VRage.Game;

namespace SEDiscordBridge
Expand Down Expand Up @@ -73,19 +70,30 @@ public void SendStatus(string status)
}
}

public void SendMessage(string user, string msg)
public void SendChatMessage(string user, string msg)
{
if (user != null)
{
string fmsg = Plugin.Config.Format.Replace("{p}", user).Replace("{msg}", msg);
discord.SendMessageAsync(discord.GetChannelAsync(ulong.Parse(Plugin.Config.ChannelId)).Result, fmsg);
}
else
{
if (Plugin.Config.ChannelId.Length > 0)
{
if (user != null)
{
msg = Plugin.Config.Format.Replace("{msg}", msg).Replace("{p}", user);
}
discord.SendMessageAsync(discord.GetChannelAsync(ulong.Parse(Plugin.Config.ChannelId)).Result, msg);
}
}

public void SendStatusMessage(string user, string msg)
{
if (Plugin.Config.StatusChannelId.Length > 0)
{
if (user != null)
{
msg = msg.Replace("{p}", user);
}
discord.SendMessageAsync(discord.GetChannelAsync(ulong.Parse(Plugin.Config.StatusChannelId)).Result, msg);
}
}

private Task Discord_MessageCreated(DSharpPlus.EventArgs.MessageCreateEventArgs e)
{
if (!e.Author.IsBot)
Expand Down
11 changes: 7 additions & 4 deletions SEDiscordBridge/SEDBConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;
using Torch;
using Torch;

namespace SEDiscordBridge
{
Expand Down Expand Up @@ -31,12 +28,18 @@ public class SEDBConfig : ViewModel
private string _serverName = "Discord";
public string ServerName { get => _serverName; set => SetValue(ref _serverName, value); }

private string _statusChannelID;
public string StatusChannelId { get => _statusChannelID; set => SetValue(ref _statusChannelID, value); }

private string _started = ":white_check_mark: Server Started!";
public string Started { get => _started; set => SetValue(ref _started, value); }

private string _stopped = ":x: Server Stopped!";
public string Stopped { get => _stopped; set => SetValue(ref _stopped, value); }

private string _connect = ":key: The player {p} connected to server";
public string Connect { get => _connect; set => SetValue(ref _connect, value); }

private string _join = ":sunny: The player {p} joined the server";
public string Join { get => _join; set => SetValue(ref _join, value); }

Expand Down
27 changes: 20 additions & 7 deletions SEDiscordBridge/SEDBControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SEDiscordBridge"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
d:DesignHeight="650" d:DesignWidth="800">
<StackPanel>
<Label FontWeight="Bold" FontSize="16" Content="Main Config:" />
<Label Width="Auto">
Expand Down Expand Up @@ -44,18 +43,27 @@
<TextBox Width="180" Text="{Binding CommandPrefix}" ToolTip="Prefix for Commands from Discord" Margin="3" />
<Label Content="Commands Prefix"/>
</StackPanel>
<Label FontWeight="Bold" FontSize="16" Content="Messages:" />

<Label FontWeight="Bold" FontSize="16" Content="Status Messages:" />
<StackPanel Orientation="Horizontal">
<TextBox Width="180" Text="{Binding StatusChannelId}" ToolTip="Discord channel to send status messages" Margin="3" />
<Label Content="Channel id for status messages"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="180" Text="{Binding Started}" ToolTip="Message to show on server started" Margin="3" />
<Label Content="Started Message"/>
<Label Content="Server Started Message"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="180" Text="{Binding Stopped}" ToolTip="Message to show on server stop" Margin="3" />
<Label Content="Stoped Message"/>
<Label Content="Server Stoped Message"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="180" Text="{Binding Connect}" Margin="3" ToolTip="Use {p} for player name. Leave empty to disable"/>
<Label Content="Player Conected Message" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="180" Text="{Binding Join}" Margin="3" ToolTip="Use {p} for player name. Leave empty to disable"/>
<Label Content="Player Join Message" />
<Label Content="Player Joined Message" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBox Width="180" Text="{Binding Leave}" Margin="3" ToolTip="Use {p} for player name. Leave empty to disable"/>
Expand All @@ -65,7 +73,7 @@
<Label FontWeight="Bold" FontSize="16" Content="BOT Status:" />
<CheckBox Content="Enable BOT Status" Margin="3" IsChecked="{Binding UseStatus}" />
<StackPanel Orientation="Horizontal">
<TextBox Width="180" Text="{Binding StatusInterval}" ToolTip="Interval to update the BOT status. 1000 ms = 1 seconds" Margin="3" />
<TextBox Width="180" Text="{Binding StatusInterval}" ToolTip="Interval to update the BOT status. 1000 ms = 1 second" Margin="3" />
<Label Content="Update Status Interval"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
Expand All @@ -75,5 +83,10 @@
<StackPanel Orientation="Horizontal">
<Button Width="180" Content="Save Config" Click="SaveConfig_OnClick" Margin="3" />
</StackPanel>
<Label Width="Auto">
<Hyperlink NavigateUri="https://goo.gl/CD7Yj8" RequestNavigate="Hyperlink_RequestNavigate">
Like the plugin? I'm accepting donations!
</Hyperlink>
</Label>
</StackPanel>
</UserControl>
17 changes: 3 additions & 14 deletions SEDiscordBridge/SEDBControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SEDiscordBridge
{
Expand All @@ -35,10 +24,10 @@ public SEDBControl(SEDicordBridgePlugin plugin) : this()
}

private void SaveConfig_OnClick(object sender, RoutedEventArgs e)
{
{
Plugin.Save();
Plugin.StopTimer();
Plugin.DDBridge.SendStatus(null);
Plugin.DDBridge?.SendStatus(null);
if (Plugin.Config.UseStatus)
{
Plugin.StartTimer();
Expand Down
74 changes: 54 additions & 20 deletions SEDiscordBridge/SEDicordBridgePlugin.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Controls;
using NLog;
using Sandbox.Game.Entities;
using Sandbox.Game.Entities.Character;
using Sandbox.Game.World;
using Torch;
using Torch.API;
Expand All @@ -18,25 +22,32 @@ namespace SEDiscordBridge
public sealed class SEDicordBridgePlugin : TorchPluginBase, IWpfPlugin
{
public SEDBConfig Config => _config?.Data;

private Persistent<SEDBConfig> _config;

public DiscordBridge DDBridge;

private UserControl _control;
private SEDBControl _control;
private TorchSessionManager _sessionManager;
private ChatManagerServer _chatmanager;
private IMultiplayerManagerBase _multibase;
private Timer _timer;
private TorchServer torchServer;
private HashSet<ulong> _conecting = new HashSet<ulong>();

public readonly Logger Log = LogManager.GetLogger("SEDicordBridge");

/// <inheritdoc />
public UserControl GetControl() => _control ?? (_control = new SEDBControl(this));

public void Save() => _config.Save();

/// <inheritdoc />
public override void Init(ITorchBase torch)
{
base.Init(torch);
torchServer = (TorchServer)torch;

try
{
_config = Persistent<SEDBConfig>.Load(Path.Combine(StoragePath, "SEDiscordBridge.cfg"));
Expand All @@ -46,7 +57,7 @@ public override void Init(ITorchBase torch)
}
if (_config?.Data == null)
_config = new Persistent<SEDBConfig>(Path.Combine(StoragePath, "SEDiscordBridge.cfg"), new SEDBConfig());

if (Config.BotToken.Length > 0)
{
_sessionManager = Torch.Managers.GetManager<TorchSessionManager>();
Expand All @@ -63,14 +74,9 @@ public override void Init(ITorchBase torch)
private void MessageRecieved(TorchChatMessage msg, ref bool consumed)
{
if (msg.AuthorSteamId != null)
DDBridge.SendMessage(msg.Author, msg.Message);
DDBridge.SendChatMessage(msg.Author, msg.Message);
}

public void Save()
{
_config.Save();
}


private void SessionChanged(ITorchSession session, TorchSessionState state)
{
switch (state)
Expand All @@ -80,6 +86,7 @@ private void SessionChanged(ITorchSession session, TorchSessionState state)
if (_multibase != null)
{
_multibase.PlayerJoined += _multibase_PlayerJoined;
MyEntities.OnEntityAdd += MyEntities_OnEntityAdd;
_multibase.PlayerLeft += _multibase_PlayerLeft;
}
else
Expand All @@ -106,7 +113,7 @@ private void SessionChanged(ITorchSession session, TorchSessionState state)
if (DDBridge != null)
{
if (Config.Stopped.Length > 0)
DDBridge.SendMessage(null, Config.Stopped);
DDBridge.SendStatusMessage(null, Config.Stopped);
DDBridge.Stopdiscord();
}
Log.Warn("Discord Bridge Unloaded!");
Expand Down Expand Up @@ -145,22 +152,54 @@ private void _timer_Elapsed(object sender, ElapsedEventArgs e)

private void _multibase_PlayerLeft(IPlayer obj)
{
//Remove to conecting list
_conecting.Remove(obj.SteamId);
if (Config.Leave.Length > 0)
DDBridge.SendMessage(null, Config.Leave.Replace("{p}", obj.Name));
{
DDBridge.SendStatusMessage(obj.Name, Config.Leave);
}

}

private void _multibase_PlayerJoined(IPlayer obj)
{
if (Config.Join.Length > 0)
DDBridge.SendMessage(null, Config.Join.Replace("{p}", obj.Name));
//Add to conecting list
_conecting.Add(obj.SteamId);
if (Config.Connect.Length > 0)
{
DDBridge.SendStatusMessage(obj.Name, Config.Connect);
}

}

/// <inheritdoc />
private void MyEntities_OnEntityAdd(VRage.Game.Entity.MyEntity obj)
{
if (obj is MyCharacter character)
{
Task.Run(() =>
{
System.Threading.Thread.Sleep(1000);
Torch.Invoke(() =>
{
if (_conecting.Contains(character.ControlSteamId) && character.IsPlayer && Config.Join.Length > 0)
{
DDBridge.SendStatusMessage(character.DisplayName, Config.Join);
//After spawn on world, remove from connecting list
_conecting.Remove(character.ControlSteamId);
}
});
});

}
}

/// <inheritdoc />
public override void Dispose()
{
if (_multibase != null)
{
_multibase.PlayerJoined -= _multibase_PlayerJoined;
MyEntities.OnEntityAdd -= MyEntities_OnEntityAdd;
_multibase.PlayerLeft -= _multibase_PlayerLeft;
}

Expand All @@ -174,10 +213,5 @@ public override void Dispose()

StopTimer();
}

UserControl IWpfPlugin.GetControl()
{
return _control ?? (_control = new SEDBControl(this));
}
}
}

0 comments on commit 95ceb85

Please sign in to comment.