Skip to content

Commit

Permalink
Killed the nazis
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Apr 15, 2018
1 parent 24a8641 commit f5874b0
Show file tree
Hide file tree
Showing 46 changed files with 1,308 additions and 1,285 deletions.
794 changes: 404 additions & 390 deletions src/main/groovy/hlaaftana/discordg/Client.groovy

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/main/groovy/hlaaftana/discordg/DSLMain.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import hlaaftana.discordg.dsl.*

@CompileStatic
class DSLMain {
static void main(String[] args){
static void main(String[] args) {
ImportCustomizer imports = new ImportCustomizer()
imports.addStarImports('hlaaftana.discordg', 'hlaaftana.discordg.dsl',
'hlaaftana.discordg.objects', 'hlaaftana.discordg.status',
Expand All @@ -23,7 +23,7 @@ class DSLMain {
script.delegate = dsl
script.run()
if (null != dsl.bot) dsl.bot.initialize()
else if (null != dsl.client) dsl.client.login()
else if (null != dsl.client) Thread.start { dsl.client.login() }
else throw new IllegalArgumentException('Why run a DSL if you aren\'t going to use it?')
}
}
15 changes: 9 additions & 6 deletions src/main/groovy/hlaaftana/discordg/DiscordG.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ class DiscordG {
static final String GITHUB = 'https://github.com/hlaaftana/DiscordG'
static final String USER_AGENT = "groovy/$GroovySystem.version DiscordBot ($GITHUB, $VERSION)"

static Client withLogin(String email, String password){
static Client withLogin(String email, String password, boolean threaded = true) {
Client client = new Client()
client.login(email, password)
def a = { client.login(email, password) }
if (threaded) Thread.start(a) else a()
client
}

static Client withToken(String token, boolean bot = true){
static Client withToken(String token, boolean bot = true, boolean threaded = true) {
Client client = new Client()
client.login(token, bot)
def a = { client.login(token, bot) }
if (threaded) Thread.start(a) else a()
client
}

static Client withToken(String botName, String token){
static Client withToken(String botName, String token, boolean threaded = true) {
Client client = new Client()
client.login(botName){ token }
def a = { client.login(botName) { token } }
if (threaded) Thread.start(a) else a()
client
}
}
96 changes: 48 additions & 48 deletions src/main/groovy/hlaaftana/discordg/DiscordObject.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ import hlaaftana.discordg.util.ConversionUtil
class DiscordObject implements Comparable, JSONable {
Client client
Map object
DiscordObject(Client c, Map o){
DiscordObject(Client c, Map o) {
client = c
object = o
}

InputStream inputStreamFromDiscord(String url){
InputStream inputStreamFromDiscord(String url) {
url.toURL().newInputStream(requestProperties:
['User-Agent': client.fullUserAgent, Accept: '*/*'])
}

File downloadFileFromDiscord(String url, file){
File downloadFileFromDiscord(String url, file) {
File f = file as File
f.withOutputStream { out ->
out << inputStreamFromDiscord(url)
Expand All @@ -35,57 +35,57 @@ class DiscordObject implements Comparable, JSONable {
f
}

Map getRawObject(){
Map getRawObject() {
def x = new HashMap(object.size())
for (e in x) {
x.put(e.key, e.value instanceof DiscordListCache ? ((DiscordListCache) e.value).rawList() : e.value)
}
x
}

Map getPatchableObject(){
Map getPatchableObject() {
def x = new HashMap(object.size())
for (e in x) {
if (!(e.value instanceof DiscordListCache)) x.put(e.key, e.value)
}
x
}

String getId(){ object.id.toString() }
String getName(){ object.name.toString() }
String toString(){ name }
String inspect(){ "'$name' ($id)" }
Date getCreatedAt(){ new Date(createdAtMillis) }
long getCreatedAtMillis(){ idToMillis(id) }
String getId() { object.id.toString() }
String getName() { object.name.toString() }
String toString() { name }
String inspect() { "'$name' ($id)" }
Date getCreatedAt() { new Date(createdAtMillis) }
long getCreatedAtMillis() { idToMillis(id) }

static long idToMillis(id){
static long idToMillis(id) {
(Long.parseLong(this.id(id)) >> 22) + 1420070400000L
}

static String millisToId(long ms, boolean raised = false){
static String millisToId(long ms, boolean raised = false) {
((ms - 1420070400000L) << 22) + (raised ? 1 << 22 : 0)
}

static String mentionToId(String mention){
static String mentionToId(String mention) {
mention.substring(Character.isDigit(mention[2] as char) ? 2 : 3, mention.length() - 1)
}

static boolean isId(long x){
static boolean isId(long x) {
x instanceof long && x > 2000000000000000
}

static boolean isId(String x){
static boolean isId(String x) {
x.isLong() && x.toLong() > 2000000000000000
}

static boolean isMention(String x){
static boolean isMention(String x) {
def a = x.charAt(0), b = x.charAt(1), c
a == ((char) '<') && b == ((char) '@') || b == ((char) '#') &&
(Character.isDigit((c = x.charAt(2))) || c == ((char) '&') || c == ((char) '!')) &&
x.charAt(x.length() - 1) == (char) '>'
}

static Map patchData(Map data, ...imageKeys = ['avatar', 'icon']){
static Map patchData(Map data, ...imageKeys = ['avatar', 'icon']) {
Map a = new HashMap(data.size())
for (e in data) {
a.put(CasingType.camel.to(CasingType.snake, e.key.toString()), e.value)
Expand All @@ -95,7 +95,7 @@ class DiscordObject implements Comparable, JSONable {
if (a.containsKey(key)){
if (ConversionUtil.isImagable(a[key])){
a[key] = ConversionUtil.encodeImageBase64(a[key])
}else{
} else {
throw new IllegalArgumentException("$key cannot be resolved " +
"for class ${data[key].getClass()}")
}
Expand All @@ -104,69 +104,69 @@ class DiscordObject implements Comparable, JSONable {
a
}

static Map find(Collection<Map> ass, value){
static Map find(Collection<Map> ass, value) {
String bong = id(value)
if (!bong || !ass) return null
if (isId(bong)){
findId(ass, bong) ?: findName(ass, bong)
}else{
} else {
findName(ass, bong)
}
}

static Map find(Collection<Map> ass, Map<String, Map> idMap, value){
static Map find(Collection<Map> ass, Map<String, Map> idMap, value) {
String bong = id(value)
if (!bong || !ass) return null
if (isId(bong) && idMap.containsKey(bong)){
idMap[bong]
}else{
} else {
findName(ass, bong)
}
}

static <T extends DiscordObject> T findBuilt(Collection<T> ass, value){
static <T extends DiscordObject> T findBuilt(Collection<T> ass, value) {
String bong = id(value)
if (!bong || !ass) return null
if (isId(bong)){
findId(ass, bong) ?: findNameBuilt(ass, bong)
}else{
} else {
findNameBuilt(ass, bong)
}
}

static <T extends DiscordObject> T findBuilt(Map<String, T> idMap, value){
static <T extends DiscordObject> T findBuilt(Map<String, T> idMap, value) {
String bong = id(value)
if (!bong || !idMap) return null
if (isId(bong) && idMap.containsKey(bong)){
idMap[bong]
}else{
} else {
findNameBuilt(idMap.values(), bong)
}
}

static <T extends DiscordObject> T find(DiscordListCache<T> cache, value){
static <T extends DiscordObject> T find(DiscordListCache<T> cache, value) {
String a = id(value)
if (!a || !cache) return null
isId(a) && cache.containsKey(a) ? cache.at(a) : findName(cache, a)
}

static Member findMember(DiscordListCache<Member> cache, value){
static Member findMember(DiscordListCache<Member> cache, value) {
String a = id(value)
if (!a || !cache) return null
isId(a) && cache.containsKey(a) ? cache.at(a) : findMemberName(cache, a)
}

static <T extends DiscordObject> List<T> findAll(DiscordListCache<T> cache, value){
static <T extends DiscordObject> List<T> findAll(DiscordListCache<T> cache, value) {
String a = id(value)
if (!a || !cache) return null
isId(a) && cache.containsKey(a) ? [cache.at(a)] : findAllName(cache, a)
}

static <T extends DiscordObject> T findNested(DiscordListCache cache, name, value){
static <T extends DiscordObject> T findNested(DiscordListCache cache, name, value) {
String x = id(value)
if (!x || !cache) return null
boolean a = isId(x)
for (e in cache){
for (e in cache) {
def c = (DiscordListCache<T>) ((Map) e.value).get(name)
if (null == c || c.isEmpty()) return null
if (a && c.containsKey(x)) return c.class().newInstance(cache.client(), c[x])
Expand All @@ -178,12 +178,12 @@ class DiscordObject implements Comparable, JSONable {
null
}

static <T extends DiscordObject> List<T> findAllNested(DiscordListCache cache, name, value){
static <T extends DiscordObject> List<T> findAllNested(DiscordListCache cache, name, value) {
String x = id(value)
if (!x || !cache) return []
boolean a = isId(x)
def d = []
for (e in cache){
for (e in cache) {
def c = (DiscordListCache<T>) ((Map) e.value).get(name)
if (null == c || c.isEmpty()) return []
if (a && c.containsKey(x)) d.add c.class().newInstance(cache.client(), c[x])
Expand All @@ -195,7 +195,7 @@ class DiscordObject implements Comparable, JSONable {
d
}

static String resolveId(thing){
static String resolveId(thing) {
if (null == thing) null
else if (thing instanceof String) isMention(thing) ? mentionToId(thing) : thing
else if (thing instanceof DiscordObject) thing.id
Expand All @@ -209,9 +209,9 @@ class DiscordObject implements Comparable, JSONable {
@CompileDynamic
private static dynamicprop(a, String name) { a."$name" }

static String id(thing){ resolveId(thing) }
static String id(thing) { resolveId(thing) }

static <T> T find(Collection<T> ass, String propertyName, value){
static <T> T find(Collection<T> ass, String propertyName, value) {
if (!ass || null == value) return null
int hash = value.hashCode()
for (x in ass) {
Expand All @@ -232,32 +232,32 @@ class DiscordObject implements Comparable, JSONable {
((Map) o.user).username == v || o.nick == v
}

static Map findName(Collection<Map> ass, value){
static Map findName(Collection<Map> ass, value) {
if (!ass || null == value) return null
ass.find(nameClosure.rcurry(value.toString()))
}

static <T extends DiscordObject> T findNameBuilt(Collection<T> ass, value){
static <T extends DiscordObject> T findNameBuilt(Collection<T> ass, value) {
if (!ass || null == value) return null
final s = value.toString()
final h = s.hashCode()
for (c in ass) if (c.name.hashCode() == h && c.name == s) return c
null
}

static <T extends DiscordObject> T findName(DiscordListCache<T> cache, value){
static <T extends DiscordObject> T findName(DiscordListCache<T> cache, value) {
if (null == value || !cache) return null
def i = cache.rawList().find(nameClosure.rcurry(value))
null == i ? null : cache.at(i.id)
}

static Member findMemberName(DiscordListCache<Member> cache, value){
static Member findMemberName(DiscordListCache<Member> cache, value) {
if (null == value || !cache) return null
def i = cache.rawList().find(memberNameClosure.rcurry(value))
null == i ? null : cache.at(i.id)
}

static <T extends DiscordObject> List<T> findAllName(DiscordListCache<T> cache, value){
static <T extends DiscordObject> List<T> findAllName(DiscordListCache<T> cache, value) {
if (null == value || !cache) return null
def i = cache.rawList().findAll(nameClosure.rcurry(value))
if (i.empty) return []
Expand All @@ -266,7 +266,7 @@ class DiscordObject implements Comparable, JSONable {
res
}

static <T> T findId(Collection<T> ass, value){
static <T> T findId(Collection<T> ass, value) {
if (null == value || !ass) return null
int hash = value.hashCode()
for (x in ass) {
Expand All @@ -276,20 +276,20 @@ class DiscordObject implements Comparable, JSONable {
null
}

DiscordObject swapClient(Client newClient){
DiscordObject swapClient(Client newClient) {
def oldNotClient = this
oldNotClient.client = newClient
oldNotClient
}

boolean isCase(other){ id(other) in id }
boolean equals(other){ id == id(other) }
boolean isCase(other) { id(other) in id }
boolean equals(other) { id == id(other) }

int hashCode(){
int hashCode() {
id.hashCode()
}
/// compares creation dates
int compareTo(other){
int compareTo(other) {
id.toLong() <=> id(other).toLong()
}

Expand Down
Loading

0 comments on commit f5874b0

Please sign in to comment.