Пример #1
0
        static async Task Main(string[] args)
        {
            // get a new access token from discord api
            DiscordBotAuthorizer authorizer = new DiscordBotAuthorizer();
            DiscordToken         Token      = DiscordToken.GetTokenFromResponse(await authorizer.GetTokenResponse(Client));

            Console.WriteLine(Token.access_token);

            // Get list of slash commands from commands.json
            var listOfCommands = CommandRegistrar.GetSlashCommands();

            // Register each command from the list
            foreach (var command in listOfCommands)
            {
                var res = await CommandRegistrar.AddApplicationCommand(Client, Token, command);

                Console.WriteLine(res);
            }
        }
Пример #2
0
        public static async Task <string> AddApplicationCommand(HttpClient client, DiscordToken token, SlashCommand command)
        {
            // Get resources from App.config
            var apiEndpoint = new Uri(ConfigurationManager.ConnectionStrings["apiEndpoint"].ToString());
            var clientId    = ConfigurationManager.AppSettings["client_id"];
            var guildId     = ConfigurationManager.ConnectionStrings["guildId"].ConnectionString;

            // Get Uri for request
            Uri requestUri = new Uri($"{apiEndpoint}/applications/{clientId}/guilds/{guildId}/commands");

            // Create a new request
            var request = new HttpRequestMessage(HttpMethod.Post, requestUri);

            request.Headers.Authorization = new AuthenticationHeaderValue(token.token_type, token.access_token);

            // Set the request content type
            request.Content = new StringContent(JsonConvert.SerializeObject(command));
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            // Ask for a response from the request
            var response = await client.SendAsync(request);

            return(await response.Content.ReadAsStringAsync());
        }