Beispiel #1
0
        public async Task ProcessAsync(SocketMessage message)
        {
            int        verificationCode = Convert.ToInt32(message.Content);
            SocketUser messageAuthor    = message.Author;
            bool       isCodeCorrect    = this.verificationCodeManager.IsCodeCorrectForUser(verificationCode, messageAuthor.Id);

            if (isCodeCorrect == false)
            {
                NonBlockingLogger.Warn($"{messageAuthor.Username} provided a wrong verification code: {verificationCode}. Correct would have been: {this.verificationCodeManager.GetCodeForUser(messageAuthor.Id)}");
                await messageAuthor.SendMessageAsync(
                    "Hmmm... Es sieht aus, als wäre das der falsche Code. Bitte überprüfe, ob du mir den richtigen " +
                    "Code geschickt hast. Falls Ja, gib mir nochmals deine Mail Adresse, dann schicke ich dir ein neues Mail.\n\r" +
                    "Hmmm... It looks like that's the wrong code. Please make sure that you entered the code correctly. If you did, " +
                    "send me your mail address again and I'll send you another mail with a new verification code.");

                return;
            }

            SocketGuild     socketGuild     = messageAuthor.MutualGuilds.Single(sg => sg.CurrentUser != null && sg.CurrentUser.Guild.Id == sg.Id);
            SocketGuildUser socketGuildUser = socketGuild.Users.Single(sgu => sgu.Id == messageAuthor.Id);

            NonBlockingLogger.Info($"Verification code {verificationCode} is correct for user {messageAuthor.Username}");
            SocketRole socketRole = socketGuild.Roles.Single(sr => sr.Name == this.discordClient.StudentRoleName);
            await socketGuildUser.AddRoleAsync(socketRole);

            await socketGuildUser.SendMessageAsync("Danke vielmals. Du bist nun verifiziert als Student.\n\rThank you very much. You're now verified as a student.");

            NonBlockingLogger.Info($"Assigned role @student to {messageAuthor.Username}");
            this.verificationCodeManager.RemoveCodesForUser(messageAuthor.Id);
        }
Beispiel #2
0
        public async Task <string> GetAccessToken()
        {
            AuthenticationResult result;

            // If there is no saved user account, the user must sign-in
            if (this.userAccount == null)
            {
                try
                {
                    // Invoke device code flow so user can sign-in with a browser
                    result = await this.msalClient.AcquireTokenWithDeviceCode(
                        this.scopes,
                        callback =>
                    {
                        NonBlockingLogger.Info(callback.Message);
                        return(Task.CompletedTask);
                    }).ExecuteAsync();

                    this.userAccount = result.Account;
                    return(result.AccessToken);
                }
                catch (Exception exception)
                {
                    NonBlockingLogger.Error($"Error getting access token: {exception.Message}");
                    return(null);
                }
            }

            // If there is an account, call AcquireTokenSilent
            // By doing this, MSAL will refresh the token automatically if
            // it is expired. Otherwise it returns the cached token.
            result = await this.msalClient.AcquireTokenSilent(this.scopes, this.userAccount).ExecuteAsync();

            return(result.AccessToken);
        }
        public async Task Start()
        {
            Config config = this.configLoader.LoadConfigFromFile();

            NonBlockingLogger.Info($"Service started with the following config: {config}");
            await this.mailService.Initialize(config.FromMailAddress, config.FromName, config.AppId, config.Scopes);

            await this.orchestrator.InitializeDiscordAsync(config.DiscordApplicationToken, config.GuildName, config.AnnouncementRoleName, config.StudentRoleName);
        }
Beispiel #4
0
        public async Task SendMailToAsync(string mailAdress, string subject, string messageBody)
        {
            try
            {
                Message message = new Message
                {
                    Subject = subject,
                    Body    = new ItemBody {
                        ContentType = BodyType.Text, Content = messageBody
                    },
                    ToRecipients = new List <Recipient> {
                        new Recipient {
                            EmailAddress = new EmailAddress {
                                Address = mailAdress
                            }
                        }
                    },
                    Sender = new Recipient {
                        EmailAddress = new EmailAddress {
                            Address = this.fromMailAddress, Name = this.fromName
                        }
                    },
                    From = new Recipient {
                        EmailAddress = new EmailAddress {
                            Address = this.fromMailAddress, Name = this.fromName
                        }
                    }
                };

                await this.graphServiceClient.Me.SendMail(message).Request().PostAsync();
            }
            catch (Exception e)
            {
                NonBlockingLogger.Error($"{e.Message}\nStacktrace: {e.StackTrace}");
            }
        }
        public async Task ProcessAsync(SocketMessage message)
        {
            int verificationCode = this.verificationCodeManager.CreateCodeForUser(message.Author.Id);

            string messageBody = $"Hi {message.Author.Username}\n\r"
                                 + $"Hier ist dein Verifizierungscode für den STAIR Discord Server: {verificationCode}.\n\r"
                                 + $"Bitte kopiere den Code und sende ihn mir persönlich via Discord. Danach bekommst du "
                                 + $"die @student Rolle auf dem STAIR Discord Server zugewiesen.\n\r"
                                 + $"Falls du irgendwelche Fragen bezüglich mir, dem Discord Server oder STAIR hast, "
                                 + $"zögere nicht ein STAIR Mitglied zu fragen (grün markiert im Discord).\n\r\n\r"
                                 + $"Here is your verification code for the STAIR Discord server: {verificationCode}\n\r"
                                 + $"Please copy the verification code and send it to me via Discord. Then you'll be "
                                 + $"assigned the @student role on the STAIR Discord server\n\r"
                                 + $"If you have any questions about me, the Discord server or about STAIR please don't "
                                 + $"hesitate to ask a STAIR member (marked green on Discord).\n\r\n\r"
                                 + $"Liebe Grüsse/Kind regards\n"
                                 + $"Stan";

            await this.mailService.SendMailToAsync(message.Content, "STAIR Discord Verification", messageBody);

            await message.Channel.SendMessageAsync($"Vielen Dank! Ich habe ein Mail an {message.Content} geschickt.\n\rThanks! I've sent a mail to {message.Content}.");

            NonBlockingLogger.Info($"Sent verification mail to: {message.Author.Username} with mail: {message.Content}. Verification code: {verificationCode}");
        }
 public void Stop()
 {
     NonBlockingLogger.Info("Service stopped!");
 }
Beispiel #7
0
 private static void HandleException(Exception exception)
 {
     NonBlockingLogger.Error($"Unhandled exception occured: {exception.Message}\nStacktrace: {exception.StackTrace}");
 }