forked from GeyserMC/GeyserDiscordBot
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
630 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
query SponsorsQuery($username: String!, $count: Int!, $cursor: String!) { | ||
user(login: $username) { | ||
sponsors(first: $count, after: $cursor) { | ||
totalCount | ||
pageInfo { | ||
endCursor | ||
hasNextPage | ||
} | ||
nodes { | ||
__typename | ||
... on User { | ||
login | ||
sponsorshipsAsSponsor(first: 1) { | ||
nodes { | ||
isActive | ||
isOneTimePayment | ||
tier { | ||
monthlyPriceInCents | ||
} | ||
tierSelectedAt | ||
createdAt | ||
} | ||
totalRecurringMonthlyPriceInCents | ||
} | ||
} | ||
... on Organization { | ||
login | ||
sponsorshipsAsSponsor(first: 1) { | ||
nodes { | ||
isActive | ||
isOneTimePayment | ||
tier { | ||
monthlyPriceInCents | ||
} | ||
tierSelectedAt | ||
createdAt | ||
} | ||
totalRecurringMonthlyPriceInCents | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
schema: schema.json | ||
documents: '**/*.graphql' |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/main/java/org/rtm516/discordbot/commands/CheckDonateCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (c) 2024 GeyserMC. http://geysermc.org | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @author GeyserMC | ||
* @link https://github.com/GeyserMC/GeyserDiscordBot | ||
*/ | ||
|
||
package org.rtm516.discordbot.commands; | ||
|
||
import com.jagrosh.jdautilities.command.SlashCommand; | ||
import com.jagrosh.jdautilities.command.SlashCommandEvent; | ||
import net.dv8tion.jda.api.EmbedBuilder; | ||
import net.dv8tion.jda.api.entities.UserSnowflake; | ||
import net.dv8tion.jda.api.interactions.InteractionHook; | ||
import net.dv8tion.jda.api.utils.TimeFormat; | ||
import org.rtm516.discordbot.util.BotColors; | ||
import org.rtm516.discordbot.util.Sponsor; | ||
import org.rtm516.discordbot.util.SponsorUtil; | ||
|
||
import java.util.Optional; | ||
|
||
public class CheckDonateCommand extends SlashCommand { | ||
public CheckDonateCommand() { | ||
this.name = "checkdonate"; | ||
this.help = "Checks if your linked github account has an active sponsorship"; | ||
this.guildOnly = true; | ||
} | ||
|
||
@Override | ||
protected void execute(SlashCommandEvent event) { | ||
InteractionHook interactionHook = event.deferReply(true).complete(); | ||
|
||
// Check if the user has linked their github account | ||
String ghUsername = SponsorUtil.getGithub(event.getUser()); | ||
if (ghUsername == null) { | ||
interactionHook.editOriginalEmbeds(new EmbedBuilder() | ||
.setTitle("No linked github account found") | ||
.setDescription("Please link your github account on " + SponsorUtil.getGithubLink(event.getUser())) | ||
.setColor(BotColors.FAILURE.getColor()) | ||
.build()).queue(); | ||
return; | ||
} | ||
|
||
// Check if the user has an active sponsorship | ||
SponsorUtil.getSponsors().thenAccept(sponsors -> { | ||
Optional<Sponsor> foundSponsor = sponsors.stream().filter(sponsor -> sponsor.username().equals(ghUsername)).findFirst(); | ||
|
||
if (foundSponsor.isPresent() && foundSponsor.get().active()) { | ||
interactionHook.editOriginalEmbeds(new EmbedBuilder() | ||
.setTitle("Active Sponsorship Found!") | ||
.addField("Github Username", foundSponsor.get().username(), true) | ||
.addField("Amount", "$" + String.format("%.02f", foundSponsor.get().amount()), true) | ||
.addField("Re-occuring", String.valueOf(!foundSponsor.get().oneTime()), true) | ||
.addField("Started", TimeFormat.DATE_TIME_SHORT.format(foundSponsor.get().started()), true) | ||
.setColor(BotColors.SUCCESS.getColor()) | ||
.build()).queue(); | ||
|
||
SponsorUtil.checkAdd(event.getGuild(), foundSponsor.get()); | ||
} else { | ||
interactionHook.editOriginalEmbeds(new EmbedBuilder() | ||
.setTitle("No active sponsorship found") | ||
.addField("Github Username", ghUsername, true) | ||
.setColor(BotColors.FAILURE.getColor()) | ||
.build()).queue(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright (c) 2024 GeyserMC. http://geysermc.org | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* @author GeyserMC | ||
* @link https://github.com/GeyserMC/GeyserDiscordBot | ||
*/ | ||
|
||
package org.rtm516.discordbot.http; | ||
|
||
import com.sun.net.httpserver.HttpExchange; | ||
import com.sun.net.httpserver.HttpServer; | ||
import org.rtm516.discordbot.util.SponsorUtil; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.net.URLDecoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
public class Server { | ||
private final HttpServer server; | ||
|
||
public Server() throws Exception { | ||
server = HttpServer.create(new InetSocketAddress("0.0.0.0", 3000), 0); | ||
server.createContext("/", exchange -> { | ||
if (exchange.getRequestURI().getQuery() == null) { | ||
respond("Invalid request", exchange); | ||
return; | ||
} | ||
|
||
Map<String, String> queryParams = queryToMap(exchange.getRequestURI().getQuery()); | ||
|
||
String code = queryParams.getOrDefault("code", null); | ||
String state = queryParams.getOrDefault("state", null); | ||
|
||
if (code == null || state == null) { | ||
respond("Invalid request", exchange); | ||
} else { | ||
respond(SponsorUtil.linkGithub(UUID.fromString(state), code), exchange); | ||
} | ||
}); | ||
server.setExecutor(null); // creates a default executor | ||
} | ||
|
||
public void start() { | ||
server.start(); | ||
} | ||
|
||
public void stop() { | ||
server.stop(0); | ||
} | ||
|
||
private void respond(String message, HttpExchange exchange) throws IOException { | ||
String response = "<html><body><h1>" + message + "</h1></body></html>"; | ||
|
||
exchange.getResponseHeaders().set("Content-Type", "text/html; charset=UTF-8"); | ||
byte[] bytes = response.getBytes(StandardCharsets.UTF_8); | ||
exchange.sendResponseHeaders(200, bytes.length); | ||
OutputStream os = exchange.getResponseBody(); | ||
os.write(bytes); | ||
os.close(); | ||
} | ||
|
||
/** | ||
* Take a raw query string and transforming it into a key value map | ||
* | ||
* @param query The raw query string | ||
* @return The key value map of the query params | ||
*/ | ||
public static Map<String, String> queryToMap(String query) { | ||
if(query == null) { | ||
return null; | ||
} | ||
Map<String, String> result = new HashMap<>(); | ||
for (String param : query.split("&")) { | ||
param = URLDecoder.decode(param, StandardCharsets.UTF_8); | ||
String[] entry = param.split("="); | ||
if (entry.length > 1) { | ||
result.put(entry[0], entry[1]); | ||
} else { | ||
result.put(entry[0], ""); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.