// This is called by the mod runner before connecting to the game server during startup. public void Start(IGameServerConnection gameServerConnection) { // figure out the path to the setting file in the same folder where this DLL is located. var configFilePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\" + "DiscordBotMod_Settings.yaml"; // save connection to game server for later use _gameServerConnection = gameServerConnection; // This deserializes the yaml config file _config = EmpyrionModApi.BaseConfiguration.GetConfiguration <Configuration>(configFilePath); // Tell the string to use for "!MODS" command. _gameServerConnection.AddVersionString(k_versionString); _discordClient = new DiscordClient(new DiscordConfiguration { Token = _config.DiscordToken, TokenType = TokenType.Bot }); // Subscribe for the chat event _gameServerConnection.Event_ChatMessage += OnEvent_ChatMessage; _discordClient.MessageCreated += async e => { DSharpPlus.Entities.DiscordChannel discordChannel; lock (_discordClient) { discordChannel = _discordChannel; } if (e.Author != _discordClient.CurrentUser) { if (e.Message.Content == "cb:get_online_users") { // build list of online players: string onlinePlayers = string.Join("\n", from player in _gameServerConnection.GetOnlinePlayers().Values select player.DisplayName); var dmUserChannel = await _discordClient.CreateDmAsync(e.Author); await _discordClient.SendMessageAsync(dmUserChannel, $"Online Players:\n{onlinePlayers}"); } else if (e.Channel == discordChannel) { await _gameServerConnection.SendChatMessageToAll(string.Format(_config.FromDiscordFormattingString, e.Author.Username, e.Message.Content)); } } }; _discordClient.ConnectAsync() .ContinueWith(async t => { var channel = await _discordClient.GetChannelAsync(_config.ChannelId); lock (_discordClient) { _discordChannel = channel; } }); }
private void timer_5sec_Tick(object Sender, EventArgs e) { onlinePlayersList.Items.Clear(); var factions = _gameServerConnection.GetFactions(); foreach (KeyValuePair <int, Player> playerEntry in _gameServerConnection.GetOnlinePlayers()) { Faction faction; factions.TryGetValue(playerEntry.Value.FactionIdOrEntityId, out faction); var playerLine = String.Format( "{0,-15}|{1,-7}|{2,-15}|{3,-19}|{4,-9}", playerEntry.Value.Name.PadRight(15).Substring(0, 15), ((faction != null) ? faction.Initials : "").PadRight(7).Substring(0, 7), playerEntry.Value.Playfield.PadRight(15).Substring(0, 15), playerEntry.Value.Position.position.ToString().Replace("<", "").Replace(">", "").PadRight(19).Substring(0, 19), playerEntry.Value.EntityId.ToString() ); onlinePlayersList.Items.Add(playerLine); } }