// Tweets for announcments to discord public async void GetTweetAsync(object source, ElapsedEventArgs e) { try { var twitter = new Twitter { OAuthConsumerKey = Creds.TwitterOauthKey, OAuthConsumerSecret = Creds.TwitterOauthSecret, AccessToken = Creds.TwitterAccessToken, AccessKey = Creds.TwitterAccessSecret }; ulong channelid = new ulong(); string tweetchannel = ""; bool everyonetag = false; using (var db = new LiteDatabase(@"Config.db")) { var DatabaseConfig = db.GetCollection <DatabaseConfig>("DatabaseConfig"); var config = DatabaseConfig.FindOne(x => x.Id.Equals(1)); if (DatabaseConfig.Count() == 0) { config = new DatabaseConfig { Everyonetag = false, TweetChannel = "announcements" }; DatabaseConfig.Insert(config); } tweetchannel = config.TweetChannel; everyonetag = config.Everyonetag; } // Guardian ID 405513567681642517 // Bot Test 486327167035244554 foreach (var channelparser in Client.GetGuild(405513567681642517).TextChannels) { if (channelparser.Name.Trim().ToLower() == tweetchannel.ToLower().Trim()) { channelid = channelparser.Id; break; } } List <Tweet> twitts = twitter.GetTwitts("GuardianWire", 15); bool tweeted = false; foreach (var t in twitts) { if (Lastrun <= t.created_at) { if (t.text != String.Empty) { await Client.GetGuild(405513567681642517).GetTextChannel(channelid).SendMessageAsync(t.text); tweeted = true; } if (t.media != null && t.video == null) { await Client.GetGuild(405513567681642517).GetTextChannel(channelid).SendMessageAsync(t.media); tweeted = true; } if (t.url != null) { await Client.GetGuild(405513567681642517).GetTextChannel(channelid).SendMessageAsync(t.url); tweeted = true; } if (t.video != null) { await Client.GetGuild(405513567681642517).GetTextChannel(channelid).SendMessageAsync(t.video); tweeted = true; } } } if (tweeted == true) { if (everyonetag == true) { await Client.GetGuild(405513567681642517).GetTextChannel(channelid).SendMessageAsync("@everyone"); } } Lastrun = DateTime.UtcNow; } catch { } }
/// <summary> /// Used for starting the connection to discord and loading settings /// </summary> public async Task MainAsync() { // Get the current directory string curDir = Directory.GetCurrentDirectory(); Validation.ValidateConfigFiles(curDir); // Assuming everything exists load in the json file string credsfile = File.ReadAllText(curDir + "/botsettings.json"); creds = JsonConvert.DeserializeObject <Config>(credsfile); // Load the Database from google sheets List <UserData> users = GoogleData.ReadDB(); using (var db = new LiteDatabase(@"Guardians.db")) { var Guardians = db.GetCollection <UserData>("Guardians"); foreach (UserData user in users) { UserData Guardian = null; foreach (var Guard in Guardians.FindAll()) { if (Guard.DiscordUsername.ToLower().Trim().Contains(user.DiscordUsername.ToLower().Trim())) { Guardian = Guard; break; } } if (Guardian != null) { Guardian.Team = user.Team; Guardians.Update(Guardian); } else { if (user.DiscordUsername.Contains("#")) { try { using (var eventdb = new LiteDatabase(@"Events.db")) { var Events = eventdb.GetCollection <Events>("Events"); var Event = Events.FindAll(); Guardian = new UserData { Event = Event.First().Event, DiscordUsername = user.DiscordUsername, Team = user.Team, Authenticated = false, Channels = null, GroupMeBot = null, GroupMeGroup = null, GroupMeTime = null }; Guardians.Insert(Guardian); } } catch { } } } } // Index document using a document property Guardians.EnsureIndex(x => x.DiscordUsername); } // Initalize the client _services = ConfigureServices(); discordclient = _services.GetRequiredService <DiscordSocketClient>(); discordclient.Log += LogAsync; discordclient.MessageUpdated += MessageEditedAsync; discordclient.Ready += Ready; discordclient.MessageReceived += MessageReceivedAsync; // Tokens should be considered secret data, and never hard-coded. await discordclient.LoginAsync(TokenType.Bot, creds.BotToken); await discordclient.StartAsync(); Twitter TweetGrabber = new Twitter { Creds = creds, Client = discordclient, Lastrun = lastrun }; System.Timers.Timer TwitterTimer = new System.Timers.Timer(); TwitterTimer.Elapsed += new ElapsedEventHandler(TweetGrabber.GetTweetAsync); TwitterTimer.Interval = 60000; TwitterTimer.Enabled = true; System.Timers.Timer SMSGroup = new System.Timers.Timer(); SMSGroup.Elapsed += new ElapsedEventHandler(Timer); SMSGroup.Interval = 60000; SMSGroup.Enabled = true; System.Timers.Timer FileWatcher = new System.Timers.Timer(); FileWatcher.Elapsed += new ElapsedEventHandler(Filewatcher); FileWatcher.Interval = 5000; FileWatcher.Enabled = true; System.Timers.Timer Activity = new System.Timers.Timer(); Activity.Elapsed += new ElapsedEventHandler(ActivityEventAsync); Activity.Interval = TimeSpan.FromMinutes(48).TotalMilliseconds; Activity.Enabled = true; await discordclient.SetGameAsync("Murder She Wrote", null, ActivityType.Watching); activity++; // Block the program until it is closed. await Task.Delay(-1); }