Пример #1
0
        private static Task Discord_AllianceMessage(DSharpPlus.EventArgs.MessageCreateEventArgs e)
        {
            if (debugMode)
            {
                if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                {
                    MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                    ShipyardCommands.SendMessage("Discord", "Got a message " + e.Message.ChannelId + " " + e.Channel.Id, Color.Blue, (long)player.Id.SteamId);
                }
            }
            if (allianceChannels.ContainsKey(e.Channel.Id))
            {
                if (debugMode)
                {
                    if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                    {
                        MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                        ShipyardCommands.SendMessage("Discord 1", "Is an alliance channel " + e.Message.ChannelId + " " + e.Channel.Id, Color.Blue, (long)player.Id.SteamId);
                    }
                }
                if (e.Author.IsBot)
                {
                    if (debugMode)
                    {
                        if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                        {
                            MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                            ShipyardCommands.SendMessage("Discord 2", "Bot message " + e.Message.ChannelId + " " + e.Channel.Id, Color.Blue, (long)player.Id.SteamId);
                        }
                    }
                    //if (LastMessageSent.ContainsKey(allianceChannels[e.Channel.Id]))
                    //{
                    //    if (LastMessageSent[allianceChannels[e.Channel.Id]].Equals(e.Message.Content))
                    //    {
                    //        return Task.CompletedTask;
                    //    }
                    //}
                    String[] split = e.Message.Content.Split(':');
                    int      i     = 0;
                    if (WorldName.Equals("") && MyMultiplayer.Static.HostName != null)
                    {
                        if (MyMultiplayer.Static.HostName.Contains("SENDS"))
                        {
                            WorldName = MyMultiplayer.Static.HostName.Replace("SENDS", "");
                        }
                        else
                        {
                            if (MyMultiplayer.Static.HostName.Equals("Sigma Draconis Lobby"))
                            {
                                WorldName = "01";
                            }
                            else
                            {
                                WorldName = MyMultiplayer.Static.HostName;
                            }
                        }
                    }

                    String exclusionBeforeFormat = GetStringBetweenCharacters(split[0], '[', ']');
                    if (!exclusionBeforeFormat.Contains(WorldName))
                    {
                        StringBuilder message = new StringBuilder();
                        foreach (String s in split)
                        {
                            if (i == 0)
                            {
                                i++;
                                continue;
                            }
                            message.Append(s);
                        }
                        StringBuilder newMessage = new StringBuilder();
                        string        output     = e.Message.Content.Substring(e.Message.Content.IndexOf(':') + 1);
                        AllianceChat.SendChatMessageFromDiscord(allianceChannels[e.Channel.Id], split[0].Replace("**", ""), output.Replace("**", "").Trim(), 0);
                    }
                }
                else
                {
                    if (debugMode)
                    {
                        if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                        {
                            MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                            ShipyardCommands.SendMessage("Discord 3", "Player message " + e.Message.ChannelId + " " + e.Channel.Id, Color.Blue, (long)player.Id.SteamId);
                        }
                    }
                    if (WorldName.Equals("") && MyMultiplayer.Static.HostName != null)
                    {
                        if (MyMultiplayer.Static.HostName.Contains("SENDS"))
                        {
                            WorldName = MyMultiplayer.Static.HostName.Replace("SENDS", "");
                        }
                        else
                        {
                            if (MyMultiplayer.Static.HostName.Equals("Sigma Draconis Lobby"))
                            {
                                WorldName = "01";
                            }
                            else
                            {
                                WorldName = MyMultiplayer.Static.HostName;
                            }
                        }
                    }
                    if (debugMode)
                    {
                        if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                        {
                            MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                            ShipyardCommands.SendMessage("Discord 4", "Player message 1", Color.Blue, (long)player.Id.SteamId);
                        }
                    }
                    //if (nickNames.ContainsKey(e.Message.Author.Id))
                    //{
                    //    if (debugMode)
                    //    {
                    //        if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                    //        {
                    //            MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                    //            ShipyardCommands.SendMessage("Discord 5", "Player message 2", Color.Blue, (long)player.Id.SteamId);
                    //        }
                    //    }
                    AllianceChat.SendChatMessageFromDiscord(allianceChannels[e.Channel.Id], "[D] " + e.Message.Author.Username, e.Message.Content.Trim(), e.Author.Id);
                    //}
                    //else
                    //{
                    //    if (debugMode)
                    //    {
                    //        if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                    //        {
                    //            MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                    //            ShipyardCommands.SendMessage("Discord 6", "Player message 3", Color.Blue, (long)player.Id.SteamId);
                    //        }
                    //    }
                    //    Task.Run(async () =>
                    //    {

                    //        String nick;
                    //          DiscordMember mem = await e.Guild.GetMemberAsync(e.Author.Id);
                    //        nick = mem.Nickname;
                    //        if (String.IsNullOrEmpty(nick))
                    //        {
                    //            nickNames.Add(e.Message.Author.Id, mem.DisplayName);
                    //        }
                    //        else
                    //        {
                    //            nickNames.Add(e.Message.Author.Id, nick);
                    //        }
                    //        AllianceChat.SendChatMessageFromDiscord(allianceChannels[e.Channel.Id], "[D] " + nickNames[e.Message.Author.Id], e.Message.Content.Trim(), e.Author.Id);
                    //    });

                    //}
                    //e.Message.Author.
                    //String nick = e.Guild.GetMemberAsync(e.Author.Id).Result.Nickname;
                    //if (String.IsNullOrEmpty(nick))
                    //{

                    //}
                    //else
                    //{
                    //   AllianceChat.SendChatMessageFromDiscord(allianceChannels[e.Channel.Id], "[D] " + nick, e.Message.Content.Trim(), e.Author.Id);
                    //}
                }
            }
            else
            {
                if (debugMode)
                {
                    if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                    {
                        MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                        ShipyardCommands.SendMessage("Discord 3", "Message channel not alliance channel " + e.Message.ChannelId + " " + e.Channel.Id, Color.Blue, (long)player.Id.SteamId);
                    }
                }
            }
            return(Task.CompletedTask);
        }
