static void StartServerInfoTask() { var t = new System.Timers.Timer(); t.AutoReset = true; t.Elapsed += new System.Timers.ElapsedEventHandler((s, e) => { using (SharedDbContext shared = new SharedDbContext()) { ServerDb serverDb = shared.Servers.Where(s => s.Name == Name).FirstOrDefault(); if (serverDb != null) { serverDb.IpAddress = IpAddress; serverDb.Port = Port; serverDb.BusyScore = SessionManager.Instance.GetBusyScore(); shared.SaveChangesEx(); } else { serverDb = new ServerDb() { Name = Program.Name, IpAddress = Program.IpAddress, Port = Program.Port, BusyScore = SessionManager.Instance.GetBusyScore() }; shared.Servers.Add(serverDb); shared.SaveChangesEx(); } } }); t.Interval = 10 * 1000; t.Start(); }
public Server GetServerById(int id) { Server ret; using (var db = new ServerDb()) { lock (mutexDbAccess) { ret = db.Servers.Find(id); } } return(ret); #region [ === Old Code : For cache memory (no DB) === ] //var ctx = HttpContext.Current; //if (ctx != null) //{ // var currentData = (Server[])ctx.Cache[CacheKey]; // var server = currentData.FirstOrDefault((s) => s.Id == id); // if (server != default(Server)) // return server; //} //return null; #endregion }
// NAMIKO JOIN private async Task Client_JoinedGuild(SocketGuild arg) { DateTime now = DateTime.Now; Server server = ServerDb.GetServer(arg.Id) ?? new Server { GuildId = arg.Id, JoinDate = now }; server.LeaveDate = null; server.Prefix = AppSettings.DefaultPrefix; await ServerDb.UpdateServer(server); if (server.JoinDate.Equals(now)) { await BalanceDb.SetToasties(Client.CurrentUser.Id, 1000000, arg.Id); } SocketTextChannel ch = arg.SystemChannel ?? arg.DefaultChannel; try { await ch?.SendMessageAsync("Hi! Please take good care of me!", false, BasicUtil.GuildJoinEmbed(server.Prefix).Build()); } catch { } await WebhookClients.GuildJoinLogChannel.SendMessageAsync($"<:TickYes:577838859107303424> {Client.CurrentUser.Username} joined `{arg.Id}` **{arg.Name}**.\nOwner: `{arg.Owner.Id}` **{arg.Owner}**"); }
public ActionResult CreateNewTag(string title) { Tag newTag = new Tag(title); using (var db = new ServerDb()) { var old = db.Tags.Where(m => m.title == newTag.title) .FirstOrDefault(); // Save Existing if (old != null) { db.Tags.Remove(old); db.Tags.Add(newTag); } // Save New else { db.Tags.Add(newTag); } db.SaveChanges(); } return(PartialView(newTag)); }
public Server[] GetAllServers() { Server[] ret; using (var db = new ServerDb()) { lock (mutexDbAccess) { ret = db.Servers.ToArray(); } } return(ret); #region [ === Old Code : For cache memory (no DB) === ] //var ctx = HttpContext.Current; //if (ctx != null) //{ // return (Server[])ctx.Cache[CacheKey]; //} //return new Server[] //{ // new Server // { // Id = 0, // Name = "Placeholder" // } //}; #endregion }
public ActionResult GetTags() { using (var db = new ServerDb()) { var result = db.Tags .ToList(); return(PartialView(result)); } }
public List <MasterEntity> Servers() { using (var db = new ServerDb()) { var result = db.Servers .Include(m => m.services) .Include("applications.dependencies") .ToList(); return(result); } }
public JsonResult Entities() { using (var db = new ServerDb()) { var result = db.Servers .Include(m => m.services) .Include("applications.dependencies") .ToList(); return(Json(result, JsonRequestBehavior.AllowGet)); } }
private async Task Client_LeftGuild(SocketGuild arg) { if (!GuildLeaveEvent) { return; } var server = ServerDb.GetServer(arg.Id); server.LeaveDate = DateTime.Now; await ServerDb.UpdateServer(server); await WebhookClients.GuildJoinLogChannel.SendMessageAsync($"<:TickNo:577838859077943306> {Client.CurrentUser.Username} left `{arg.Id}` **{arg.Name}**.\nOwner: `{arg.Owner.Id}` **{arg.Owner}**"); }
void serverUpdate_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { using (var db = new ServerDb()) { lock (mutexDbAccess) { DateTime now = DateTime.Now; foreach (Server s in db.Servers) { s.LastUpdate = now; } } } }
public ActionResult Details(string id) { using (var db = new ServerDb()) { var result = db.Servers .Include(m => m.environment) .Include(m => m.services) .Include(m => m.iis) .Include("iis.websites.applicationPools") .Where(m => m.Id == id) .FirstOrDefault(); return(View(result)); } }
public async Task SetBotPrefix(string prefix) { if (prefix.Length < 1) { return; } var server = ServerDb.GetServer(Context.Guild.Id); server.Prefix = prefix; await ServerDb.UpdateServer(server); Program.UpdatePrefix(Context.Guild.Id, prefix); await Context.Channel.SendMessageAsync($"My prefix is now `{prefix}`"); }
/// <summary> /// Use to update a server /// </summary> /// <param name="id">The ID of the server you want to update</param> /// <param name="server">The data you want to update (ID is ignored)</param> /// <returns>Return the updated server if it succeeded / return null if it failed and lastExceptionThrown is updated</returns> public Server UpdateServer(int id, Server server) { Server oldServer; server.Id = id; server.LastUpdate = DateTime.Now; using (var db = new ServerDb()) { lock (mutexDbAccess) { oldServer = db.Servers.Find(id); oldServer.Copy(server); db.Entry(oldServer).State = EntityState.Modified; db.SaveChanges(); } } return(server); #region [ === Old Code : For cache memory (no DB) === ] //var ctx = HttpContext.Current; //Server newServer = null; //if (ctx != null) //{ // try // { // var currentData = ((Server[])ctx.Cache[CacheKey]).ToList(); // // Cannot have a double of IDs because it is handled by the DB (Id is the key) // var serverToUpdate = currentData.FirstOrDefault((s) => s.Id == id); // if (serverToUpdate == default(Server)) // throw new Exception("No server found with this ID : " + id); // else // newServer = updateServer(serverToUpdate, server); // ctx.Cache[CacheKey] = currentData.ToArray(); // return newServer; // } // catch (Exception ex) // { // this.lastExceptionThrown = ex; // Console.WriteLine(ex.ToString()); // return null; // } //} //return null; #endregion }
public ActionResult Index() { using (var db = new ServerDb()) { var result = db.Servers .Include(m => m.services) .Include(m => m.environment) .Include(m => m.owner) .Include("services.dependencies") .OrderByDescending(m => m.Id) .Skip(0) .Take(50) .ToList(); return(PartialView(result)); } }
// USER JOIN private async Task Client_UserJoinedWelcome(SocketGuildUser arg) { if (arg?.Guild?.Id == 417064769309245471) { return; } var server = ServerDb.GetServer(arg.Guild.Id); if (server != null && server.WelcomeChannelId != 0) { var ch = arg.Guild.GetTextChannel(server.WelcomeChannelId); if (ch != null) { await ch.SendMessageAsync(GetWelcomeMessageString(arg)); } } }
public bool DeleteServer(int id) { Server server; using (var db = new ServerDb()) { lock (mutexDbAccess) { server = db.Servers.Find(id); server = db.Servers.Remove(server); db.SaveChanges(); } } if (server != null) { return(true); } return(false); #region [ === Old Code : For cache memory (no DB) === ] //var ctx = HttpContext.Current; //if (ctx != null) //{ // try // { // var currentData = ((Server[])ctx.Cache[CacheKey]).ToList(); // var serverToDelete = currentData.FirstOrDefault((s) => s.Id == id); // if (serverToDelete == default(Server)) // return false; // currentData.Remove(serverToDelete); // ctx.Cache[CacheKey] = currentData.ToArray(); // return true; // } // catch (Exception ex) // { // this.lastExceptionThrown = ex; // Console.WriteLine(ex.ToString()); // return false; // } //} //return false; #endregion }
public async Task SetWelcomeChannel() { var server = ServerDb.GetServer(Context.Guild.Id); if (server.WelcomeChannelId == Context.Channel.Id) { server.WelcomeChannelId = 0; await ServerDb.UpdateServer(server); await Context.Channel.SendMessageAsync("Welcome channel removed."); return; } server.WelcomeChannelId = Context.Channel.Id; await ServerDb.UpdateServer(server); await Context.Channel.SendMessageAsync("Welcome channel set."); }
/// <summary> /// Use to create a new server /// </summary> /// <param name="server">The server you want to create</param> /// <returns>Return null if fails and LastExceptionThrown is updated / Return the created server if succeeded</returns> public Server SaveServer(Server server) { Server newServer; server.DateCreated = DateTime.Now; server.LastUpdate = DateTime.Now; using (var db = new ServerDb()) { lock (mutexDbAccess) { newServer = db.Servers.Add(server); db.SaveChanges(); } } return(newServer); #region [ === Old Code : For cache memory (no DB) === ] //var ctx = HttpContext.Current; //if (ctx != null) //{ // try // { // var currentData = ((Server[])ctx.Cache[CacheKey]).ToList(); // server.Id = currentData.Select(s => s.Id).Max(); // currentData.Add(server); // ctx.Cache[CacheKey] = currentData.ToArray(); // return server; // } // catch (Exception ex) // { // this.lastExceptionThrown = ex; // Console.WriteLine(ex.ToString()); // return null; // } //} //return null; #endregion }
// SET-UP private static void SetUp() { switch (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")) { case "Development": Development = true; Pause = true; break; default: Console.WriteLine("Entry: " + Assembly.GetEntryAssembly().Location); _ = ImgurAPI.ImgurSetup(); break; } NamikoDbContext.ConnectionString = AppSettings.ConnectionString; _ = LootboxStats.Reload(Locations.LootboxStatsJSON); Prefixes = ServerDb.GetPrefixes(); Images.ReactionImageCommands = ImageDb.GetReactionImageCommandHashSet(); Blacklist = BlacklistDb.GetAll(); Waifu.Path = AppSettings.ImageUrlPath + "waifus/"; }
public object Post([FromBody] MasterEntity content) { var json = this.Request.Content.ReadAsStringAsync().Result; Console.WriteLine(json); using (var db = new ServerDb()) { // Check if the server already exists in the DB var old = db.Servers.Where(m => m.Id == content.Id) .Include(m => m.environment) .Include(m => m.iis) .FirstOrDefault(); // Save Existing Server if (old != null) { db.Servers.Remove(old); db.Servers.Add(content); db.SaveChanges(); } // Save New Server else { db.Servers.Add(content); try { db.SaveChanges(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { throw; } } } return(Request.CreateResponse(HttpStatusCode.OK, "OK")); }
private async Task Client_ReactionAdded(Cacheable <IUserMessage, ulong> arg1, Cacheable <IMessageChannel, ulong> arg2, SocketReaction arg3) { if (arg3.MessageId != 700399700196458546) { return; } SocketTextChannel sch = (await arg2.GetOrDownloadAsync()) as SocketTextChannel; var user = sch.Guild.GetUser(arg3.UserId); var role = sch.Guild.GetRole(697234413360119808); if (RoleUtil.HasRole(user, role)) { return; } await user.AddRoleAsync(role); var chid = ServerDb.GetServer(sch.Guild.Id).WelcomeChannelId; var ch = sch.Guild.GetTextChannel(chid); await ch.SendMessageAsync(GetWelcomeMessageString(user)); }
private static async Task <int> CheckJoinedGuilds(DiscordSocketClient shard = null) { IReadOnlyCollection <SocketGuild> guilds; if (shard == null) { guilds = Client.Guilds; } else { guilds = shard.Guilds; } var existingIds = ServerDb.GetNotLeft(); var newIds = guilds.Where(x => !existingIds.Contains(x.Id)).Select(x => x.Id); int addedBal = await BalanceDb.AddNewServerBotBalance(newIds, Client.CurrentUser.Id); int added = await ServerDb.AddNewServers(newIds, AppSettings.DefaultPrefix); return(added); }
void serverChecker_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { using (var db = new ServerDb()) { lock (mutexDbAccess) { DateTime now = DateTime.Now; List <Server> removeList = new List <Server>(); TimeSpan? diff; foreach (Server s in db.Servers) { if (s.LastUpdate == null) { diff = null; } else { diff = now - s.LastUpdate.Value; } Trace.Write("Checing timers", "Now is : " + now.ToString() + " && LastUpdate is : " + s.LastUpdate + " && Diff is : " + diff); if (diff == null || diff.Value.TotalSeconds > SERVER_LIFETIME) { Trace.Write("Remove server cauz of timer", "Now is : " + now.ToString() + " && LastUpdate is : " + s.LastUpdate + " && Diff is : " + diff); removeList.Add(s); } } if (removeList.Count > 0) { foreach (Server s in removeList) { db.Servers.Remove(s); } //db.Servers.RemoveRange(removeList); db.SaveChanges(); } } } }
static GuildService() { BotGuilds = ServerDb.GetAll(); }
public ServerController(ServerDb Sdb) { serverDb = Sdb; }
public async Task TeamKick(IUser user, [Remainder] string str = "") { var leader = RoleUtil.GetLeaderRole(Context.Guild, Context.User); if (leader == null) { await Context.Channel.SendMessageAsync("You're not a leader! Getting ahead of yourself eh?~"); return; } var team = TeamDb.TeamByLeader(leader.Id); var userteam = RoleUtil.GetMemberRole(Context.Guild, user); var leaderteam = Context.Guild.GetRole(team.MemberRoleId); if (!userteam.Equals(leaderteam)) { await Context.Channel.SendMessageAsync($"They're not in your team! If you just want to kick them normaly use `{Program.GetPrefix(Context)}kick`"); return; } await Context.Guild.GetUser(user.Id).RemoveRoleAsync(userteam); await Context.Channel.SendMessageAsync("Ha! One less to take care of!"); ISocketMessageChannel ch = (ISocketMessageChannel)Context.Client.GetChannel(ServerDb.GetServer(Context.Guild.Id).TeamLogChannelId); await ch.SendMessageAsync($":hammer: `{Context.User}` kicked `{user}` from **{userteam.Name}**."); }
public async Task Leave([Remainder] string str = "") { var role = RoleUtil.GetMemberRole(Context.Guild, Context.User); if (role == null) { await Context.Channel.SendMessageAsync("Buuut... You're not in a team."); return; } try { await Context.Guild.GetUser(Context.User.Id).RemoveRoleAsync(role); } catch { await Context.Channel.SendMessageAsync($"I tried removing a role that is above my own role. *Coughs blood.* Discord wouldn't let me..."); return; } await Context.Channel.SendMessageAsync($@"Ha! You left **{role.Name}**! Too bad for them ¯\_(ツ)_/¯"); ISocketMessageChannel ch = (ISocketMessageChannel)Context.Client.GetChannel(ServerDb.GetServer(Context.Guild.Id).TeamLogChannelId); await ch.SendMessageAsync($"<:TickNo:577838859077943306> `{Context.User}` left **{role.Name}**."); }
public async Task Join([Remainder] string teamName) { var role = await this.SelectRole(teamName, TeamDb.Teams(Context.Guild.Id).Select(x => x.MemberRoleId), respond : false); if (role == null) { await Context.Channel.SendMessageAsync($"*~ No teams found ~*"); return; } if (InviteDb.IsInvited(role.Id, Context.User.Id)) { var user = Context.Guild.GetUser(Context.User.Id); try { await user.AddRoleAsync(role); } catch { await Context.Channel.SendMessageAsync($"I tried giving a role that is above my own role. *Coughs blood.* Discord wouldn't let me..."); return; } await InviteDb.DeleteInvite(role.Id, user.Id); await Context.Channel.SendMessageAsync($"Congratulations! You joined **{role.Name}**!"); ISocketMessageChannel ch = (ISocketMessageChannel)Context.Client.GetChannel(ServerDb.GetServer(Context.Guild.Id).TeamLogChannelId); await ch.SendMessageAsync($"<:TickYes:577838859107303424> `{Context.User}` joined **{role.Name}**."); return; } await Context.Channel.SendMessageAsync($"You're not invited to **{role.Name}**! What a shame ^^"); }
public async Task Invite([Remainder] IUser user = null) { var channel = Context.Channel; var leaderRole = RoleUtil.GetLeaderRole(Context.Guild, Context.User); if (leaderRole == null) { await channel.SendMessageAsync("You're not a team leader, silly."); return; } if (RoleUtil.IsTeamed(Context.Guild, user)) { await channel.SendMessageAsync($@"**{user.Username}** is already in a team, I guess you're too late ¯\_(ツ)_/¯"); return; } var team = TeamDb.TeamByLeader(leaderRole.Id); var teamRole = Context.Guild.GetRole(team.MemberRoleId); if (InviteDb.IsInvited(team.MemberRoleId, user.Id)) { await channel.SendMessageAsync($"You already invited **{user.Username}**, baaaaaka."); return; } await InviteDb.NewInvite(team.MemberRoleId, user.Id); await channel.SendMessageAsync($"{user.Mention} You're invited to join **{teamRole.Name}**! Type `{Program.GetPrefix(Context)}jointeam {teamRole.Name}`"); ISocketMessageChannel ch = (ISocketMessageChannel)Context.Client.GetChannel(ServerDb.GetServer(Context.Guild.Id).TeamLogChannelId); await ch.SendMessageAsync($"<:KannaHype:571690048001671238> `{Context.User}` invited `{user}` to **{teamRole.Name}**."); }
private static SocketTextChannel GetJoinLogChannel(SocketGuild guild) { return((SocketTextChannel)guild.GetChannel(ServerDb.GetServer(guild.Id).JoinLogChannelId)); }