ShiftOS_TheReturn/ShiftOS.Server/ChatBackend.cs

140 lines
5.6 KiB
C#
Raw Normal View History

2017-02-12 17:11:28 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ShiftOS.Objects;
using NetSockets;
using Newtonsoft.Json;
using System.IO;
using static ShiftOS.Server.Program;
using Discord;
2017-02-13 15:33:43 +00:00
using Discord.WebSocket;
2017-02-13 18:12:05 +00:00
using Discord.Net.WebSockets;
2017-02-12 17:11:28 +00:00
namespace ShiftOS.Server
{
public static class ChatBackend
{
2017-02-13 15:33:43 +00:00
public static async Task StartDiscordBots()
2017-02-12 17:11:28 +00:00
{
Reinitialized?.Invoke();
2017-02-13 15:33:43 +00:00
if (!File.Exists("chats.json"))
File.WriteAllText("chats.json", "[]");
2017-02-12 17:11:28 +00:00
foreach (var chat in JsonConvert.DeserializeObject<List<ShiftOS.Objects.Channel>>(File.ReadAllText("chats.json")))
{
2017-02-13 18:12:05 +00:00
string chatID = chat.ID;
2017-02-12 17:11:28 +00:00
bool chatKilled = false;
if (chat.IsDiscordProxy == true)
{
2017-02-13 15:33:43 +00:00
DiscordSocketConfig builder = new DiscordSocketConfig();
builder.AudioMode = Discord.Audio.AudioMode.Disabled;
2017-02-13 18:12:05 +00:00
builder.WebSocketProvider = () => Discord.Net.Providers.WS4Net.WS4NetProvider.Instance();
2017-02-13 15:33:43 +00:00
var client = new DiscordSocketClient(builder);
await client.LoginAsync(TokenType.Bot, chat.DiscordBotToken);
await client.ConnectAsync();
await client.SetGameAsync("ShiftOS");
await client.SetStatusAsync(UserStatus.Online);
2017-02-13 21:49:51 +00:00
//Get the Discord channel for this chat.
var Chan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel;
//Relay the message to Discord.
await Chan.SendMessageAsync("**Hello! Multi-user domain is online.**");
2017-02-13 15:33:43 +00:00
client.MessageReceived += async (s) =>
2017-02-12 17:11:28 +00:00
{
if (chatKilled == false)
{
2017-02-13 15:33:43 +00:00
if (s.Channel.Id == Convert.ToUInt64(chat.DiscordChannelID))
2017-02-12 17:11:28 +00:00
{
2017-02-13 18:12:05 +00:00
if (s.Author.Id != client.CurrentUser.Id)
2017-02-12 17:11:28 +00:00
{
2017-02-13 18:12:05 +00:00
server.DispatchAll(new NetObject("chat_msgreceived", new ServerMessage
{
Name = "chat_msgreceived",
GUID = "server",
Contents = JsonConvert.SerializeObject(new ChatMessage(s.Author.Username, "discord_" + s.Channel.Name, (s as SocketUserMessage).Resolve(0), chatID))
}));
}
2017-02-12 17:11:28 +00:00
}
}
};
MessageReceived += (g, msg) =>
{
if (chatKilled == false)
{
//Determine if the message was sent to this channel.
if (msg.Channel == chat.ID)
{
//Get the Discord channel for this chat.
2017-02-13 18:12:05 +00:00
var dChan = client.GetChannel(Convert.ToUInt64(chat.DiscordChannelID)) as ISocketMessageChannel;
2017-02-12 17:11:28 +00:00
//Relay the message to Discord.
2017-02-13 21:49:51 +00:00
dChan.SendMessageAsync($"**[{msg.Username}@{msg.SystemName}]** `<mud/{msg.Channel}>` {msg.Message}");
2017-02-12 17:11:28 +00:00
}
//Relay it back to all MUD clients.
RelayMessage(g, msg);
}
};
Reinitialized += () =>
{
2017-02-13 15:33:43 +00:00
client.DisconnectAsync();
2017-02-12 17:11:28 +00:00
chatKilled = true;
};
}
else
{
MessageReceived += (g, msg) =>
{
if (chatKilled == false)
{
//Just relay it.
RelayMessage(g, msg);
}
};
Reinitialized += () => { chatKilled = true; };
}
}
}
internal static void RelayMessage(string guid, ChatMessage msg)
{
server.DispatchAllExcept(new Guid(guid), new NetObject("chat_msgreceived", new ServerMessage
{
Name = "chat_msgreceived",
GUID = "server",
Contents = JsonConvert.SerializeObject(msg)
}));
}
public static event Action<string, ChatMessage> MessageReceived;
public static event empty Reinitialized;
public delegate void empty();
2017-02-12 23:39:41 +00:00
[MudRequest("chat_getallchannels")]
public static void GetAllChannels(string guid, object contents)
{
server.DispatchTo(new Guid(guid), new NetObject("chat_all", new ServerMessage
{
Name = "chat_all",
GUID = "Server",
Contents = (File.Exists("chats.json") == true) ? File.ReadAllText("chats.json") : "[]"
}));
}
2017-02-12 17:11:28 +00:00
[MudRequest("chat_send")]
public static void ReceiveMessage(string guid, object contents)
{
2017-02-13 18:12:05 +00:00
var args = contents as Dictionary<string, object>;
var msg = new ChatMessage(args["Username"] as string, args["SystemName"] as string, args["Message"] as string, args["Channel"] as string);
2017-02-12 17:11:28 +00:00
MessageReceived?.Invoke(guid, msg);
}
}
2017-02-13 18:12:05 +00:00
2017-02-12 17:11:28 +00:00
}