public async Task LinkSystem(Context ctx) { ctx.CheckSystem(); await using var conn = await _db.Obtain(); var account = await ctx.MatchUser() ?? throw new PKSyntaxError("You must pass an account to link with (either ID or @mention)."); var accountIds = await _repo.GetSystemAccounts(conn, ctx.System.Id); if (accountIds.Contains(account.Id)) { throw Errors.AccountAlreadyLinked; } var existingAccount = await _repo.GetSystemByAccount(conn, account.Id); if (existingAccount != null) { throw Errors.AccountInOtherSystem(existingAccount); } var msg = $"{account.Mention}, please confirm the link by clicking the {Emojis.Success} reaction on this message."; var mentions = new IMention[] { new UserMention(account) }; if (!await ctx.PromptYesNo(msg, user: account, mentions: mentions)) { throw Errors.MemberLinkCancelled; } await _repo.AddAccount(conn, ctx.System.Id, account.Id); await ctx.Reply($"{Emojis.Success} Account linked to system."); }
/// <summary> /// Adds a mention to this interaction response /// </summary> /// <param name="mention">The mention to be added</param> /// <returns>The modified interaction builder</returns> public DiscordInteractionBuilder WithMention(IMention mention) { var list = Mentions.ToList(); list.Add(mention); Mentions = list; return(this); }
internal static Mention ToMastoNet(IMention mention) => new Mention() { Id = 0, // will not implement Url = mention.Href, AccountName = mention.WebFinger, UserName = mention.UserName };
public Task <IReference?> ResolveMentionAsync(IMention mention) { if (!IsMentionSupported(mention)) { throw new UnsupportedMentionException(mention); } return(ResolveMentionAsync((TMention)mention)); }
private static void ShowMention(IMention mention) { System.Console.WriteLine(""); System.Console.WriteLine("ID: " + mention.Id); System.Console.WriteLine("Text: " + mention.Text); System.Console.WriteLine("Has Quoted tweet: " + (mention.QuotedTweet != null)); System.Console.WriteLine("Quoted ID: " + mention.QuotedTweet?.Id); System.Console.WriteLine("Quoted Text: " + mention.QuotedTweet?.Text); System.Console.WriteLine(""); _previouslyProcessedTweets.Add(mention.Id); }
private async Task <IReference?> ConvertToReference(IMention mention) { foreach (IMentionResolver resolver in _resolvers) { if (!resolver.IsMentionSupported(mention)) { continue; } IReference?itemReference = await resolver.ResolveMentionAsync(mention); if (itemReference != null) { return(itemReference); } } return(null); }
public UnsupportedMentionException(IMention mention) : this(mention, $"Mention {mention.GetType()} is not supported") { }
public UnsupportedMentionException(IMention mention, string?message) : base(message) { Mention = mention; }
private static void ProcessMention(IMention mention) { Console.WriteLine($@" Processing: ID: {mention.Id} Text: {mention.Text}"); if (_previouslyProcessedTweets.Contains(mention.Id)) { Console.WriteLine($" {mention.Id} already processed"); return; // already processed, move on } else { _previouslyProcessedTweets.Add(mention.Id); } var pattern = @"\#?[0-9a-f]{3,6}"; var regex = new Regex(pattern, RegexOptions.IgnoreCase); var textToMatch = mention.Text; if (mention.QuotedTweet != null) { textToMatch = mention.QuotedTweet.Text; } var match = regex.Match(textToMatch); if (!match.Success) { Console.WriteLine(" No hex code found in {mention.Text}"); Tweet.PublishTweetInReplyTo($"@{mention.CreatedBy.ScreenName} couldn't find a hex code in your tweet, yo!", mention.Id); return; } var hex = match.Value; if (hex.StartsWith("#")) { hex = hex.Substring(1); } try { var imageFile = Path.Combine(_generatedImagesFolder, $"{hex}.jpg"); if (!File.Exists(imageFile)) { Console.WriteLine("File not generated previously, generating"); using (Image <Rgba32> image = new Image <Rgba32>(100, 100)) { image .Mutate(ctx => ctx.BackgroundColor(Rgba32.FromHex(hex))); image.Save(imageFile); } } else { Console.WriteLine(" File already generated, returning from cache"); } var attachment = File.ReadAllBytes(imageFile); var media = Upload.UploadImage(attachment); string mentionedNames = "@" + mention.CreatedBy.ScreenName + " "; foreach (var name in mention.UserMentions) { // skip the bot, because it will be tweet itself if (name.ScreenName != "thehexbot") { mentionedNames += "@" + name.ScreenName + " "; } } Tweet.PublishTweet($"{mentionedNames} Found '{hex}' in your tweet. Here you go!", new PublishTweetOptionalParameters { InReplyToTweetId = mention.Id, Medias = new List <IMedia> { media } }); Console.WriteLine(" Replied!"); } catch (Exception ex) { // Our problem, allow for reprocess _previouslyProcessedTweets.Remove(mention.Id); Console.WriteLine(ex.StackTrace); Tweet.PublishTweetInReplyTo($"@{mention.CreatedBy.ScreenName} Some problem occured. Sorry. I'll inform my master", mention.Id); } }
public bool IsMentionSupported(IMention mention) { return(mention is TMention); }
internal static Mention ToMastoNet(this IMention mention) => MastoNetMention.ToMastoNet(mention);