private async Task <CouchUserStats> UserStatsRead(string bKey, string uKey) { using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.CommandType = CommandType.Text; cmd.Connection = Core.Data; cmd.CommandText = $"SELECT * FROM \"{bKey + PLUGINSTATS}\" WHERE UserKey IS @uKey AND BotChannelKey IS @bKey"; cmd.Parameters.AddWithValue("@uKey", uKey); cmd.Parameters.AddWithValue("@bKey", bKey); SQLiteDataReader result; try { result = cmd.ExecuteReader(); } catch (Exception) { await Core.LOG(new LogEntry(LOGSEVERITY.WARNING, PLUGINNAME, $"Database query failed hard. ({cmd.CommandText})")); throw; } result.Read(); CouchUserStats user = new CouchUserStats(result.GetString(0), result.GetString(1), result.GetInt32(2), result.GetInt32(3)); return(user); } }
private async Task ShakeCouch(BotChannel bChan, CouchSettings settings) { if (!settings._active || !settings._couches[bChan.Key].couchOpen) { return; } await Core.LOG(new LogEntry(LOGSEVERITY.INFO, PLUGINNAME, $"Shaking couch in {bChan.TwitchChannelName}.")); Random rng = new Random(); List <string> victims = new List <string>(); foreach (string twitchUserName in settings._couches[bChan.Key].TwitchUsernames) { if (rng.Next(1, 100) <= 20) { victims.Add(twitchUserName); } } if (victims.Count < 1) { Program.TwitchSayMessage(bChan.TwitchChannelName, dbStrings.GetRandomLine(bChan, "SHAKEF") ); return; } string msg = string.Empty; foreach (string victim in victims) { settings._couches[bChan.Key].TwitchUsernames.Remove(victim); msg += victim + ","; UserEntry usr = await Program.Users.GetUserByTwitchDisplayName(victim); if (usr != null) { CouchUserStats markUserStats = await UserStatsRead(bChan.Key, usr.Key); markUserStats.CountBooted++; UserStatsSave(bChan, markUserStats); } } Program.TwitchSayMessage(bChan.TwitchChannelName, dbStrings.GetRandomLine(bChan, "SHAKES", msg) ); settings._couches[bChan.Key].lastActivationTime = Core.CurrentTime; SaveBaseSettings(bChan, PLUGINNAME, settings); }
private async void TwitchInUserJoined(object sender, TwitchLib.Client.Events.OnUserJoinedArgs e) { if (TimerStuff.Uptime < 300) { return; } BotChannel bChan = await Program.Channels.GetTwitchChannelByName(e.Channel); if (!bChan.isLive) { return; } UserEntry user = await Program.Users.GetUserByTwitchUserName(e.Username); CouchSettings settings = await Settings <CouchSettings>(bChan, PLUGINNAME); if (user == null || settings == null) { return; } if (settings._greeted.Exists(p => p == user._twitchUsername)) { return; } if (!settings._couches.ContainsKey(bChan.Key)) { settings._couches[bChan.Key] = new CouchEntry(); } if (user != null && bChan != null && settings._active) { CouchUserStats uStats = await GetUserCouchStats(bChan.Key, user.Key); if (uStats.CountSeated >= settings.potatoGreeting) { if (!await dbStrings.TableInit(bChan)) { await DBStringsFirstSetup(bChan); } Program.TwitchSayMessage(e.Channel, dbStrings.GetRandomLine(bChan, "GREET").Replace("[USER]", user._twitchDisplayname)); settings._greeted.Add(user._twitchUsername); SaveBaseSettings(bChan, PLUGINNAME, settings); } } }
public void UserStatsSave(BotChannel bChan, CouchUserStats userStats) { using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.CommandType = CommandType.Text; cmd.Connection = Core.Data; cmd.CommandText = $"UPDATE \"{bChan.Key + PLUGINSTATS}\" SET " + $"BotChannelKey = @BotChannelKey, " + $"CountSeated = @CountSeated, " + $"CountBooted = @CountBooted " + $" WHERE BotChannelKey is @BotChannelKey AND UserKey is @UserKey"; cmd.Parameters.AddWithValue("@BotChannelKey", userStats.BotChannelKey); cmd.Parameters.AddWithValue("@UserKey", userStats.UserKey); cmd.Parameters.AddWithValue("@CountSeated", userStats.CountSeated); cmd.Parameters.AddWithValue("@CountBooted", userStats.CountBooted); cmd.ExecuteNonQuery(); } }
private void UserStatsCreate(string bKey, string uKey) { CouchUserStats userStats = new CouchUserStats(bKey, uKey, 0, 0); using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.CommandType = CommandType.Text; cmd.Connection = Core.Data; cmd.CommandText = $"INSERT INTO \"{bKey + PLUGINSTATS}\" VALUES (" + $"@BotChannelKey, " + $"@UserKey, " + $"@CountSeated, " + $"@CountBooted " + $")"; cmd.Parameters.AddWithValue("@BotChannelKey", bKey); cmd.Parameters.AddWithValue("@UserKey", userStats.UserKey); cmd.Parameters.AddWithValue("@CountSeated", userStats.CountSeated); cmd.Parameters.AddWithValue("@CountBooted", userStats.CountBooted); cmd.ExecuteNonQuery(); } }
private async void CommandResolve(BotWideCommandArguments args) { if (args.command.ToLower() != "couch" && args.command.ToLower() != "seat" && args.command.ToLower() != "seats") { return; } BotChannel bChan = await GetBotChannel(args); if (bChan == null) { return; } if (args.user == null) { return; } // Prep response instance BotWideResponseArguments response = new BotWideResponseArguments(args); // For Couch we don't go further if we don't have a twitch channel tied to the discord guild if (bChan.TwitchChannelName == string.Empty && args.arguments.Count == 0) { response.message = "There is no twitch channel to run a couch in."; Respond(bChan, response); return; } CouchSettings settings = await Settings <CouchSettings>(bChan, PLUGINNAME); if (!settings._couches.ContainsKey(bChan.Key)) { settings._couches[bChan.Key] = new CouchEntry(); } switch (args.command.ToLower()) { case "couch": // Broadcaster and Moderator commands if (args.isModerator || args.isBroadcaster || args.canManageMessages) { // Check so we are connected to the twitch channel if (!Program.Channels.CheckIfInTwitchChannel(bChan.TwitchChannelName)) { response.message = "Couch needs a twitch channel connection. See \"twitch\" command."; Respond(bChan, response); return; } if (args.arguments.Count == 0) { if (args.source == MESSAGESOURCE.DISCORD) { string message = $"```fix{System.Environment.NewLine}Admin/Broadcaster commands {System.Environment.NewLine}" + $"{Program.CommandCharacter}couch < Arguments >{System.Environment.NewLine}{System.Environment.NewLine}" + $"Arguments....{System.Environment.NewLine}" + $"< none > ->responds current settings{System.Environment.NewLine}" + $"open -> Manually resets and open the couch.{System.Environment.NewLine}" + $"on/off -> Turns plugin on or off for the channel.{System.Environment.NewLine}" + $"size # -> Sets the number of seats between 1 and 40.{System.Environment.NewLine}" + $"greet # -> Sets the number of seated needed in stats for a greeting when a user joins the twitch channel.{System.Environment.NewLine}" + $"time # -> Sets the time in seconds the couch will stay open.{System.Environment.NewLine}{System.Environment.NewLine}" + $"Discord only arguments(make sure adminchannel is set in adminplugin){System.Environment.NewLine}" + $"addsuccess < text > Text being the line returned. Use [USER] in text where username should be.{System.Environment.NewLine}" + $"addfail < text >{System.Environment.NewLine}" + $"addgreet < text >{System.Environment.NewLine}" + $"addincident < text >{System.Environment.NewLine}" + $"list / list # -> Shows stored lines by page.{System.Environment.NewLine}" + $"use # -> Toggles the inuse flag for the line with given ID.{System.Environment.NewLine}" + $"delete # -> Deletes the line with the ID if inuse flag is false. As in not in use.{System.Environment.NewLine}" + System.Environment.NewLine + System.Environment.NewLine + $"User commands{System.Environment.NewLine}" + $"{Program.CommandCharacter}seat -> When couch open it responds with success of fail message.{System.Environment.NewLine}" + $"{Program.CommandCharacter}seats -> User stats rundown.{System.Environment.NewLine}```"; await SayOnDiscord(message, args.channelID); } if (settings._active) { response.message = $"Couch is active. {settings.couchsize} seats. Greetlimit is {settings.potatoGreeting}. Open time is {settings.openTime}"; Respond(bChan, response); } else { response.message = $"Couch is inactive. {settings.couchsize} seats. Greetlimit is {settings.potatoGreeting}. Open time is {settings.openTime}"; Respond(bChan, response); } return; } switch (args.arguments[0].ToLower()) { case "addfail": if (args.arguments.Count >= 2) { await AddLine(bChan, "FAIL", args.arguments); } break; case "addgreet": if (args.arguments.Count >= 2) { await AddLine(bChan, "GREET", args.arguments); } break; case "addincident": if (args.arguments.Count >= 2) { await AddLine(bChan, "INCIDENT", args.arguments); } break; case "addsuccess": if (args.arguments.Count >= 2) { await AddLine(bChan, "SUCCESS", args.arguments); } break; case "addtardy": if (args.arguments.Count >= 2) { await AddLine(bChan, "TARDY", args.arguments); } break; case "addshakes": if (args.arguments.Count >= 2) { await AddLine(bChan, "SHAKES", args.arguments); } break; case "addshakef": if (args.arguments.Count >= 2) { await AddLine(bChan, "SHAKEF", args.arguments); } break; case "close": CloseCouch(bChan, settings); break; case "dbgreet": response.message = dbStrings.GetRandomLine(bChan, "GREET"); response.parseMessage = true; Respond(bChan, response); break; case "delete": if (args.arguments.Count == 1) { return; } int id = -1; int.TryParse(args.arguments[1], out id); if (id <= 0) { return; } await DeleteEntry(bChan, args.channelID, id); break; case "greet": if (args.arguments.Count == 2) { int greet = settings.potatoGreeting; int.TryParse(args.arguments[1], out greet); if (greet > 0 && greet <= 100 && greet != settings.potatoGreeting) { settings.potatoGreeting = greet; response.message = $"Couch greeting limit is now {settings.potatoGreeting}."; Respond(bChan, response); await SayOnDiscordAdmin(bChan, $"{args.userDisplayName} changed the Couch Greetlimit setting to {settings.potatoGreeting}."); SaveBaseSettings(bChan, PLUGINNAME, settings); } else { response.message = $"Couch greeting limit has to be more than 0 and max 100."; Respond(bChan, response); } } break; case "off": settings._active = false; SaveBaseSettings(bChan, PLUGINNAME, settings); response.message = $"Couch is inactive. {settings.couchsize} seats. Greetlimit is {settings.potatoGreeting}. Open time is {settings.openTime}"; Respond(bChan, response); break; case "on": settings._active = true; SaveBaseSettings(bChan, PLUGINNAME, settings); response.message = $"Couch is active. {settings.couchsize} seats. Greetlimit is {settings.potatoGreeting}. Open time is {settings.openTime}"; Respond(bChan, response); break; case "open": if (!settings._active) { return; } OpenCouch(bChan, settings); break; /* TODO never look at this again!! * case "restore": * bool removed = await dbStrings.TableDrop(bChan); * if (removed) * { * await SayOnDiscordAdmin(bChan, "Removed all current line data from the database."); * } * else * { * await SayOnDiscordAdmin(bChan, "Couldn't remove anything from the database."); * } * break;*/ case "rock": case "shake": await ShakeCouch(bChan, settings); break; case "size": if (args.arguments.Count == 2) { int seats = settings.couchsize; int.TryParse(args.arguments[1], out seats); if (seats > 0 && seats <= 100 && seats != settings.couchsize) { settings.couchsize = seats; response.message = $"Couch now has {settings.couchsize} seats."; Respond(bChan, response); await SayOnDiscordAdmin(bChan, $"{args.userDisplayName} changed the Couch size to {settings.couchsize}."); SaveBaseSettings(bChan, PLUGINNAME, settings); } else { response.message = $"Couch size limit has to be more than 0 and max 100."; Respond(bChan, response); } } break; case "time": if (args.arguments.Count == 2) { int timer = settings.openTime; int.TryParse(args.arguments[1], out timer); if (timer > 0 && timer <= 10000 && timer != settings.openTime) { settings.openTime = timer; response.message = $"Couch time limit is now {settings.openTime} seconds."; Respond(bChan, response); await SayOnDiscordAdmin(bChan, $"{args.user} changed the Couch open time limit setting to {settings.openTime} seconds."); SaveBaseSettings(bChan, PLUGINNAME, settings); } else { response.message = $"Couch time limit has to be more than 0 and max 10000 seconds."; Respond(bChan, response); } } break; case "use": if (args.arguments.Count == 1) { return; } id = -1; int.TryParse(args.arguments[1], out id); if (id <= 0) { return; } await ToggleInUse(bChan, id); break; case "who": if (!settings._active || args.source != MESSAGESOURCE.DISCORD) { return; } response.message = GetAllSittersAsString(bChan, settings); Respond(bChan, response); break; case "list": if (args.source != MESSAGESOURCE.DISCORD) { return; } if (args.arguments.Count == 1) { await ListLinesFromDB(bChan, args.channelID, 0); return; } int page = 0; int.TryParse(args.arguments[1], out page); if (page <= 0) { page = 1; } await ListLinesFromDB(bChan, args.channelID, page - 1); break; } } break; // User Commands case "seat": if (!settings._couches[bChan.Key].couchOpen || !settings._active) { return; } // To late if (Core.CurrentTime > settings._couches[bChan.Key].lastActivationTime + settings.openTime) { // only give feedback a specified count on fails if (settings.failCount <= settings.maxFails) { Respond(bChan, new BotWideResponseArguments(args) { source = args.source, twitchChannel = bChan.TwitchChannelName, discordChannel = bChan.discordAdminChannel, user = args.user, victim = null, message = dbStrings.GetRandomLine(bChan, "TARDY"), parseMessage = true }); settings.failCount++; SaveBaseSettings(bChan, PLUGINNAME, settings); } return; } if (settings._couches[bChan.Key].TwitchUsernames.Contains(args.userDisplayName)) { return; } if (settings._couches[bChan.Key].TwitchUsernames.Count < settings.couchsize) { if (settings._couches[bChan.Key].TwitchUsernames.Count != 0) { if (RollIncident(15)) { string rngSitter = GetRNGSitter(bChan, settings); if (rngSitter != null) { UserEntry victim = await Program.Users.GetUserByTwitchDisplayName(rngSitter); if (victim != null) { settings._couches[bChan.Key].TwitchUsernames.RemoveAll(p => p == victim._twitchUsername); if (!await UserStatsExists(bChan.Key, args.user.Key)) { UserStatsCreate(bChan.Key, args.user.Key); } CouchUserStats userStats1 = await UserStatsRead(bChan.Key, args.user.Key); userStats1.CountSeated++; UserStatsSave(bChan, userStats1); if (!await UserStatsExists(bChan.Key, victim.Key)) { UserStatsCreate(bChan.Key, args.user.Key); } CouchUserStats markUserStats = await UserStatsRead(bChan.Key, victim.Key); markUserStats.CountBooted++; UserStatsSave(bChan, markUserStats); response.victim = victim; response.message = dbStrings.GetRandomLine(bChan, "INCIDENT"); response.parseMessage = true; Respond(bChan, response); SaveBaseSettings(bChan, PLUGINNAME, settings); } } } } if (!await UserStatsExists(bChan.Key, args.user.Key)) { UserStatsCreate(bChan.Key, args.user.Key); } CouchUserStats userStats = await UserStatsRead(bChan.Key, args.user.Key); userStats.CountSeated++; UserStatsSave(bChan, userStats); settings._couches[bChan.Key].TwitchUsernames.Add(args.userDisplayName); Respond(bChan, new BotWideResponseArguments(args) { message = dbStrings.GetRandomLine(bChan, "SUCCESS"), parseMessage = true }); SaveBaseSettings(bChan, PLUGINNAME, settings); } else { Program.TwitchSayMessage(args.channel, dbStrings.GetRandomLine(bChan, "FAIL").Replace("[USER]", args.user._twitchDisplayname) ); settings.failCount++; SaveBaseSettings(bChan, PLUGINNAME, settings); } break; case "seats": CouchUserStats cStats = await GetUserCouchStats(bChan.Key, args.user.Key); Program.TwitchSayMessage(args.channel, $"{args.user._twitchDisplayname}, you have sat in couch {cStats.CountSeated} times. {cStats.CountBooted} times you fell off." ); break; } }