From 7d516a14471c864b88b4090666f739a62c8c35a8 Mon Sep 17 00:00:00 2001 From: Not Archiey Date: Tue, 2 Jun 2020 15:50:56 +0700 Subject: [PATCH] Add Avatar command (#19) * Added avatar command * Use List#isNotEmpty for args check --- .../yuutoproject/yuutobot/commands/Avatar.kt | 47 +++++++++++++++++++ .../yuutoproject/yuutobot/commands/Ship.kt | 18 ++----- .../yuutobot/utils/memberUtils.kt | 35 ++++++++++++++ 3 files changed, 85 insertions(+), 15 deletions(-) create mode 100644 src/main/kotlin/io/github/yuutoproject/yuutobot/commands/Avatar.kt create mode 100644 src/main/kotlin/io/github/yuutoproject/yuutobot/utils/memberUtils.kt diff --git a/src/main/kotlin/io/github/yuutoproject/yuutobot/commands/Avatar.kt b/src/main/kotlin/io/github/yuutoproject/yuutobot/commands/Avatar.kt new file mode 100644 index 0000000..c9a0a8d --- /dev/null +++ b/src/main/kotlin/io/github/yuutoproject/yuutobot/commands/Avatar.kt @@ -0,0 +1,47 @@ +/* + * Open source bot built by and for the Camp Buddy Discord Fan Server. + * Copyright (C) 2020 Kyuuto-devs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package io.github.yuutoproject.yuutobot.commands + +import io.github.yuutoproject.yuutobot.commands.base.AbstractCommand +import io.github.yuutoproject.yuutobot.commands.base.CommandCategory +import io.github.yuutoproject.yuutobot.utils.findMember +import net.dv8tion.jda.api.entities.User +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent + +class Avatar : AbstractCommand( + "avatar", + CommandCategory.UTIL, + "Gets your own or someone's avatar", + "avatar or avatar " +) { + override fun run(args: MutableList, event: GuildMessageReceivedEvent) { + var user: User? = event.author + + if (args.isNotEmpty()) { + user = findMember(args.joinToString(" "), event)?.user + } + + if (user == null) { + event.channel.sendMessage("${event.author.asMention} Sorry, but I can't find that user").queue() + return + } + + event.channel.sendMessage("${event.author.asMention}, Here ya go~!\n" + user.effectiveAvatarUrl + "?size=2048").queue() + } +} diff --git a/src/main/kotlin/io/github/yuutoproject/yuutobot/commands/Ship.kt b/src/main/kotlin/io/github/yuutoproject/yuutobot/commands/Ship.kt index e397c1c..512e23c 100644 --- a/src/main/kotlin/io/github/yuutoproject/yuutobot/commands/Ship.kt +++ b/src/main/kotlin/io/github/yuutoproject/yuutobot/commands/Ship.kt @@ -18,11 +18,11 @@ package io.github.yuutoproject.yuutobot.commands -import com.jagrosh.jdautilities.commons.utils.FinderUtil import io.github.yuutoproject.yuutobot.commands.base.AbstractCommand import io.github.yuutoproject.yuutobot.commands.base.CommandCategory import io.github.yuutoproject.yuutobot.extensions.getStaticAvatarUrl import io.github.yuutoproject.yuutobot.utils.Constants +import io.github.yuutoproject.yuutobot.utils.findMember import io.github.yuutoproject.yuutobot.utils.httpClient import io.github.yuutoproject.yuutobot.utils.jackson import java.io.File @@ -73,7 +73,7 @@ class Ship : AbstractCommand( } val name1 = args[0] - val member1 = this.findMember(name1, event) + val member1 = findMember(name1, event) if (member1 == null) { channel.sendMessage("No user found for input $name1").queue() @@ -81,7 +81,7 @@ class Ship : AbstractCommand( } val name2 = args[1] - val member2 = this.findMember(name2, event) + val member2 = findMember(name2, event) if (member2 == null) { channel.sendMessage("No user found for input $name2").queue() @@ -112,18 +112,6 @@ class Ship : AbstractCommand( } } - private fun findMember(input: String, event: GuildMessageReceivedEvent): Member? { - val foundMembers = FinderUtil.findMembers(input, event.guild) - - if (foundMembers.isEmpty()) { - return null - } - - // why not "?: null" - // Well java is fun and will throw an index out of bounds exception if there is no first element - return foundMembers[0] - } - private fun shouldBeRigged(member1: Member, member2: Member): Boolean { val id1 = member1.idLong val id2 = member2.idLong diff --git a/src/main/kotlin/io/github/yuutoproject/yuutobot/utils/memberUtils.kt b/src/main/kotlin/io/github/yuutoproject/yuutobot/utils/memberUtils.kt new file mode 100644 index 0000000..1dc3157 --- /dev/null +++ b/src/main/kotlin/io/github/yuutoproject/yuutobot/utils/memberUtils.kt @@ -0,0 +1,35 @@ +/* + * Open source bot built by and for the Camp Buddy Discord Fan Server. + * Copyright (C) 2020 Kyuuto-devs + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package io.github.yuutoproject.yuutobot.utils + +import com.jagrosh.jdautilities.commons.utils.FinderUtil +import net.dv8tion.jda.api.entities.Member +import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent + +fun findMember(input: String, event: GuildMessageReceivedEvent): Member? { + val foundMembers = FinderUtil.findMembers(input, event.guild) + + if (foundMembers.isEmpty()) { + return null + } + + // why not "?: null" + // Well java is fun and will throw an index out of bounds exception if there is no first element + return foundMembers[0] +}