public async Task AddStream(string streamer) { Console.WriteLine("Executing add command for " + streamer); string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); string namepath = Path.Combine(guildpath, FileDirUtil.JSONNAMES); List <string> names = JSONUtil.GetJsonToList <string>(namepath); //"Link" can be twitch link or just the channel name string name = MessageUtil.GetChannelNameFromMessage(streamer); if (name == null) { await ReplyAsync("Invalid link, please check again"); return; } if (!names.Contains(name)) { names.Add(name); JSONUtil.WriteJsonToFile(names, namepath); await ReplyAsync("Successfully added streamer: " + name); //refresh IDS after adding await MessageUtil.GetChannelIDs(guildid, true); } else { await ReplyAsync("I already have that streamer stored, try !!list to check the current list"); } await Context.Message.DeleteAsync(); Console.WriteLine("leaving addstreamer"); }
public void CreateDirフォルダ作成成功() { Assert.IsTrue(FileDirUtil.CreateDir("c:\\test", "test2")); Assert.IsTrue(Directory.Exists("C:\\test\\test2")); Assert.IsTrue(FileDirUtil.CreateDir("c:\\test", "あ①")); Assert.IsTrue(Directory.Exists("C:\\test\\あ①")); }
public async Task PriceCheck(string coin, string cur = "USD", string expanded = null) { Console.WriteLine("Command: crypto pricecheck"); //get the api-correct name for the coin string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); Dictionary <string, string> coindict = JSONUtil.GetJsonToDic <string, string>(Path.Combine(guildpath, FileDirUtil.COINS)); string coinname = GetFullCoinName(coindict, coin); string message = "Could not find the coin mentioned, try updating my record!"; if (coinname != null) { if (cur == "full") { cur = "usd"; expanded = "full"; } string response = Request(coinname, cur); message = FormatCoinPriceResponse(response, cur.ToLower(), expanded); } await ReplyAsync(message); }
public async Task GetHelp() { Console.WriteLine("executing help command"); string id = Context.Guild.Id.ToString(); string settingsfile = FileDirUtil.GetGuildFile(id, FileDirUtil.JSONSETTINGS); Settings guildsettings = JSONUtil.GetSettingsObj(id); //get the settings for this guild string helpstring = ""; helpstring += "List of commands for this bot:\n**{0}start** - tells the bot start monitoring for live streams (notifies in same channel)\n"; helpstring += "**{0}stop** - Tells the bot to stop monitoring (NOTE this may take up to 1 minute to fufil)\n"; helpstring += "**{0}add <twitch name or link>** - to add a streamer to watch out for\n"; helpstring += "**{0}remove <twitch name or link>** - to remove a streamer from the watch list\n"; helpstring += "**{0}notify <on/off>** - Adds the “notify” role to the user invoking the command. (Creates the role if it doesn’t already exist)\n"; helpstring += "**{0}list** - lists current watch list"; helpstring += "\n"; helpstring += "**{0}prefix <new prefix>** - Changes the default or existing prefix to the new one entered\n"; helpstring += "**{0}addresponse <trigger> <response>** - The bot will watch out for the trigger word and reply with a response when seen\n"; helpstring += "**{0}deleteresponse <trigger>** - Deletes the response for that trigger\n"; helpstring += "**{0}responses** - Lists the current stored responses for this server"; helpstring += "**{0}stats <user>** - Returns the most used words from that user\n"; helpstring += "**{0}stats all** - Returns the top word for each user in the chat log\n"; helpstring += "**{0}stats server** - Returns the top words in the server\n"; helpstring += "\n"; helpstring += "**{0}pc <coin> <currency>(opt) full(opt)** - Returns basic or full price info for a coin\n"; helpstring = String.Format(helpstring, guildsettings.prefix); await ReplyAsync(helpstring); }
public void CreateFileファイル作成成功() { Assert.IsTrue(FileDirUtil.CreateFile("c:\\test", "test2.txt", 0)); Assert.IsTrue(File.Exists("C:\\test\\test2.txt")); Assert.IsTrue(FileDirUtil.CreateFile("c:\\test", "あ①.txt", 0)); Assert.IsTrue(File.Exists("C:\\test\\あ①.txt")); }
public async Task StartMon(string startfile) { Console.WriteLine("start mon"); while (true) { if (File.Exists(startfile)) { await CheckLive(Context.Message, Context.Guild.Id.ToString()); System.Threading.Thread.Sleep(60000); } else { break; } } //once the monitor stops, remove the stop file string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); try { File.Delete(Path.Combine(guildpath, FileDirUtil.JSONSTOP)); Console.WriteLine("removed stop file"); } catch (Exception e) { Console.WriteLine("could not remove stop file: " + e); } await ReplyAsync("**Monitor stopped**"); }
public static Dictionary <string, Dictionary <string, int> > AggregateRecords(SocketCommandContext context) { Console.WriteLine("aggregating records.."); string filepath = Path.Combine(FileDirUtil.GetGuildDir(context.Guild.Id.ToString()), FileDirUtil.PROCESSLOG); Console.WriteLine("reading from the csv file.."); List <ChatRecord> records = File.ReadAllLines(filepath) .Select(v => FromCSV(v)) .ToList(); Dictionary <string, Dictionary <string, int> > aggregate = new Dictionary <string, Dictionary <string, int> >(); // {user: {word1:0..}} Console.WriteLine("looking through records"); foreach (ChatRecord record in records) { if (aggregate.ContainsKey(record.User)) { //Console.WriteLine("Contained key " + record.User); foreach (string word in record.Words.Keys) { //Console.WriteLine("looking at word " + word); if (aggregate[record.User].ContainsKey(word)) { aggregate[record.User][word] += record.Words[word]; } else { aggregate[record.User].Add(word, record.Words[word]); } } } else { aggregate.Add(record.User, record.Words); } } Console.WriteLine("sorting word counts"); //sort the word collections for each user based on the number of instances of each word foreach (KeyValuePair <string, Dictionary <string, int> > pair in aggregate.ToList()) { //var mylist = aggregate[pair.Key].ToList(); //mylist.Sort((x, y) => x.Value.CompareTo(y.Value)); var sortedDict = from entry in aggregate[pair.Key] orderby entry.Value descending select entry; aggregate[pair.Key] = sortedDict.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); } Console.WriteLine("Aggregated orederd recrods"); return(aggregate); }
public async Task ChangePrefix(string newpref) { string id = Context.Guild.Id.ToString(); string settingsfile = FileDirUtil.GetGuildFile(id, FileDirUtil.JSONSETTINGS); Settings guildsettings = JSONUtil.GetSettingsObj(id); //get the settings for this guild guildsettings.prefix = newpref; JSONUtil.WriteJsonToFile(guildsettings, settingsfile); await ReplyAsync($"The prefix for the bot has been changed to **{newpref}**"); }
private async Task CleanGuilds() { var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; Guilds guilds = JsonConvert.DeserializeObject <Guilds>(File.ReadAllText(FileDirUtil.JSONGUILDS), settings); if (guilds.guilds != null) { foreach (Guild guild in guilds.guilds) { string startfile = FileDirUtil.GetGuildFile(guild.id, FileDirUtil.JSONSTART); string stopfile = FileDirUtil.GetGuildFile(guild.id, FileDirUtil.JSONSTOP); if (File.Exists(startfile)) { try { File.Delete(FileDirUtil.GetGuildFile(guild.id, FileDirUtil.JSONSTART)); Console.WriteLine("Cleaned up start files for " + guild.name); } catch (Exception e) { Console.WriteLine("couldn't clean up start files " + e); } } else { Console.WriteLine("no start file present, carry on"); } if (File.Exists(stopfile)) { try { File.Delete(FileDirUtil.GetGuildFile(guild.id, FileDirUtil.JSONSTOP)); Console.WriteLine("Cleaned up stop files for " + guild.name); } catch (Exception e) { Console.WriteLine("couldn't clean up stop files " + e); } } else { Console.WriteLine("no stop file present, carry on"); } } } }
private async Task JoinedGuild(SocketGuild guild) { Console.WriteLine("Entered guild "); var settings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore, MissingMemberHandling = MissingMemberHandling.Ignore }; Guilds guilds = JsonConvert.DeserializeObject <Guilds>(File.ReadAllText(FileDirUtil.JSONGUILDS), settings);//checks json containing all guilds + their settings Console.WriteLine("got guilds json"); string id = guild.Id.ToString(); string name = guild.Name; List <string> ids = guilds.GetIds(); Console.WriteLine("Entered guild " + name + " " + id); //if this is a brand new guild if ((guilds.guilds == null) || !ids.Contains(id)) { string guildpath = FileDirUtil.GetGuildDir(id); string settingfile = Path.Combine(guildpath, FileDirUtil.JSONSETTINGS); Console.WriteLine("new guild!"); Settings gdst = new Settings("!!", id, 0); Console.WriteLine("settings!"); Guild newguilld = new Guild(id, name, DateTime.Now, DateTime.Now); //create the new guild with info Console.WriteLine("newguild!"); guilds.AddGuild(newguilld); Console.WriteLine("addedguild!"); await FileDirUtil.EstablishGuildFiles(newguilld); Console.WriteLine("writing guilds back to file"); JSONUtil.WriteJsonToFile(guilds, FileDirUtil.JSONGUILDS); JSONUtil.WriteJsonToFile(gdst, settingfile); Console.WriteLine("completed setting up for new guild!"); } else { Console.WriteLine("existing guild"); await FileDirUtil.VerifyGuildFiles(id); } //write back the json }
public async Task List() { Console.WriteLine("executing list command"); string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); List <string> names = JSONUtil.GetJsonToList <string>(Path.Combine(guildpath, FileDirUtil.JSONNAMES)); Console.WriteLine("2"); string messagestring = "Here's the current list of streamers i'm looking out for!"; Console.WriteLine("3"); foreach (string name in names) { messagestring += ("\n<https://www.twitch.tv/" + name + ">"); } await ReplyAsync(messagestring); }
public async Task AddReponse(string word, string reaction) { string id = Context.Guild.Id.ToString(); string settingsfile = FileDirUtil.GetGuildFile(id, FileDirUtil.JSONSETTINGS); Settings guildsettings = JSONUtil.GetSettingsObj(id); bool addnew = guildsettings.AddOrModifyCommand(word.ToLower(), reaction); if (addnew) { await ReplyAsync($"Successfully added a new response for {word}!"); } else { await ReplyAsync($"Successfully modified the response {word}"); } JSONUtil.WriteJsonToFile(guildsettings, settingsfile); }
public async Task DeleteResponse(string word) { string id = Context.Guild.Id.ToString(); string settingsfile = FileDirUtil.GetGuildFile(id, FileDirUtil.JSONSETTINGS); Settings guildsettings = JSONUtil.GetSettingsObj(id); bool remove = guildsettings.DelCommand(word.ToLower()); if (remove) { await ReplyAsync($"Successfully deleted the response for {word}!"); } else { await ReplyAsync($"Oops, looks like {word} didn't exist anyway!"); } JSONUtil.WriteJsonToFile(guildsettings, settingsfile); }
public async Task Start() { Console.WriteLine("Executing start command"); string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); string startfile = Path.Combine(guildpath, FileDirUtil.JSONSTART); string stopfile = Path.Combine(guildpath, FileDirUtil.JSONSTOP); //if already running if (File.Exists(startfile)) { await ReplyAsync("Bot already running, if it appears to be broken try to restart"); return; } //if a stop command already issued but not completed, don't try to re-start if (File.Exists(stopfile)) { await ReplyAsync("Awaiting termination of the existing session before restarting, please try again shortly!"); return; } //if not already running + no stop command in progress, start the bot FileStream fs = File.Create(startfile); fs.Flush(); fs.Close(); await MessageUtil.GetChannelIDs(guildid); await Context.Client.SetGameAsync("with yer nan"); await ReplyAsync("Starting up the bot!"); Task monitask = Task.Run(async() => { await StartMon(startfile); }); await Context.Message.DeleteAsync(); }
public async Task Stop() { Console.WriteLine("Executing stop command"); string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); string startfile = Path.Combine(guildpath, FileDirUtil.JSONSTART); string stopfile = Path.Combine(guildpath, FileDirUtil.JSONSTOP); FileStream fs = File.Create(stopfile); fs.Flush(); fs.Close(); if (File.Exists(stopfile)) { await ReplyAsync("Waiting for previous session to end, please try again shortly"); } if (File.Exists(startfile)) { try { File.Delete(startfile); await ReplyAsync("Monitor has been stopped! Please wait up to 1 minute before attempting a restart.."); Console.WriteLine("stopped"); } catch (Exception e) { Console.WriteLine("error trying to delete startfile " + e); await ReplyAsync("Could not stop the monitoring service right now, please retry"); return; } } else { await ReplyAsync("Currently not monitoring the streams, use **start** to begin monitoring!"); } await Context.Client.SetGameAsync("AFK"); await Context.Message.DeleteAsync(); }
public async Task RemoveStream(string streamer) { string guildid = Context.Guild.Id.ToString(); string guildpath = FileDirUtil.GetGuildDir(guildid); List <string> names = JSONUtil.GetJsonToList <string>(Path.Combine(guildpath, FileDirUtil.JSONNAMES)); string name = MessageUtil.GetChannelNameFromMessage(streamer); if (names.Contains(name)) { await MessageUtil.UpdateList(name, guildid, true); await ReplyAsync("Successfully deleted streamer: " + name); } else { await ReplyAsync("Please try again, streamer could not be found!"); } await Context.Message.DeleteAsync(); Console.WriteLine("leaving deletestreamer"); }
public void CreateDirNullTest() { Assert.IsFalse(FileDirUtil.CreateDir(null, null)); Assert.IsFalse(FileDirUtil.CreateDir("c:\\", null)); Assert.IsFalse(FileDirUtil.CreateDir(null, "test")); }
public void CreateDirフォルダ作成失敗() { Trace.WriteLine("【個別】ろぐでない"); Assert.IsFalse(FileDirUtil.CreateDir("c:\\test", "test>2")); }
public void BaseDirCheckNullTest() { Assert.IsFalse(FileDirUtil.BaseDirCheck(null)); }
public async Task CheckLive(SocketUserMessage msg, string guildid) { string guildids = FileDirUtil.GetGuildFile(guildid, FileDirUtil.JSONIDS); string guildlive = FileDirUtil.GetGuildFile(guildid, FileDirUtil.JSONLIVE); string guildstream = FileDirUtil.GetGuildFile(guildid, FileDirUtil.JSONSTREAMS); //dic of users to check; dic of last known live, list of currently live Dictionary <string, string> users = JSONUtil.GetJsonToDic <string, string>(guildids); Dictionary <string, string> live = JSONUtil.GetJsonToDic <string, string>(guildlive); List <string> liveusers = new List <string>(); string s = string.Join("&channel=", users.Select(x => x.Value)); string response = SendRequest(BaseString + "streams/?channel=", s, "Requesting live channels for " + s); Console.WriteLine("channelresponse" + response); JSONUtil.WriteJsonToFile(null, guildstream, response); Streams streams = JsonConvert.DeserializeObject <Streams>(response); //if any live streams, check if already noted as live, if not, set to "live" and notify if (streams._total > 0) { foreach (Stream x in streams.streams) { liveusers.Add(x.channel.name); if (live[x.channel.name] != "live") { live[x.channel.name] = "live"; SocketGuildUser guilduser = msg.Author as SocketGuildUser; SocketRole guildrole = guilduser.Guild.Roles.FirstOrDefault(y => y.Name == "notify"); string mention = ""; if (guildrole != null) { mention = guildrole.Mention; } await msg.Channel.SendMessageAsync($"{mention} {x.channel.name} has just gone live playing {x.channel.game}! \n" + "https://www.twitch.tv/" + x.channel.name); } } } Console.WriteLine("check1"); //check the current live against previous live //if set as live when not currently live, set to offline Dictionary <string, string> newDictionary = live.ToDictionary(entry => entry.Key, entry => entry.Value); Console.WriteLine("check2"); foreach (KeyValuePair <string, string> entry in live) { if (entry.Value == "live") { if (!liveusers.Any(x => x == entry.Key)) { newDictionary[entry.Key] = "offline"; } } } Console.WriteLine("check3"); live = newDictionary.ToDictionary(entry => entry.Key, entry => entry.Value); Console.WriteLine("check4"); JSONUtil.WriteJsonToFile(live, guildlive); Console.WriteLine("Completed check\n"); }