<commit_before>/*
 *     Copyright 2015-2016 <NAME> & <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.dv8tion.jda.utils;

import net.dv8tion.jda.JDA;
import net.dv8tion.jda.Permission;
import net.dv8tion.jda.entities.Channel;
import net.dv8tion.jda.entities.Guild;
import net.dv8tion.jda.entities.User;
import net.dv8tion.jda.entities.impl.JDAImpl;
import net.dv8tion.jda.exceptions.PermissionException;
import net.dv8tion.jda.requests.Requester;
import org.json.JSONArray;
import org.json.JSONObject;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class InviteUtil
{
    /**
     * Takes an invite url or invite code and changes it into an {@link net.dv8tion.jda.utils.InviteUtil.Invite Invite}.
     * If the url or code isn't a proper invite, this returns <code>null</code>
     *
     * @param code
     *          The invite url or code
     * @return
     *      An {@link net.dv8tion.jda.utils.InviteUtil.Invite Invite} representing the invite specified by the provided
     *      code or url. This will return <code>null</code> if the provided code or url is invalid.
     * @throws java.lang.NullPointerException
     *      If the provided String is null.
     */
    public static Invite resolve(String code)
    {
        if (code == null)
            throw new NullPointerException("The provided invite code/url was null.");
        if (code.startsWith("http"))
        {
            String[] split = code.split("/");
            code = split[split.length - 1];
        }
        JSONObject object = new JDAImpl(false, false).getRequester().get(Requester.DISCORD_API_PREFIX + "invite/" + code).getObject();
        if (object != null && object.has("code"))
        {
            JSONObject guild = object.getJSONObject("guild");
            JSONObject channel = object.getJSONObject("channel");
            return new Invite(object.getString("code"), object.isNull("xkcdpass") ? null : object.getString("xkcdpass"), guild.getString("name"), guild.getString("id"),
                    channel.getString("name"), channel.getString("id"), channel.getString("type").equals("text"));
        }
        return null;
    }

    /**
     * Creates a standard-invite (valid for 24hrs, infinite usages, permanent access and not human-readable).
     * To create a customized Invite, use {@link #createInvite(Channel, InviteDuration, int, boolean, boolean)} instead.
     *
     * @param chan
     *      The channel to create the invite for.
     * @return
     *      The created AdvancedInvite object.<commit_msg>InviteUtil Failing when trying to get GUild<commit_after>/*
 *     Copyright 2015-2016 <NAME> & <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.dv8tion.jda.utils;

import net.dv8tion.jda.JDA;
import net.dv8tion.jda.Permission;
import net.dv8tion.jda.entities.Channel;
import net.dv8tion.jda.entities.Guild;
import net.dv8tion.jda.entities.User;
import net.dv8tion.jda.entities.impl.JDAImpl;
import net.dv8tion.jda.exceptions.PermissionException;
import net.dv8tion.jda.requests.Requester;
import org.json.JSONArray;
import org.json.JSONObject;

import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class InviteUtil
{
    /**
     * Takes an invite url or invite code and changes it into an {@link net.dv8tion.jda.utils.InviteUtil.Invite Invite}.
     * If the url or code isn't a proper invite, this returns <code>null</code>
     *
     * @param code
     *          The invite url or code
     * @return
     *      An {@link net.dv8tion.jda.utils.InviteUtil.Invite Invite} representing the invite specified by the provided
     *      code or url. This will return <code>null</code> if the provided code or url is invalid.
     * @throws java.lang.NullPointerException
     *      If the provided String is null.
     */
    public static Invite resolve(String code)
    {
        if (code == null)
            throw new NullPointerException("The provided invite code/url was null.");
        if (code.startsWith("http"))
        {
            String[] split = code.split("/");
            code = split[split.length - 1];
        }
        JSONObject object = new JDAImpl(false, false).getRequester().get(Requester.DISCORD_API_PREFIX + "invite/" + code).getObject();
        if (object != null && object.has("code") && object.has("guild"))
        {
            JSONObject guild = object.getJSONObject("guild");
            JSONObject channel = object.getJSONObject("channel");
            return new Invite(object.getString("code"), object.isNull("xkcdpass") ? null : object.getString("xkcdpass"), guild.getString("name"), guild.getString("id"),
                    channel.getString("name"), channel.getString("id"), channel.getString("type").equals("text"));
        }
        return null;
    }

    /**
     * Creates a standard-invite (valid for 24hrs, infinite usages, permanent access and not human-readable).
     * To create a customized Invite, use {@link #createInvite(Channel, InviteDuration, int, boolean, boolean)} instead.
     *
     * @param chan
     *      The channel to create the invite for.
     * @return
     *      The created AdvancedInvite object.
{'fraction_non_alphanumeric': 0.1092090664130607, 'fraction_numerical': 0.008242193691551752, 'mean_word_length': 3.024234693877551, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3949722', 'n_tokens_mistral': 1839, 'n_tokens_neox': 1791, 'n_words': 564}