package nl.voidcorp.discord.events import net.dv8tion.jda.api.MessageBuilder import net.dv8tion.jda.api.entities.ChannelType import net.dv8tion.jda.api.events.message.MessageReceivedEvent import net.dv8tion.jda.api.events.message.MessageUpdateEvent import net.dv8tion.jda.api.hooks.ListenerAdapter import nl.voidcorp.discord.command.Command import nl.voidcorp.discord.command.CommandResult import nl.voidcorp.discord.creator import nl.voidcorp.discord.logger import nl.voidcorp.discord.storage.GuildRepo import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service @Service class CommandListener( @Autowired val guildRepo: GuildRepo, @Autowired final val commands: Set ) : ListenerAdapter() { init { logger.info("Found ${commands.size} commands!") } override fun onMessageReceived(event: MessageReceivedEvent) { if (event.author.isBot) return val prefix: String = when { event.channelType == ChannelType.TEXT -> Command.settings.getPrefix(event.guild) event.channelType == ChannelType.PRIVATE -> Command.settings.prefix else -> return } if (!event.message.contentRaw.startsWith(prefix)) return val res = commands.map { it to it.onCommand(event, prefix) }.filter { it.second != CommandResult.NOPE } if (res.size > 1) { val mb = MessageBuilder("Well this is awkward... It seems that multiple actions are bound to this command... ") .append("Report this error to the staff of the server or ") val member = event.guild.getMember(creator) if (member != null) mb.append(member) else { mb.append(creator.asTag) } event.channel.sendMessage(mb.append(" since this shouldn't happen...").build()).queue() } else if (res.isEmpty()) { event.channel.sendMessage("I don't seem to know this command...").queue() } } override fun onMessageUpdate(event: MessageUpdateEvent) = onMessageReceived(MessageReceivedEvent(event.jda, event.messageIdLong, event.message)) }