// called whenever .sync command is used, and at first program launch
        public async Task <bool> ManualSync(DiscordServer server = null, SocketCommandContext context = null)
        {
            // if server is null, context is not null - we're calling via command, so get the right server via context
            if (server == null && context != null)
            {
                server = Servers.ServerList.Find(x => x.DiscordServerObject == context.Guild);
            }

            // check if we're authenticated and have a calendar id to sync from
            var syncStatus = CheckIfSyncPossible(server);

            if (syncStatus != CalendarSyncStatus.OK)
            {
                if (server == null && context != null)
                {
                    await context.Channel.SendMessageAsync($"Sync failed: {SyncFailedReason(syncStatus)}");
                }
                else
                {
                    await server.ConfigChannel.SendMessageAsync($"Sync failed: {SyncFailedReason(syncStatus)}");
                }

                return(false);
            }

            // perform the actual sync
            var success = SyncFromGoogleCalendar(server);

            // handle sync success or failure
            if (success)
            {
                // send message reporting we've synced calendar events
                string resultMessage = $":calendar: Synced {server.Events.Count} calendar events.";
                if (context != null) // we only want to send a message announcing sync success if the user sent the command
                {
                    await _interactiveService.ReplyAndDeleteAsync(context, resultMessage);
                }
            }
            else
            {
                // send message reporting there were no calendar events to sync
                string resultMessage = ":calendar: No events found in calendar.";
                if (context != null)
                {
                    await _interactiveService.ReplyAndDeleteAsync(context, resultMessage);
                }
            }

            // send/modify events embed in reminders to reflect newly synced values
            await _scheduleService.SendEvents(server);

            return(true);
        }
 /// <summary>
 /// Sends an embed, and deletes it after a specified timeout.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <param name="interactivity">The interactivity service.</param>
 /// <param name="colour">The colour of the embed.</param>
 /// <param name="contents">The contents of the message.</param>
 /// <param name="timeout">The timeout after which the message should be deleted.</param>
 public async Task SendEmbedAndDeleteAsync
 (
     [NotNull] SocketCommandContext context,
     [NotNull] InteractiveService interactivity,
     Color colour,
     [NotNull] string contents,
     [CanBeNull] TimeSpan?timeout = null
 )
 {
     var eb = CreateFeedbackEmbed(context.User, colour, contents);
     await interactivity.ReplyAndDeleteAsync(context, string.Empty, false, eb, timeout);
 }
Esempio n. 3
0
        public async Task Help()
        {
            await _interactiveService.ReplyAndDeleteAsync(Context, "Sending you list of my commands now!", timeout : TimeSpan.FromSeconds(10));

            await _help.HelpAsync(Context);
        }
        async Task <IUserMessage> ReplyAndDelete(SocketUserMessage original, string response, double seconds)
        {
            SocketCommandContext newContext = new SocketCommandContext(discord, original);

            return(await interactive.ReplyAndDeleteAsync(newContext, response, timeout : TimeSpan.FromSeconds(seconds)));
        }
        /// <summary>
        /// Interactively builds an embed by prompting the user.
        /// <para>
        /// Shows an embed layout guide, a preview of the embed being built, and instructions for the current prompt. The user is
        /// first prompted for an action. Then, if applicable, the user is prompted again to enter a value for the action. The
        /// builder is cancelled if it times out before an initial response is received. Otherwise, it listens indefinitely for
        /// a response.
        /// </para>
        /// <para>See <see cref="_Instructions"/> and <see cref="BuilderAction.Actions"/> for supported actions.</para>
        /// </summary>
        /// <returns>The built embed.</returns>
        public async Task <Discord.Embed> BuildAsync()
        {
            await InitAsync();

            while (true)
            {
                SocketMessage input = await _interactive.NextMessageAsync(_context);

                if (input == null)
                {
                    await _context.Channel.SendMessageAsync("```The announcement builder has timed out after 120 seconds.```");
                    await RemoveAsync();

                    return(null);
                }

                if (BuilderAction.Actions.TryGetValue(input.Content, out BuilderAction action))
                {
                    await input.DeleteAsync();

                    await _instructionsMsg.ModifyAsync(m => m.Content = action.Instructions);

                    input = await _interactive.NextMessageAsync(_context);

                    if (input == null)
                    {
                        continue;
                    }

                    // Displays an error if the callback failed and the action has an error message.
                    if (!action.Callback(input.Content, _embed) && !string.IsNullOrWhiteSpace(action.Error))
                    {
                        await _interactive.ReplyAndDeleteAsync(
                            _context,
                            $"```Input: {input.Content}\nError: {action.Error}```",
                            timeout : TimeSpan.FromSeconds(5));
                    }

                    await input.DeleteAsync();
                }
                else if (input.Content.Equals("field", StringComparison.OrdinalIgnoreCase))
                {
                    await input.DeleteAsync();
                    await HandleFieldAsync();
                }
                else if (input.Content.Equals("submit", StringComparison.OrdinalIgnoreCase))
                {
                    await input.DeleteAsync();
                    await RemoveAsync();

                    return(_embed.Build());
                }
                else if (input.Content.Equals("cancel", StringComparison.OrdinalIgnoreCase))
                {
                    await input.DeleteAsync();
                    await RemoveAsync();

                    return(null);
                }
                else
                {
                    await input.DeleteAsync();

                    await _interactive.ReplyAndDeleteAsync(
                        _context,
                        $"```Unknown action '{input.Content}'.```",
                        timeout : TimeSpan.FromSeconds(5));

                    continue;
                }

                await _previewMsg.ModifyAsync(m => m.Embed = _embed.Build());

                await _instructionsMsg.ModifyAsync(m => m.Content = _Instructions);
            }
        }