Пример #2
0
        //public static void testConnection()
        //{
        //    MySqlConnection conn = new MySqlConnection(connStr);
        //    try
        //    {
        //        AlliancePlugin.Log.Info("Connecting to MySQL...");
        //        conn.Open();


        //        string sql = "CREATE DATABASE IF NOT EXISTS AllianceBank";
        //        MySqlCommand cmd = new MySqlCommand(sql, conn);
        //        cmd.ExecuteNonQuery();
        //        sql = "CREATE TABLE IF NOT EXISTS AllianceBank.BankBalances(allianceId CHAR(36) PRIMARY KEY, balance BIGINT)";
        //        cmd = new MySqlCommand(sql, conn);
        //        cmd.ExecuteNonQuery();
        //        AlliancePlugin.Log.Info("Created the tables if it needed to");
        //    }
        //    catch (Exception ex)
        //    {
        //        ReadyToSave = false;
        //        conn.Close();
        //        AlliancePlugin.Log.Error("Error connecting to database, disabling bank features.");
        //        AlliancePlugin.Log.Error(ex);
        //    }
        //    conn.Close();
        //    AlliancePlugin.Log.Info("Successfully connected to database!");
        //}
        public static void DoUpkeepForOne(Alliance alliance)
        {
            try
            {
                using (var db = new LiteDatabase(connectionString))
                {
                    var collection = db.GetCollection <BankData>("BankData");


                    FileUtils jsonStuff = new FileUtils();

                    jsonStuff.WriteToJsonFile <Alliance>(AlliancePlugin.path + "//UpkeepBackups//" + alliance.AllianceId + ".json", alliance);
                    var bank = collection.FindById(alliance.AllianceId);
                    if (bank == null)
                    {
                        bank = new BankData
                        {
                            Id      = alliance.AllianceId,
                            balance = 1
                        };
                        collection.Insert(bank);
                        alliance.failedUpkeep++;
                        if (alliance.failedUpkeep >= AlliancePlugin.config.UpkeepFailBeforeDelete)
                        {
                            AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, met the threshold for delete. Deleting Alliance.", true, 0);
                            AlliancePlugin.AllAlliances.Remove(alliance.name);
                            File.Delete(AlliancePlugin.path + "//AllianceData//" + alliance.AllianceId + ".json");
                            foreach (long id in alliance.AllianceMembers)
                            {
                                AlliancePlugin.FactionsInAlliances.Remove(id);
                            }
                        }
                        else
                        {
                            AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, Upgrades disabled.", true, 0);
                        }
                    }
                    else
                    {
                        if (bank.balance >= alliance.GetUpkeep())
                        {
                            bank.balance -= alliance.GetUpkeep();
                            collection.Update(bank);
                            AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Paying upkeep of " + String.Format("{0:n0}", alliance.GetUpkeep()) + " SC.", true, 0);
                            alliance.Upkeep(alliance.GetUpkeep(), 1);
                            alliance.bankBalance -= alliance.GetUpkeep();
                            alliance.failedUpkeep = 0;
                            AlliancePlugin.SaveAllianceData(alliance);
                        }
                        else
                        {
                            alliance.failedUpkeep++;
                            if (alliance.failedUpkeep >= AlliancePlugin.config.UpkeepFailBeforeDelete)
                            {
                                AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, met the threshold for delete. Deleting Alliance.", true, 0);
                                AlliancePlugin.AllAlliances.Remove(alliance.name);
                                File.Delete(AlliancePlugin.path + "//AllianceData//" + alliance.AllianceId + ".json");
                                foreach (long id in alliance.AllianceMembers)
                                {
                                    AlliancePlugin.FactionsInAlliances.Remove(id);
                                }
                            }
                            else
                            {
                                AllianceChat.SendChatMessage(alliance.AllianceId, "Upkeep", "Upkeep failed, Upgrades disabled.", true, 0);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                AlliancePlugin.Log.Error("Error for all upkeep");
                AlliancePlugin.Log.Error(ex);
                return;
            }
        }
Пример #3
0
        public static void SendAllianceMessage(Alliance alliance, string prefix, string message)
        {
            if (AllianceHasBot(alliance.AllianceId) && alliance.DiscordChannelId > 0)
            {
                DiscordClient bot = allianceBots[alliance.AllianceId];

                DiscordChannel chann = bot.GetChannelAsync(alliance.DiscordChannelId).Result;
                if (bot == null)
                {
                    return;
                }
                if (WorldName.Equals("") && MyMultiplayer.Static.HostName != null)
                {
                    if (MyMultiplayer.Static.HostName.Contains("SENDS"))
                    {
                        WorldName = MyMultiplayer.Static.HostName.Replace("SENDS", "");
                    }
                    else
                    {
                        if (MyMultiplayer.Static.HostName.Equals("Sigma Draconis Lobby"))
                        {
                            WorldName = "01";
                        }
                        else
                        {
                            WorldName = MyMultiplayer.Static.HostName;
                        }
                    }
                }
                try
                {
                    botId = bot.SendMessageAsync(chann, "**[" + WorldName + "] " + prefix + "**: " + message.Replace(" /n", "\n")).Result.Author.Id;
                }
                catch (DSharpPlus.Exceptions.RateLimitException)
                {
                    if (attempt <= 5)
                    {
                        attempt++;
                        SendAllianceMessage(alliance, prefix, message);
                        attempt = 0;
                    }
                    else
                    {
                        attempt = 0;
                    }
                }
                catch (System.Net.Http.HttpRequestException)
                {
                    AllianceChat.SendChatMessageFromDiscord(alliance.AllianceId, "Bot", "Failed to send message.", 0);
                }
                catch (Exception ex)
                {
                    if (debugMode)
                    {
                        if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                        {
                            MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                            ShipyardCommands.SendMessage("Discord", "" + ex.ToString(), Color.Blue, (long)player.Id.SteamId);
                        }
                    }
                }
            }
            else
            {
                if (debugMode)
                {
                    if (MySession.Static.Players.GetPlayerByName("Crunch") != null)
                    {
                        MyPlayer player = MySession.Static.Players.GetPlayerByName("Crunch");
                        ShipyardCommands.SendMessage("Discord", "doesnt have bot or channel id is 0", Color.Blue, (long)player.Id.SteamId);
                    }
                }
            }
        }